Accessing control from a different thread
-
I have a time consuming process that is called from a Windows forms application and run asynchronously in another thread, when the user clicks on
runButton
extProcess = gcnew System::Diagnostics::Process;
extProcess->StartInfo = gcnew System::Diagnostics::ProcessStartInfo;
extProcess->StartInfo->RedirectStandardOutput = true;
extProcess->StartInfo->UseShellExecute = false; //Necessary to redirect
extProcess->StartInfo->FileName = EXTPROC_EXE;
extProcess->StartInfo->CreateNoWindow = true; //Don't open a new window for Process
extProcess->OutputDataReceived += gcnew System::Diagnostics::DataReceivedEventHandler (this, &Form1::StdoutHandler);
try
{
extProcessThread->RunWorkerAsync();The thread is launched and the standard output is read, to be shown in real time in a textBox in the main window
void extProcessThreadDoWork (System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e)
{
extProcess->Start();
extProcess->BeginOutputReadLine();
}
void Form1::StdoutHandler (Object ^ sender,
System::Diagnostics::DataReceivedEventArgs ^output)
{
//Get text
textBox->AppendText (output->Data + "\n");
}The problem comes in this last line. An exception is launched "Control accessed from a thread other than the thread it was created on" I do understand why, but I don´t know how to solve it...
-
I have a time consuming process that is called from a Windows forms application and run asynchronously in another thread, when the user clicks on
runButton
extProcess = gcnew System::Diagnostics::Process;
extProcess->StartInfo = gcnew System::Diagnostics::ProcessStartInfo;
extProcess->StartInfo->RedirectStandardOutput = true;
extProcess->StartInfo->UseShellExecute = false; //Necessary to redirect
extProcess->StartInfo->FileName = EXTPROC_EXE;
extProcess->StartInfo->CreateNoWindow = true; //Don't open a new window for Process
extProcess->OutputDataReceived += gcnew System::Diagnostics::DataReceivedEventHandler (this, &Form1::StdoutHandler);
try
{
extProcessThread->RunWorkerAsync();The thread is launched and the standard output is read, to be shown in real time in a textBox in the main window
void extProcessThreadDoWork (System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e)
{
extProcess->Start();
extProcess->BeginOutputReadLine();
}
void Form1::StdoutHandler (Object ^ sender,
System::Diagnostics::DataReceivedEventArgs ^output)
{
//Get text
textBox->AppendText (output->Data + "\n");
}The problem comes in this last line. An exception is launched "Control accessed from a thread other than the thread it was created on" I do understand why, but I don´t know how to solve it...
-
I have a time consuming process that is called from a Windows forms application and run asynchronously in another thread, when the user clicks on
runButton
extProcess = gcnew System::Diagnostics::Process;
extProcess->StartInfo = gcnew System::Diagnostics::ProcessStartInfo;
extProcess->StartInfo->RedirectStandardOutput = true;
extProcess->StartInfo->UseShellExecute = false; //Necessary to redirect
extProcess->StartInfo->FileName = EXTPROC_EXE;
extProcess->StartInfo->CreateNoWindow = true; //Don't open a new window for Process
extProcess->OutputDataReceived += gcnew System::Diagnostics::DataReceivedEventHandler (this, &Form1::StdoutHandler);
try
{
extProcessThread->RunWorkerAsync();The thread is launched and the standard output is read, to be shown in real time in a textBox in the main window
void extProcessThreadDoWork (System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e)
{
extProcess->Start();
extProcess->BeginOutputReadLine();
}
void Form1::StdoutHandler (Object ^ sender,
System::Diagnostics::DataReceivedEventArgs ^output)
{
//Get text
textBox->AppendText (output->Data + "\n");
}The problem comes in this last line. An exception is launched "Control accessed from a thread other than the thread it was created on" I do understand why, but I don´t know how to solve it...
You need to have a look at the invoke methods in the article shown by the other poster MSDN: Look at the example at the bottom of the page[^]
As barmey as a sack of badgers Dude, if I knew what I was doing in life, I'd be rich, retired, dating a supermodel and laughing at the rest of you from the sidelines.
-
I have a time consuming process that is called from a Windows forms application and run asynchronously in another thread, when the user clicks on
runButton
extProcess = gcnew System::Diagnostics::Process;
extProcess->StartInfo = gcnew System::Diagnostics::ProcessStartInfo;
extProcess->StartInfo->RedirectStandardOutput = true;
extProcess->StartInfo->UseShellExecute = false; //Necessary to redirect
extProcess->StartInfo->FileName = EXTPROC_EXE;
extProcess->StartInfo->CreateNoWindow = true; //Don't open a new window for Process
extProcess->OutputDataReceived += gcnew System::Diagnostics::DataReceivedEventHandler (this, &Form1::StdoutHandler);
try
{
extProcessThread->RunWorkerAsync();The thread is launched and the standard output is read, to be shown in real time in a textBox in the main window
void extProcessThreadDoWork (System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e)
{
extProcess->Start();
extProcess->BeginOutputReadLine();
}
void Form1::StdoutHandler (Object ^ sender,
System::Diagnostics::DataReceivedEventArgs ^output)
{
//Get text
textBox->AppendText (output->Data + "\n");
}The problem comes in this last line. An exception is launched "Control accessed from a thread other than the thread it was created on" I do understand why, but I don´t know how to solve it...
This[^] explains it, however all examples are C# or VB.NET; same principles apply to C++/CLI. Amd a BackgroundWorker's ProgressChanged and RunWorkerCompleted handlers don't have the cross-thread problem as they run on the thread that created the BGW, which normally is the main thread. That is one of the big advantages of BGW. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.