c# - 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# - 检查部分未知路径中的文件
- laravel - 有没有其他方法可以让 Laravel 重新加载环境变量?
- webpack - 在 babel-loader 中获取 webpack 块名称
- android - “打开”新活动后黑屏
- python - 将python OrderedDict隐蔽到数据框
- bazel - 需要帮助使用 Bazel + Linaro 交叉编译 nsync
- sql - Amazon Redshift 中的固定长度字符串比较
- asp.net - ASP.NET Core 等效于 OwinContext.Environment?
- ionic-framework - Jhipster Ionic 实体创建:没有这样的文件 _entity.html.ejs
- reactjs - 即使组件仍然被渲染,React props 也会被评估