Solved

C# Win Form STA Thread is not supported

Posted on 2012-03-12
4
555 Views
Last Modified: 2012-06-22
Hi,

I had a class I used that use's multiple threads. I have since added this class to a new windows from project. The code complies however at the line below,

WaitHandle.WaitAll(doneEvents);

Open in new window


an NotSupportedException was unhandlewed is thrown. The error message is "WaitAll for multiple handles on a STA thread is not supported".

After reading some webpages on google, what I understand is that a window form run in single thread so cannot use multi threading without changing the attribute to MTA. Is this correct and how do I do such a thing? Also is this the same for WPF forms as I'm looking to learn about them very soon.

Thanks,
M
0
Comment
Question by:mcs26
  • 2
4 Comments
 
LVL 8

Assisted Solution

by:cubaman_24
cubaman_24 earned 250 total points
ID: 37710422
Hello:
You can set the threaded apartment in Main method as an attribute. See this:
http://msdn.microsoft.com/en-us/library/system.stathreadattribute%28VS.71%29.aspx
And this:
http://msdn.microsoft.com/en-us/library/system.mtathreadattribute%28VS.71%29.aspx

Best regards.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 37710428
Couldn't you just iterate over them and call WaitOne() against each one?
            foreach (WaitHandle doneEvent in doneEvents)
            {
                doneEvent.WaitOne();   
            }

Open in new window

0
 

Author Comment

by:mcs26
ID: 37710444
Hi cubaman,

Thanks for the reply. Sorry those link's don't make a huge amount of sense to me. Am I able to change the attribute of a windows form & WPF to MTA from STA and make use of multithreading?

Cheers
M
0
 

Author Comment

by:mcs26
ID: 37710452
Hi idle_mind,

Thanks for the reply. I think I have seen this as a viable soultion but wanted to know if I could change the attribute of either a windows form or WPF form to enable me to use to  WaitHandle.WaitAll method.

Cheers
M
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question