我有一个使用PageControl
和PageTabs
的WinForm项目。假设有两个PageTabs
,每个都有自己的UserControl
对象。如果一个UserControl
启动了一个Thread(),该线程旨在无限循环并访问启动它的TextBox
上的UserControl
,那么该线程的进程如何访问正确的UserControl
更具体地说:
GUI.h
有两个PageTabs
,每个都有自己的UserControl
对象。
第一个选项卡有一个ReceiveButton
,它启动一个线程。该线程在循环上做了很多工作,并更新了TextBox
。
第二个标签页基本相同,但是具有自己的ReceiveButton
和TextBox
。此按钮还会启动一个线程,并应更新 this TextBox
。
我很难解决如何使每个线程访问/更新自己的TextBox
。
以下是我的UserControl
遵循的一系列代码:
//MyUserControl.h
void ContinueNormally(void);
System::Void buttonReceive_Click(System::Object^ sender, System::EventArgs^ e)
{
this->myThread = gcnew System::Threading::Thread(gcnew System::Threading::ThreadStart(this, &MyUserControl::ContinueNormally));
this->myThread->Start();
}
//GUI.h
#include "MyUserControl.h"
void BufferRecieveLoop()
{
while (true)
{
//receive from multicast
incoming.Process(buffer, bytes_read, endian); //this is the method in the other .h file
}
}
void MyUserControl::ContinueNormally()
{
//setup
BufferRecieveLoop();
//cleanup
}
//EntityStateProcessorPdu.h
#include "MyUserControl.h"
void EntityStatePduProcessor::Process(const DIS::Pdu& packet)
{
//do stuff
///Below are attempts at accessing the correct textbox, all in vain :(
//GUI_Example_Receive::Globals::gui->SetConsoleTextBoxText(sysStr);
//GUI_Example_Receive::Globals::gui->Controls->Find("myUserControl", true)[0]->Controls->Find("")
//GUI_Example_Receive::MyUserControl::SetTextBoxConsoleText(sysStr);
}
我还应该注意,这对于一个UserControl或一个PageTab来说效果很好。我正确Invoking
来获取UI线程进行更新。
答案 0 :(得分:0)
In Win32 (and pretty much any OS GUI that I know of) only allows one "thread" to access the GUI. This is normally known as the "GUI Thread".
The "GUI Thread" is the thread that runs the Win32 message pump.
Normally what you do is post your own "custom" messages that will be run in the GUI thread and you update your GUI controls from there.
I don't know the C++ GUI library you are using but it will most likely provide some sort of utility for post messages or code to be run on the GUI thread.
答案 1 :(得分:0)
在Windows窗体中,每个控件都有一个方法Invoke,它可以执行您要搜索的内容: 在创建控件的同一线程中调用委托。