Event handlers are not threads?

B

Boni

Dear all,
I do need to lock a not thread-safe object from multiple access. This object
is accessed in OnPaint,MouseMove,MouseUp,MouseDown.
SyncLock seems to not work(?) because event hadlers are not
threads(?).Please give me solution advice.
Thanks in advance.
Boni
 
G

Guest

Windows messages are received and dispatched from the main UI thread so
typically you do not have multithreading problems unless your application is
calling these methods directly from a different thread. If so, you need to
be sure to use Control.Invoke and pass a delegate instead of calling the
methods directly. Control.Invoke will marshall all of your calls to the main
UI thread instead of from your worker thread.
 
B

Boni

Dear John,
unfortunately the multithreading problems do happen. I see that MouseMove
call overlapes with OnPaint call of the same control.
Sometimes mousemove is not finished where OnPaint is already started.
Is there any solution for that?
Thanks in advance,
Boni
 
G

Guest

The only way this can happen is if you, or some other logic, gives control
back to the message pump by calling Application.DoEvents. But, this still
occurs on a single thread. Each message is processed completely unless it
gives up control using DoEvents. That is why when you use DoEvents you need
to be careful because you can have re-entrancy problem. You need to add your
own state control in your application to manage this.
 

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