473,387 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

winform synchronization 'onPaint', 'onKeyUp', and 'lock'

Hello, in the application I am developing, I am having trouble to
synchronize event triggered actions using 'lock(ob){...};' technique.
Here is a outline of my code:

class C{
int x = 0;
public void incre(){ if(x < 20) x++; }
}

class Form1: WinForm{

...//all the usual initialization setup code.
C c;
Object ob = new Object();
public void onPaint(..., ...) // event handler of a paint event
{
lock(ob){
c.incre();
}
}
pubilc void onKeyUp(..., ...) // event handler of a keyboard event
{
if(e.Key == Keys.G) // increase C.x when user presses 'g'
{
lock(ob){
c.incre();
}
}
}
}

Now, theoretically, because c.incre() call is protected by lock
synchronization, c.x should always <= 20; But to my surprise, this code
produces different result, c.x = 20 randomly. It seems my lock
synchronization did not work and two threads (paint thread, keyboard
handler thread) can execute c.x simultaneously...

If you know what I did wrong here, please advise. Thanks,

Jun 2 '06 #1
5 2665
But does it ever reach 21, the if will increment x for all values below 20
i.e. 0 to 19, when 19 is incremented it becomes 20, the test will then fail
and x should remain 20.

"fe*****@gmail.com" wrote:
Hello, in the application I am developing, I am having trouble to
synchronize event triggered actions using 'lock(ob){...};' technique.
Here is a outline of my code:

class C{
int x = 0;
public void incre(){ if(x < 20) x++; }
}

class Form1: WinForm{

...//all the usual initialization setup code.
C c;
Object ob = new Object();
public void onPaint(..., ...) // event handler of a paint event
{
lock(ob){
c.incre();
}
}
pubilc void onKeyUp(..., ...) // event handler of a keyboard event
{
if(e.Key == Keys.G) // increase C.x when user presses 'g'
{
lock(ob){
c.incre();
}
}
}
}

Now, theoretically, because c.incre() call is protected by lock
synchronization, c.x should always <= 20; But to my surprise, this code
produces different result, c.x = 20 randomly. It seems my lock
synchronization did not work and two threads (paint thread, keyboard
handler thread) can execute c.x simultaneously...

If you know what I did wrong here, please advise. Thanks,

Jun 2 '06 #2
On 2 Jun 2006 12:18:12 -0700, fe*****@gmail.com wrote:
class C{
int x = 0;
public void incre(){ if(x < 20) x++; }
} [...] Now, theoretically, because c.incre() call is protected by lock
synchronization, c.x should always <= 20; But to my surprise, this code
produces different result, c.x = 20 randomly. It seems my lock
synchronization did not work and two threads (paint thread, keyboard
handler thread) can execute c.x simultaneously...

If you know what I did wrong here, please advise. Thanks,


There is no "paint thread" or "keyboard thread". Under Windows, all the
events raised by UI controls are executed in the UI thread (actually, the
OnXXX methods are not events but you get my point). So in your exemple, you
only have one thread in action, the UI thread, which means that you do not
need to synchronize access to your object c. As for the results you're
getting, they seem perfectly normal to me. c.x starts at 0 then increases 1
by 1 at every paint event and whenever the user presses the g key until it
reaches 20 at which point it remains there. What do you think is wrong with
that?
Jun 3 '06 #3

Mehdi wrote:
On 2 Jun 2006 12:18:12 -0700, fe*****@gmail.com wrote:
class C{
int x = 0;
public void incre(){ if(x < 20) x++; }
}

[...]
Now, theoretically, because c.incre() call is protected by lock
synchronization, c.x should always <= 20; But to my surprise, this code
produces different result, c.x = 20 randomly. It seems my lock
synchronization did not work and two threads (paint thread, keyboard
handler thread) can execute c.x simultaneously...

If you know what I did wrong here, please advise. Thanks,


