Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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
用Win.Forms控件实现C#中的多线程_C#_Multithreading_Winforms - Fatal编程技术网

用Win.Forms控件实现C#中的多线程

用Win.Forms控件实现C#中的多线程,c#,multithreading,winforms,C#,Multithreading,Winforms,我是C#的初学者。我在使用win.forms时遇到线程问题。我的申请被冻结了。这个代码有什么问题?我使用的是来自的microsoft示例。 这是我的密码: delegate void SetTextCallback(object text); private void WriteString(object text) { // InvokeRequired required compares the thread ID of the /

我是C#的初学者。我在使用win.forms时遇到线程问题。我的申请被冻结了。这个代码有什么问题?我使用的是来自的microsoft示例。 这是我的密码:

    delegate void SetTextCallback(object text);

    private void WriteString(object text)
    {
        // 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.textBox1.InvokeRequired)
        {
            SetTextCallback d = new SetTextCallback(WriteString);
            this.Invoke(d, new object[] { text });
        }
        else
        {
            for (int i = 0; i <= 1000; i++)
            {
                this.textBox1.Text = text.ToString();
            }
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {

        Thread th_1 = new Thread(WriteString);
        Thread th_2 = new Thread(WriteString);
        Thread th_3 = new Thread(WriteString);
        Thread th_4 = new Thread(WriteString);

        th_1.Priority = ThreadPriority.Highest; // самый высокий
        th_2.Priority = ThreadPriority.BelowNormal; // выше среднего
        th_3.Priority = ThreadPriority.Normal; // средний
        th_4.Priority = ThreadPriority.Lowest; // низкий

        th_1.Start("1");
        th_2.Start("2");
        th_3.Start("3");
        th_4.Start("4");

        th_1.Join();
        th_2.Join();
        th_3.Join();
        th_4.Join();
    }
delegate void SetTextCallback(对象文本);
私有void WriteString(对象文本)
{
//invokererequired比较
//向创建线程的线程ID调用线程。
//如果这些线程不同,则返回true。
if(this.textBox1.invokererequired)
{
SetTextCallback d=新的SetTextCallback(WriteString);
调用(d,新对象[]{text});
}
其他的
{

对于(int i=0;i存在死锁-当工作线程尝试使用blocking
Control.Invoke()
向UI发送消息时,UI线程正在等待线程完成
thread.Join()
。将线程代码中的调用替换为BeginInvoke()将消除死锁

 if (this.textBox1.InvokeRequired)
    {
        SetTextCallback d = new SetTextCallback(WriteString);
        // BeginInvoke posts message to UI thread asyncronously
        this.BeginInvoke(d, new object[] { text }); 
    }
    else
    {
        this.textBox1.Text = text.ToString();
    }

出现死锁-当工作线程试图使用阻塞
控件向UI发送消息时,UI线程正在等待线程完成
线程。Join()
。Invoke()
。用BeginInvoke()替换线程代码中的Invoke将使死锁消失

 if (this.textBox1.InvokeRequired)
    {
        SetTextCallback d = new SetTextCallback(WriteString);
        // BeginInvoke posts message to UI thread asyncronously
        this.BeginInvoke(d, new object[] { text }); 
    }
    else
    {
        this.textBox1.Text = text.ToString();
    }


它因连接调用而冻结。Thread.Join()使当前线程在另一个线程完成后等待。

它因连接调用而冻结。Thread.Join()使当前线程在另一个线程完成后等待。

没有例外,我的表单刚刚冻结您尝试调试应用程序以查看它到底挂起在哪里?很高兴知道!它到底什么时候冻结?没有例外,我的表单刚刚冻结您尝试调试应用程序以查看它到底挂起在哪里挂断?好吧,很高兴知道!它什么时候冻结,准确地说?它工作正常,但不是我预期的那样,当我在MyApp_Load中添加System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls=false;时工作正常。但我读到不推荐使用它。尝试添加System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls=false并以书面形式添加removeall并添加textBox1.Text=Text.ToString();这是我预期的结果,但我读到不建议设置CheckForIllegalCrossThreadCalls=false抱歉,我在设置TextBox时更新了我的问题并添加了运算符设置System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls=false时结果不同,当我像你告诉我的那样写作时,它正在工作,但不是我预期的结果ed,当我在MyApp_Load中添加System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls=false时,它工作正常。但我了解到不建议这样做。请尝试添加System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls=false并以书面形式删除所有并添加textBox1.Text=Text.ToString();这是我预期的结果,但我读到不建议设置CheckForIllegalCrossThreadCalls=false抱歉,我在设置TextBox时更新了我的问题并添加了运算符设置System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls=false时结果不同,当我像你告诉我的那样编写时,如果添加Sy为什么它不会冻结stem.Windows.Forms.Control.CheckForIllegalCrossThreadCalls=false;在MyApp_Load?@goodspeed中-当您禁用check时,可以消除死锁,因为InvokeRequired始终返回false。这正是alexm所说的。您可以删除所有的。Join()调用,因为您的代码中没有需要在所有线程完成其他操作后等待的逻辑。我试图理解为什么在我的App_Load?@goodspeed-当您禁用check时,可以消除死锁,因为invokererequired始终返回false。这正是alexm所说的。您可以删除所有的。Join()调用,因为您的代码中没有需要在所有线程完成其他操作后等待的逻辑。我试图理解为什么在添加System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls=false时,所有操作都正常工作