线程&安培;跨线程在C#.NET,如何从另一个线程改变ComboBox的数据? [英] Threading & Cross Threading in C#.NET, How do I change ComboBox Data from another Thread?

查看:245
本文介绍了线程&安培;跨线程在C#.NET,如何从另一个线程改变ComboBox的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的应用程序使用线程,但我不知道如何执行跨线程操作。



我希望能够更改窗体对象的文本(在这种情况下,组合框),从另一个线程,我得到的错误:



 跨线程操作无效:控制'titlescomboBox从比它创建的线程以外的线程访问。 



我真的不知道如何使用的调用,并开始启动功能,因此IM真的找这个死简单的例子,解释,这样我就可以学到解决这一问题。



另外任何初学者教程将是巨大的,我找了几个,但他们都如此不同,我。不正是我需要做什么来进行跨线程OPS了解



下面是代码:

  //主线程。刷新按钮
私人无效refreshButton_Click(对象发件人,EventArgs五)
{
titlescomboBox.Items.Clear()的点击;
线T1 =新主题(updateCombo);
t1.Start();
}

//这个函数更新组合框与rssData
私人无效updateCombo()
{
rssData = getRssData(channelTextBox.Text) ; //获取的数据
(INT I = 0; I&下; rssData.GetLength(0);我++)//输出它
{

如果(rssData〔Ⅰ !,0] = NULL)
{

//跨线程操作无效:控制'titlescomboBox'
//从比它创建线程以外的线程访问上。

titlescomboBox.Items.Add(rssData [I,0]); //在这里,我得到一个错误

}
titlescomboBox.SelectedIndex = 0;
}
}


解决方案

我使用以下辅助类:

 公共静态类ControlExtensions 
{
公共静态无效的调用(此控制控制,操作动作)
{
如果(control.InvokeRequired)
{
control.Invoke(新MethodInvoker(动作),NULL);
}
,否则
{
action.Invoke();
}
}
}

现在你可以调用像 MyCombo.Invoke(()=> {MyCombo.Items.Add(东西);})的调用之前 ---或任何其他控件(如表格)因为它们都是在主线程创建



的事情是,控制可以只从他们上(在这种情况下,主应用程序线程)的创建线程访问



心连心


I need to use threading in my app, but I don't know how to perform a cross threading operation.

I want to be able to change the text of a form object (in this case a Combo Box), from another thread, I get the error:

Cross-thread operation not valid: Control 'titlescomboBox' accessed from a thread other than the thread it was created on.

I don't really understand how to use the invoke and begin invoke functions, So im really looking for a dead simple example and explanation for this so I can learn around that.

Also any beginner tutorials would be great, I found a few, but their all so different, I don't understand exactly what I need to do to perform cross threading ops.

Here is the code:

    // Main Thread. On click of the refresh button
    private void refreshButton_Click(object sender, EventArgs e)
    {
        titlescomboBox.Items.Clear();
        Thread t1 = new Thread(updateCombo);
        t1.Start();
    }

    // This function updates the combo box with the rssData
    private void updateCombo()
    {
        rssData = getRssData(channelTextBox.Text);       // Getting the Data
        for (int i = 0; i < rssData.GetLength(0); i++)   // Output it
        {

            if (rssData[i, 0] != null)
            {

              // Cross-thread operation not valid: Control 'titlescomboBox' 
              // accessed from a thread other than the thread it was created on.

              titlescomboBox.Items.Add(rssData[i, 0]);   // Here I get an Error

            }
            titlescomboBox.SelectedIndex = 0;
        }
    }

解决方案

I use the following helper class:

public static class ControlExtensions
{
    public static void Invoke(this Control control, Action action)
    {
        if (control.InvokeRequired)
        {
            control.Invoke(new MethodInvoker(action), null);
        }
        else
        {
            action.Invoke();
        }
    }
}

Now you can call something like MyCombo.Invoke(() => { MyCombo.Items.Add(something); }) --- or any other control (such as the form) before the invoke since they are all created on the main thread.

The thing is that controls can only be accessed from the thread they were created on (the main application thread in this case).

HTH

这篇关于线程&安培;跨线程在C#.NET,如何从另一个线程改变ComboBox的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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