Problem with multithread application

G

Guest

Hi there.Im building a multithreaded application which uses forms and im
having some problems possibly due to my inexperience.
The project is being developed in C# with visual studio 2005.
I have a main thread which executes Application.Run() (so it has the message
loop for the interface) and later i create more threads that are executing
and in some situations have to do some operations in the UI. Some threads
also query the UI the creation of non-modal dialogs to show information "on
the fly". There arent cross-threaded operations in the UI, im sure of that.
All the operations have been queried using the InvokeRequired attribute and
the Invoke Method (i have used only the Invoke method instead of using
BeginInvoke too) so all the operations on the UI are done in the Thread tha
run Application.Run (the UI thread).
When i execute my application everything goes fine buty sometimes when i
execute it and push some button of one of the windows or when i close one of
that windows etc etc my application hangs (well i dont know if my application
but at least the UI). I think that the problem is in that im not considering
some issue of the using of the UI in multithreading applications, maybe it
is taking place a deadlock when im quering the UI tread some operations from
different threads ¿someone can advise me or has any idea about what can be
happening? thanks
 
G

Guest

Im building a multithreaded application which uses forms and im
having some problems possibly due to my inexperience.

I have given up on 99% of the Invoke game. I keep my gui thread and my
worker threads isolated from each other. Worker threads enqueue results
(objects) on a thread-safe queue. The gui thread dequeues them for action at
its leisure. The dequeuing takes place in a timer tick event that is
normally at a slow burn rate, say one tick every 10 seconds. The enqueuing
mechanism sets the timer interval to 1 millisecond via Invoke (the remaining
1% is here). When the queue goes empty, the gui thread increases the
interval.

It is polling (gui thread dequeues when it wants to) with a demand wakeup
(timer interval). Very low tech. The gain is no multi-thread issues,
problems, or whatever. None, and I really mean it. I have not addressed the
area of a worker querying the gui for info that requires marshalling, so I
have nothing to say about that.

You probably didn't want to hear this, and I'm sorry about that. In the
past, I have had multi-thread problems like yours, and under pressure to make
progress, I settled on the queue solution. Since this approach precludes
marshalled calls between threads, I have to think through multi-threaded
designs with some care, and when I paint myself into a corner, I have to
resist the temptation to make a marshalled call. I don't find these
limitations onerous.

I know that what I do is out of the mainstream. Also, I don't urge you to
move in one direction or another. I'm just saying my bit re multi-threading
problems in general and I'm suggesting an alternative. Good luck.
 
S

Stoitcho Goutsev \(100\)

eduwushu,

It is hard to tell where your actuall problem is. If you say that all UI
operations are done via Control.Invoke (or Control.BeginInvoke, which also
also schedules for execution in the UI thread) then you should look for a
bug in your code.

I suggest reading through the following MSDN article about ideas how to
avoid deadlocks:

http://msdn.microsoft.com/msdnmag/issues/06/04/Deadlocks/default.aspx
 
G

Guest

Let me be more explicit on the problem: i've been making some testings on my
application and i realized that the application hangs in this situation:
I have a main window created in the thread which called Application.Run so
it has the message loop right? Well, then i have another thread wich
represents an entity which has to do its own operations (worker thread
created with the thread object) .Then i ask my thread object to show a
monitor where it has to write its progress in the operation, this monitor is
a non-modal dialog. The worker thread calls a method to obtain the form
which represents that monitor: this method will do a call of the Invoke
method in the form object which represents the main form of the application
and which was created in the thread which called the Application.Run
method,follow me? : ) .this invoking is to ask the UI thread to create a
monitor form object (i have to create the monitor object in the UI Thread as
it will be a non modal dialog which i will show using its Show method so it
won't have a message queue by itself ,this is the opposite to what happens if
i want it to be a modal dialog:then i call the showdialog method and it will
have its own messagequeue and it is not necessary for it to be created in the
UI Thread)
well thn the worker thread gets its monitor form object and do an Invoke to
call its Show method to display it.
Periodically the worker thread will do an Invoking to call a methos wich
updates the fields of the monitor object.
Well everything goes fine but when the monitor is running and i start moving
the main form from one side to other of the screen then the hang of the
application comes up. I think that it could be because i created the monitor
object in such a way that it share the messagequeu of the main form of the
application(creating this object in the UI Thread).
The problem is that i havent found the way to give a non modal dialog its
own message queue independent of the message queu of teh main form.
anybodu thinks that this could be the problem???? THANKS
 

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