There is no "paint thread" or "keyboard thread". Under Windows, all the
events raised by UI controls are executed in the UI thread (actually, the
OnXXX methods are not events but you get my point). So in your exemple, you
only have one thread in action, the UI thread, which means that you do not
need to synchronize access to your object c. As for the results you're
getting, they seem perfectly normal to me. c.x starts at 0 then increases 1
by 1 at every paint event and whenever the user presses the g key until it
reaches 20 at which point it remains there. What do you think is wrong with
that?


Thank you, actually c.x gets over 21 sometimes (my code is logically
more complicated) so it may be due to other bug but not
synchronization. Thanks for the clarification on the thread model.
However, when in a debugger, I see 3 threads when I run winForm app,
one is GC thread, one is main thread, I do't know what's the 3rd one?
Is it UI thread? THen what's a main thread? Thanks,

Fei

Jun 5 '06 #4

fe*****@gmail.com wrote:
Thank you, actually c.x gets over 21 sometimes (my code is logically
more complicated) so it may be due to other bug but not
synchronization. Thanks for the clarification on the thread model.
However, when in a debugger, I see 3 threads when I run winForm app,
one is GC thread, one is main thread, I do't know what's the 3rd one?
Is it UI thread? THen what's a main thread? Thanks,

Fei


Just going on memory alone (sometimes that's dangerous) I belive there
are two threads related to the GC. The 3rd may be the finalizer
thread. In the case of a windows forms application the main thread is
the same as the UI thread. The UI message pump gets started via
Application.Run in the Main method.

Brian

Jun 5 '06 #5

Brian Gideon wrote:
fe*****@gmail.com wrote:
Thank you, actually c.x gets over 21 sometimes (my code is logically
more complicated) so it may be due to other bug but not
synchronization. Thanks for the clarification on the thread model.
However, when in a debugger, I see 3 threads when I run winForm app,
one is GC thread, one is main thread, I do't know what's the 3rd one?
Is it UI thread? THen what's a main thread? Thanks,

Fei


Just going on memory alone (sometimes that's dangerous) I belive there
are two threads related to the GC. The 3rd may be the finalizer
thread. In the case of a windows forms application the main thread is
the same as the UI thread. The UI message pump gets started via
Application.Run in the Main method.

Brian


Thank you all for the thoughtful input.

Jun 5 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ivan | last post by:
Hi there My work on threads continues with more or less success. Here is what I'm trying to do: Class JobAgent is incharged for some tasks and when it's called it starts thread which performs...
5
by: Cyrus | last post by:
I have a question regarding synchronization across multiple threads for a Hashtable. Currently I have a Threadpool that is creating worker threads based on requests to read/write to a hashtable....
5
by: Bill Davidson | last post by:
Hello All: I've got a question about synchronization requiremements in a C# worker thread procedure that, among other things, sinks events from outside sources. I realize the worker thread will...
4
by: scott | last post by:
hi all, Thx to any one that can offer me help, it will be much appreciated. iv got a multithreaded program and need to use thread synchronization. The synchronization does not have to...
5
by: Tony Gravagno | last post by:
I have a class that instantiates two Timer objects that fire at different intervals. My class can be instantiated within a Windows Form or from a Windows Service. Actions performed by one of the...
6
by: Chris Ashurst | last post by:
Hi, I'm coming in from a despised Java background, and I'm having some trouble wrapping my head around sharing an object between multiple instances of a single class (in simpler terms, I would say...
0
by: jimdefruscio | last post by:
Hi, I have developed a ASP.NET 2.0 web application and am having a data access synchronization issue. I would appreciate anybody's experience. My application is a multi-user application where...
7
by: atlaste | last post by:
Hi, I have two different things I'd like to discuss here. Both are about cross-process synchronization of shared resources. The resources that are shared are: (1) an object writing to a file and...
3
by: CKKwan | last post by:
Dear All, Can synchronize a class, any function is called and the entire class is locked. Can synchronize a method What if I need to Lock a class only when specific method is call?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.