首页 > 解决方案 > Control.Invoke 不调用

问题描述

下面的代码来自我目前正在处理的开发证明:

using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

...

QueueProcessor = new Task(() =>
        {
            while (processorrunning)
            {
                if (processorrunning && dq.Count != 0)
                {
                    boxid = string.Empty;

                    if (dq.TryDequeue(out boxid))
                    {
                        if (textBox1.InvokeRequired)
                        {
                            textBox1.Invoke(new MethodInvoker(() =>
                            {
                                if (textBox1.Text == string.Empty)
                                    textBox1.Text = boxid;
                                else
                                    textBox1.Text += Environment.NewLine + boxid;
                                textBox1.Refresh();
                            }));
                        }
                        else
                        {
                            if (textBox1.Text == string.Empty)
                                textBox1.Text = boxid;
                            else
                                textBox1.Text += Environment.NewLine + boxid;
                            textBox1.Refresh();
                        }
                    }
                    else
                    {
                        throw new Exception("Exception removing boxid from queue.");
                    }
                }
                else
                {
                    trigger.WaitOne(600000, false);
                }
            }
        });

所有的 if 条件都返回 true。有一个从 dq.TryDequeue 调用分配给 boxid 的有效 boxid 字符串。除了调用代码之外的所有内容都可以正常工作。代码在 InvokeRequired 测试中从不采用 else。

Form1.textBox1 从来没有看到任何东西。欢迎提出想法。

标签: c#task

解决方案


推荐阅读