无法在 C# 中使用 Invoke() 和 BeginInvoke() 更新 UI
Cannot update UI with Invoke() and BeginInvoke() in C#
我的主要流程:
public void quoteStartReceive()
{
Thread thdWrite = new Thread(new ThreadStart(DoParseGUIDisplay));
thdWrite.IsBackground = true;
thdWrite.Start();
}
我的线程函数:
void DoParseGUIDisplay()
{
for (int i = 0; i < 1024; i++)
{
if (myQueue.Count > 0)
{
string strOut = myQueue.Dequeue().ToString();
Tick tick = new Tick(strOut);
if (tick.m_last != "")
{
string msg = "Update Text";
if (this.textBox1.InvokeRequired)
{
this.textBox1.BeginInvoke((MethosInvoker)delegate () {this.textBox1.Text = msg; };
}
else
{
this.textBox1.Text = msg;
}
}
}
}
}
无论我尝试使用 Invoke() 还是 BeginInvoke(),我都无法更新 textBox1 中的文本。
我也尝试了另一种方式:
public delegate void UpdateTextCallback(string text);
它仍然无法帮助我更新我的 textBox1。
帮我看看我错过了什么。谢谢
在线程前添加一个参数:
Application.DoEvents();
应该放在m_last更新后。
我的主要流程:
public void quoteStartReceive()
{
Thread thdWrite = new Thread(new ThreadStart(DoParseGUIDisplay));
thdWrite.IsBackground = true;
thdWrite.Start();
}
我的线程函数:
void DoParseGUIDisplay()
{
for (int i = 0; i < 1024; i++)
{
if (myQueue.Count > 0)
{
string strOut = myQueue.Dequeue().ToString();
Tick tick = new Tick(strOut);
if (tick.m_last != "")
{
string msg = "Update Text";
if (this.textBox1.InvokeRequired)
{
this.textBox1.BeginInvoke((MethosInvoker)delegate () {this.textBox1.Text = msg; };
}
else
{
this.textBox1.Text = msg;
}
}
}
}
}
无论我尝试使用 Invoke() 还是 BeginInvoke(),我都无法更新 textBox1 中的文本。 我也尝试了另一种方式:
public delegate void UpdateTextCallback(string text);
它仍然无法帮助我更新我的 textBox1。
帮我看看我错过了什么。谢谢
在线程前添加一个参数:
Application.DoEvents();
应该放在m_last更新后。