I have encountered a problem when using the backgroundworker. What I do is that I will have the oppurtunity to press the cancelbutton to stop the backgroundworker.
The code that I am using below works without any problem as expected. The thing that happens is that an error message occurs before the form2 is opened and the message is this.
-------------------------------------------
'DragDrop registration did not succeed'
System.InvalidOperationException: DragDrop registration did not succeed. ---> System.Threading.ThreadStateException:
Current thread must be set to single thread apartment (STA) mode before OLE calls can be made.
Ensure that your Main function has STAThreadAttribute marked on it.
-------------------------------------------
I have searched all over google after this message and found that this code can be used to put something in a separate thread.
Thread::CurrentThread->SetApartmentState(ApartmentState:: STA);
The problem is that I have put this code in all Load events, drag_drop events, exactly before opening form2. Before all timeconsuming code is exectuting with no result.
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
backgroundWorker1->RunWorkerAsync();
}
private: System::Void ButtonCanc_Click(System::Object^ /*sender*/, System::EventArgs^ /*e*/)
{
this->backgroundWorker1->CancelAsync();
cancelpress = true;
}
private: System::Void backgroundWorker1_DoWork(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e)
{
//TimeConsuming Work will execute as long as cancelpress is false
Form2 ^form2 = gcnew Form2;
form2->Show();
}