在不同的类更新来自不同的线程的UI [英] Update UI from a different thread in a different class

查看:112
本文介绍了在不同的类更新来自不同的线程的UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含我想换一个列表框的主要形式类。的框中填充在一个耗时的方法创建的项目。现在它看起来像这样(发明的一个例子手,可能是无效的C#):

I have the main form class which contains a list box I want to change. The box is populated with items created in a time-consuming method. Right now it looks like this (inventing an example by hand, might not be valid C#):

List<string> strings = StaticClassHelper.GetStrings(inputString);
foreach(string s in strings)
{
    listBox1.Add(s);
}

//meanwhile, in a different class, in a different file...

public static List<string> GetStrings(inputString)
{
    List<string> result = new List<string>();
    foreach(string s in inputString.Split('c'))
    {
        result.Add(s.Reverse());
        Thread.Sleep(1000);
    }
    return result;
}

我想这样做的,而不是作为新的字符串被发现定期更新列表框。其他答案我找到了工作,当线程的方法是在同一个班,这样你就可以建立一个事件处理程序。我该怎么办吗?

What I would like to do instead is regularly update the list box as new strings are found. The other answers I found work when the thread method is in the same class, so you can set up an event handler. What do I do here?

推荐答案

下面是我想做到这一点,我创建了这样的形式的方法:

Here is how I like to do this, I create a method on the form like this:

public void AddItemToList(string Item)
{
   if(InvokeRequired)
      Invoke(new Action<string>(AddItemToList), Item);
   else
      listBox1.Add(Item);
}

我preFER调用在这种情况下,确保了项目同步增加,否则他们可以走出秩序。如果你不关心的顺序,那么你可以使用的BeginInvoke 这将是一个稍快一点。由于这种方法是公共的,你都可以从任何类在应用程序中,只要你能得到一个引用到窗体。

I prefer invoke in this case to make sure the items are added synchronously, otherwise they can get out of order. If you don't care about the order then you can use BeginInvoke which will be a tad faster. Since this method is public, you can all it from any class in your application as long as you can get a reference to your form.

这样做的另一个好处是,你可以从你的UI线程或一个非UI线程调用它,它需要决定是否需要调用 ING的护理。这样,你的来电者并不需要知道哪个线程运行它们的。

Another advantage of this is that you can call it from either your UI thread or a non-UI thread and it takes care of deciding whether or not it needs Invokeing. This way your callers don't need to be aware of which thread they are running on.

更新 为了满足您对如何获得引用表格注释,通常在Windows窗体应用程序,你的Program.cs文件看起来是这样的:

UPDATE To address your comment about how to get a reference to the Form, typically in a Windows Forms app your Program.cs file looks something like this:

static class Program
{
   static void Main() 
   {
       MyForm form = new MyForm();
       Application.Run(form);  
   }

}

这是通常我会做什么,特别是在一个单表申请的情况下:

This is typically what I would do, particularly in the case of a "Single Form" application:

static class Program
{
   public static MyForm MainWindow;

   static void Main() 
   {
       mainWindow = new MyForm();
       Application.Run(form);  
   }

}

然后你就可以访问它pretty的任何地方多用:

And then you can access it pretty much anywhere with:

Program.MainWindow.AddToList(...);

这篇关于在不同的类更新来自不同的线程的UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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