Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
In UI i calls ProcessFiles method in Backgroundworker class.In this processfiles method i have created threads with monitorque method which is in worker class.
Now i want to pass data to UI when there may be an multiple errors for each thread.
How should i achieve this? I am placing code below.I tried to create delegate but how should i call this delegate method in another worker class?so that i will update data !thanks in advance..


C#
 public partial class Form1 : Form
    {
 private void Bworker_DoWork(object sender, DoWorkEventArgs e)
        {
            ProcessFiles();
            
        }


private void  processFiles()
{
	    numberOfThreads = lstfiles.Items.Count;
            Thread[] threads = new Thread[numberOfThreads];
            Worker worker = new Worker();
            for (int index = 0; index < numberOfThreads; index++)
            {
                threads[index] = new Thread(worker.MonitorQueue);
                threads[index].Start();
            }
            int i=0;
            foreach (string fName in lstfiles.Items)
            {
                worker.DocumentQueue.EnqueueItem(fName);
                temp.Add(fName);                                 
            }
}
}
------------------
public class Worker
	{
        static string csvOutputFilePath =AppDomain.CurrentDomain.BaseDirectory + "\\output\\";
		private DocumentQueue documentQueue;        
		public DocumentQueue DocumentQueue
		{
			get
			{
				return documentQueue;
			}
		}

		public void MonitorQueue()
		{
			try
			{
				while (true)
				{
					object document = documentQueue.ConsumeItem();
					if ((document == null))
					{
						//enqueue another null to terminate the next thread if there is one
						documentQueue.EnqueueItem(null);
						break;
					}
					else
					{
						ProcessDocument(document.ToString());
					}
				}
			}
			catch (Exception exception)
			{
				System.Console.WriteLine(exception.Message);
			}
		}

		private void ProcessDocument(string document)
		{
			}
}
Posted
Comments
Sergey Alexandrovich Kryukov 11-Dec-15 15:36pm    
It depends on what you want to do with the data. As you did not solve this problem so far, it's not shown where you try to pass data. One of the approach is to pass not data, but delegate instance using Control.Invoke or Control.BeginInvoke. It depends on what you want to achieve.

Your "how should i call this delegate method in another worker class" is quite unclear. What is "between"? Are you asking about passing data to a non-UI thread from UI thread, or visa versa?

—SA

1 solution

You haven't really given enough information but at a glance of your implementation i would probably just do one of these and not even bother with managing the threads myself.

C#
void DoStuff()
{
    // Has been called from a "wrong" thread?
    if (InvokeRequired)
    {
        // Dispatch to correct thread, use BeginInvoke if you don't need
        // caller thread until operation completes
        Invoke(new MethodInvoker(DoStuff));
    }
    else {
        // Do things
    }
}


void DoStuff()
{
    if (InvokeRequired)
    {
        Invoke(new MethodInvoker(delegate
        {

        // Do things

        }));
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900