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();
}

I have found something about the problem that I receives in the above post, i think that this is the solution or a part of the solution but my problem is that i dont understand what this code is doing or where i will put all the code.
I have set AllowDrop to false.
As what is defined as function A, B and Formfunction in this example and as a sentence say
"Put all the code ( for which u r getting this exception) in one function say function A".

What code is that, I dont understand what functions "is" in this case ?

1) include the following
#include System::Net;
#include System::Threading;

2) add the [STAThread] Attribute just before ur main function and also remember to #include System::Net; in ur main program,

[STAThread]
static void Main()

3) The steps are as follows for threading,

1. Put all the code ( for which u r getting this exception) in one function say function A. In the function B in which u are calling ur code,do the following,
function B
{
Thread t = gcnew Thread(gcnew ThreadStart(A));
t.SetApartmentState(ApartmentState->STA);
t.Start();

}
function A
{
the code of Windows form..or whatever which is causing the error
}


Explanation:
It turns out that WF when executing all of its pretty pictures creates a thread to run each of those pictures with. These threads are in MTA mode (multi threaded apartments) and thus, in .Net 2.0 cannot be used for forms with drag and drop. so, any code which has AllowDrop Property true will give this error. With this came the following solution. All of the code used to create the form was placed in a single static function. The parent function, when it desires to call a new form ( windows form) , creates a thread, in STA mode, to execute this function. Thus the following code goes in the parent function
Thread t = gcnew Thread(gcnew ThreadStart(FormFunction));
t.SetApartmentState(ApartmentState->STA);
t.Start();

Now I have tried to put the formopening in another void and are calling this when opening
the form but with the same error. 'DragDrop registration did not succeed'

button2_Click(this, EventArgs::Empty);
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) 
{
	  Form2 ^form2 = gcnew Form2;
	  form2->Show();
}

Any idéas of what I should focus on to do here.
I think I am pulling my hair by now :P Thanks a Lot!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.