Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 线程调用窗口窗体控件_C#_Multithreading_Winforms_Thread Safety - Fatal编程技术网

C# 线程调用窗口窗体控件

C# 线程调用窗口窗体控件,c#,multithreading,winforms,thread-safety,C#,Multithreading,Winforms,Thread Safety,我想让线程可以调用窗口窗体控件。我要这样做: delegate void SetTextCallback(String str, int i); private void SetText(string text, int i) { // InvokeRequired required compares the thread ID of the // calling thread to the thread ID of the creating thread. // I

我想让线程可以调用窗口窗体控件。我要这样做:

delegate void SetTextCallback(String str, int i);
private void SetText(string text, int i)
{
    // InvokeRequired required compares the thread ID of the 
    // calling thread to the thread ID of the creating thread. 
    // If these threads are different, it returns true. 
    if (this.label2.InvokeRequired)
    {
        SetTextCallback d = new SetTextCallback(SetText);
        this.Invoke(d, new object[] { text,i});
    }
    else
    {
        switch (i)
        {
            case 1:
                this.label1.Text = text;
                break;
            case 2:
                this.label2.Text = text;
                break;
        }

    }
}
但它似乎太长了,因为我想调用多个表单obj(label1,label2,textbox,…)->案例1,2,3,4,5,我们将有多个案例

有更好的办法吗? 在SetText中使用outint i(字符串文本,int i)

另一种方式

不使用开关(i)
了解哪个obj需要更改文本

====更新=====

这是线程中的代码

SetText("This text for the first label",1);
SetText("This text for the second label",2);

为此,我使用了一些扩展方法

public static class ControlExtensions
{
    public static void SafeInvoke(this Control control, Action action) 
    {
        if(control.InvokeRequired) 
        {
            control.BeginInvoke(action);
        }
        else 
        {
            action();
        }
    }
}
然后使用它,像这样的东西

public void TreeCompleted(bool completed)
{
    this.SafeInvoke(() =>
    {
        if(completed) 
        {
            DiagnosisTree = treeBranchControl1.GetDiagnosisTree(null);

            pctLoader.Visible = false;
            btnSelectDiagnosis.Visible = false;
            lblDiagnosis.Visible = true;
            treeBranchControl1.Visible = true;
        }
        else 
        {
            DiagnosisId = 0;
            DiagnosisTree = null;
        }
    });   
}
这是运行代码的窗体或控件

在您的情况下,您可以这样做:

public void SetText(string text, int i)
{
    this.SafeInvoke(() =>
    {
        switch (i)
        {
            case 1:
                this.label1.Text = text;
                break;
            case 2:
                this.label2.Text = text;
                break;
        }
    });   
}

为此,我使用了一些扩展方法

public static class ControlExtensions
{
    public static void SafeInvoke(this Control control, Action action) 
    {
        if(control.InvokeRequired) 
        {
            control.BeginInvoke(action);
        }
        else 
        {
            action();
        }
    }
}
然后使用它,像这样的东西

public void TreeCompleted(bool completed)
{
    this.SafeInvoke(() =>
    {
        if(completed) 
        {
            DiagnosisTree = treeBranchControl1.GetDiagnosisTree(null);

            pctLoader.Visible = false;
            btnSelectDiagnosis.Visible = false;
            lblDiagnosis.Visible = true;
            treeBranchControl1.Visible = true;
        }
        else 
        {
            DiagnosisId = 0;
            DiagnosisTree = null;
        }
    });   
}
这是运行代码的窗体或控件

在您的情况下,您可以这样做:

public void SetText(string text, int i)
{
    this.SafeInvoke(() =>
    {
        switch (i)
        {
            case 1:
                this.label1.Text = text;
                break;
            case 2:
                this.label2.Text = text;
                break;
        }
    });   
}

哪个部分太长,是
开关
语句还是
调用
部分?你应该考虑传递一个对实际标签的引用。我不喜欢用Obji来知道的int i被称为哪个部分太长,<代码>开关<代码>语句或<代码>调用< /代码>部分?你应该考虑通过一个对实际标签的引用。我不喜欢用Obji知道的INT变量,这是一个非常好的解决方案。1我对此不清楚。你能做一个设置文本的示例代码吗?哦,我明白了。但这不是我的意思。我厌倦了整数变量。有没有办法不输入i,我听不懂。INTI@用户1614028是的,这里也一样。这是您的代码,只有您知道int在这里表示什么。这是一个非常好的解决方案+1我对此不清楚。你能做一个设置文本的示例代码吗?哦,我明白了。但这不是我的意思。我厌倦了整数变量。有没有办法不输入i,我听不懂。INTI@用户1614028是的,这里也一样。这是您的代码,只有您知道int在这里表示什么。