【发布时间】:2009-05-25 09:32:05
【问题描述】:
我构建了一个测试箱来学习有关 Windows 窗体应用程序中的线程的知识。 Silverlight 和 Java 提供了 Dispatcher,这在更新时确实很有帮助 GUI 元素。
代码示例: 声明类委托
public delegate void d_SingleString(string newText);
创建线程
_thread_active = true;
Thread myThread = new Thread(delegate() { BackGroundThread(); });
myThread.Start();
线程函数
private void BackGroundThread()
{
while (_thread_active)
{
MyCounter++;
UpdateTestBox(MyCounter.ToString());
Thread.Sleep(1000);
}
}
委派文本框更新
public void UpdateTestBox(string newText)
{
if (InvokeRequired)
{
BeginInvoke(new d_SingleString(UpdateTestBox), new object[] { newText });
return;
}
tb_output.Text = newText;
}
有没有办法在 BeginInvoke 方法中声明延迟声明?!
类似
BeginInvoke(*DELEGATE DECLARATION HERE*, new object[] { newText });
非常感谢, 雷特
【问题讨论】:
标签: c# multithreading delegates