如何在多线程和UI之间传输数据 [英] How to transfer data between multithreading and UI

查看:121
本文介绍了如何在多线程和UI之间传输数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在UI中,我在Backgroundworker类中调用ProcessFiles方法。在这个processfiles方法中,我创建了一个带有monitorque方法的线程,该方法在worker类中。

现在我要传递当每个线程可能存在多个错误时,数据到UI。

我应该如何实现这一目标?我在下面放置代码。我试图创建委托,但我应该如何在另一个工人类中调用此委托方法?这样我将更新数据!提前感谢..





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..


 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)
		{
			}
}

推荐答案

你还没有真正提供足够的信息,但是只要看一眼你的实现,我可能只会做其中一个,甚至不用自己管理线程。



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.

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

        }));
    }
}


这篇关于如何在多线程和UI之间传输数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