Opening another GUI thread

U

uzi

Hi



Recently I had a problem in which I had window opened that had a slider
which was updated by raising an event on that window.



The background window was still available, and the user clicked on it
causing it to perform a long time operation.



During this operation the events for the update position started
accumulating since the main GUI thread was busy, and I got an exception that
the thread pool has no enough threads to perform the operation.



This is when decided to open the window on another GUI thread.



Below is the code snippet I used for creating the new GUI thread.



My problem is that sometime i get an exception of
System.InvalidOperationException: The object is currently in use elsewhere



Does anyone have an idea why???



Thanks

Uzi


private class NewGuiThreadWrapper

{

private ApplicationContext m_Cntx;

ManualResetEvent m_Ev;

public ApplicationContext ApplicationCntx

{

get { return m_Cntx; }

}

public NewGuiThreadWrapper(ManualResetEvent ev)

{

m_Ev = ev;

new Thread(new ThreadStart(StartNewGuiThread)).Start();

}

private void StartNewGuiThread()

{



Thread.CurrentThread.ApartmentState = ApartmentState.STA;

m_Cntx = new ApplicationContext(new MyForm());


m_Ev.Set();

Application.Run(m_Cntx);

}

}
 
J

Jon Skeet [C# MVP]

uzi said:
Recently I had a problem in which I had window opened that had a slider
which was updated by raising an event on that window.

The background window was still available, and the user clicked on it
causing it to perform a long time operation.

During this operation the events for the update position started
accumulating since the main GUI thread was busy, and I got an exception that
the thread pool has no enough threads to perform the operation.

Well, while I don't know for sure what caused your actual problem, I
don't believe your solution was the best one. If you need to perform an
operation which will take a long time, move *that* away from the UI
thread. See http://www.pobox.com/~skeet/csharp/threads/winforms.shtml
for more details.

It's quite possible that the reason your other code didn't work,
however, was that you were creating the new form on the existing
thread, so that was the thread it was associated with, rather than the
new one.
 
U

uzi

i'm fully aware of the best (desired) solution, but since i'm a small part
of an already (huge) existing application, i had two choice (1) force
everyone to write correct code (not going to happen), (2) find my own
solution (which i did).

in any case, the new form i generated inside the context of the new created
thread.

if you saw my code snippet then youll notice that i only create the new form
inside the contect of the ApplicationContext object which is inside the
thread function.

Regards,
Uzi
 
J

Jon Skeet [C# MVP]

uzi said:
i'm fully aware of the best (desired) solution, but since i'm a small part
of an already (huge) existing application, i had two choice (1) force
everyone to write correct code (not going to happen), (2) find my own
solution (which i did).

Well, I suspect you're going to run into problems again and again.
Threading is one of those areas where you can't afford to paper over
cracks, IMO.
in any case, the new form i generated inside the context of the new created
thread.

if you saw my code snippet then youll notice that i only create the new form
inside the contect of the ApplicationContext object which is inside the
thread function.

Sorry, yes, I misread it first time.

Could you post a short but complete program which demonstrates the
problem? (To make it easier to reproduce.)

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top