473,857 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2688
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.c om 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.c om 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.c om 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.c om 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
1244
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 the job. Application contains one list of agents that are idle at the moment and list of busy agents. In loop it checks if there are agents in idle list and if there are some, it starts them.
5
15588
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. One function of the Hashtable is to iterate through its keys, which apparently is inherently not thread-safe. Other functions of the Hashtable include adding/modifying/deleting. To solve the synchronization issues I am doing two things: 1. Lock...
5
2603
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 be interrupted to handle an incoming event (and the flow of execution subsequently diverted to the respective event handler). My question is at what point or points could this interruption occur. Will it only occur when the worker thread is...
4
3212
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 work across multiple processes just the one. I was wondering if any one new which one used the least overhead. Im at current using mutexes but was wondering if there was something a bit
5
12265
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 event handlers may take longer than the interval for either of the timers, so it's possible for multiple events to fire "simultaneously" and for events to queue up. I'm attempting to get the timers to sync on some reference type object, or use...
6
2064
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 imagine a simple chat server that has to share a list of connected users to each instance of a connected user). Usually, I would have a synchronized list instantiated inside each instance of a client class, which would do the trick, but since...
0
905
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 each person is working from a queue of work items. They open the app and click a button which gets the next unprocessed item on the queue and locks the record for the user by setting an isLocked flag on the database record. When they are done...
7
8851
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 (2) a file. I threat them as two different problems for various reasons. The first problem involves the object writing to a file. The problem might be best explained by thinking of a single log file that's appended by different programs that...
3
6939
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
9923
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9767
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11082
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10711
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10394
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7939
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7104
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5774
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4592
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.