Solved

C# Win Form STA Thread is not supported

Posted on 2012-03-12
4
556 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 86

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can I do Unit Testing with Session ? 1 41
TFS 2015 Access denied. 1 34
Disable the weekends on datepicker control 6 52
What is needed to become a DBA? 7 56
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

710 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