Windows Develop Bookmark and Share   
 index > Windows Forms General > STA and threadpool problem
 

STA and threadpool problem

Hello,

I have a Windows Form project which i tried to merge with a console project that uses Thread pool.

After i did that i got a messege saying:

"WaitAll for multiple handles on a STA thread is not supported."

OMG someone please help me! this is an emergency beucase i must use the ThreadPool.

anyone has an idea?

qt1h00  Friday, November 17, 2006 5:46 PM
There would be two reasons the thread that calls WaitHandle.WaitAll() would run in a STA apartment. One is that you're calling it on the GUI thread. That's a no-no, you *have* to pump messages to keep an STA apartment and the GUI alive. Another is that you've initialized a background thread with calling SetApartmentState. The only reason you would have to do that is to allow the thread to access the ActiveX component(s). There's an alternative for that, marshal the call to the GUI thread yourself. Easy to do with WinForm's Control.Invoke(), tricker to do with the AsynchOperation class.

Last but not least, you could consider dumping the COM GUI for a WinForms GUI and be forever relieved from the miseries and mysteries of COM apartment threading...
nobugz  Friday, November 17, 2006 8:33 PM

Can you provide any more information? How did you merge a WinForm and console app? What COM objects are you using? What is the logic like in the TP code?

The error sounds like you are trying to access a COM object that is running in an STA and you block. In this case a deadlock situation would occur if no one other than the COM object releases the handles being waited on.

Michael Taylor - 11/17/06

TaylorMichaelL  Friday, November 17, 2006 6:15 PM

We worked on a project that uses HTTP requests with threading (every thread has an HTTP request in it).

One of use built the GUI for that and the other one build the Threading project (lets call it the engine)

When i combined the 2 which means that lets say we press RUN button on the GUI and starts to run the threading which uses ThreadPool.

after this problem would be solvedi want to be able to run the threading and continue to use the GUI.

qt1h00  Friday, November 17, 2006 7:16 PM
Using the TP from a UI is not an issue. What are you doing within the threads that might cause them to stop running? If you are processing HTTP requests are these continual connections to a remote server or simply one off requests (like web requests)? If you create an HTTP connection and maintain it for a while then you should not use the TP as it is a scarce resource. Instead you should create your own threads. Of course you can't interact with the UI from any other thread (TP or otherwise). You should also set the threads to be background threads if you don't use the TP. Can you provide some of the code that you run in the threads and any code that might interact with them on the UI side?

Michael Taylor - 11/17/06
TaylorMichaelL  Friday, November 17, 2006 8:14 PM
There would be two reasons the thread that calls WaitHandle.WaitAll() would run in a STA apartment. One is that you're calling it on the GUI thread. That's a no-no, you *have* to pump messages to keep an STA apartment and the GUI alive. Another is that you've initialized a background thread with calling SetApartmentState. The only reason you would have to do that is to allow the thread to access the ActiveX component(s). There's an alternative for that, marshal the call to the GUI thread yourself. Easy to do with WinForm's Control.Invoke(), tricker to do with the AsynchOperation class.

Last but not least, you could consider dumping the COM GUI for a WinForms GUI and be forever relieved from the miseries and mysteries of COM apartment threading...
nobugz  Friday, November 17, 2006 8:33 PM

Thanks!

you helped me solve the problem. in the GUI i opened another thread and ran the threadpool in the thread. the GUI is in another one.

but now i have another problem and i guess it's beucase i opened this new thread.

the WaitAll method never reports that all threads are asleep (meaning they finished working), i think it's becuase of the new thread that all the thread pool is inside of it.

? Please ? :)

qt1h00  Friday, November 17, 2006 9:03 PM

sorry, i solved the problem but i have another one related.

everytime the project starts becuasei merged the console application project and the windows forms project,

i get 2 screens opens. the console and the win form.

anyone has an idea how to disable the console in the background?

I dont send output to the console (at leastnot when the application is starting)

qt1h00  Saturday, November 18, 2006 7:43 AM
Your question has nothing to do with the original thread topic, you need to start a new thread.
nobugz  Saturday, November 18, 2006 7:55 AM

Be sure that in the output type for the project you set it to Windows Application rather than console.

Michael Taylor - 11/20/06

TaylorMichaelL  Monday, November 20, 2006 2:02 PM

You can use google to search for other answers

Custom Search

More Threads

• Transparent button on gradient background?
• Weird - Applying visual styles!
• WebBrowser control = wait for page load to complete in an Assembly
• Vista Aero small preview window in c#?
• how do configure computer to boot from cd-rom drive?
• Unicode chars in a control HelpString property.
• C# threading issues. Trouble calling Invoke method
• code
• Trouble with Mouse Events
• Startup Performance