473,587 Members | 2,324 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is this multi-thread? Yes, then why lock doesn't work? No? then how C# handle threads?

I found a interesting problem in C# and couldn't explain it.

The code to reproduce it is very simple:
1. Create a windows application in C#.
2. Listen to the activated event on the default form --- Form1.cs
3. Create a method DoUpdate() which will popup two dialog boxes "Dialog box
one" and "Dialog box two".
4. Call DoUpdate() in the activated event handling.

It will look like the following:
private void Form1_Activated (object sender, System.EventArg s e)
{
this.DoUpdate() ;
}

private void DoUpdate()
{
lock(this)
{
MessageBox.Show ("Dialog box one");
MessageBox.Show ("Dialog box two");
}
}

Then, take a guess what will happen after you run the application when you
switch between windows on your machine. The result is pretty supprising for
me. Instead of showing "Dialog box one" and "Dialog box two" in turn as I
expected for a multi-thread situation, it keeps looping "Dialog box one". I
replaced this by another self defined object which doesn't solve the
problem.
If this is a single thread case(get rid of the lock in DoUpdate), the result
is the same. How can it jump from "Dialog box one" to Activated event
without finishing DoUpdate.

Our guess is C# keeping the excuting methods in a stack and somehow gives
the event handling a higher priority.
Could anyone explain this?

-Jinlin
Jul 21 '05 #1
1 1939
Hi,

Firstly merely adding a lock does not make this multithreaded, with or
without the lock this is single threaded code. With the lock DoUpdate is
merely thread safe.

This behaviour is not entirely unexpected, the Activated event is raised
when the WM_ACTIVATE with the low word of the wParam = WA_ACTIVE or
WA_CLICKACTIVE, this occurs when ever the window becomes active and in your
case that happens when the message box is dismissed, the message queue
receives a WM_ACTIVATE message and even though the primary message pump is
not running because of the modal message box, modal forms have there own
message pump that processes the message queue and it dispatches the
WM_ACTIVATE message to the form causing the Activate event to be raised.

Hope this helps

--
Chris Taylor
http://www.xanga.com/home.aspx?user=taylorza
"Jinlin" <ji*********@un isys.com> wrote in message
news:uu******** ******@TK2MSFTN GP09.phx.gbl...
I found a interesting problem in C# and couldn't explain it.

The code to reproduce it is very simple:
1. Create a windows application in C#.
2. Listen to the activated event on the default form --- Form1.cs
3. Create a method DoUpdate() which will popup two dialog boxes "Dialog box one" and "Dialog box two".
4. Call DoUpdate() in the activated event handling.

It will look like the following:
private void Form1_Activated (object sender, System.EventArg s e)
{
this.DoUpdate() ;
}

private void DoUpdate()
{
lock(this)
{
MessageBox.Show ("Dialog box one");
MessageBox.Show ("Dialog box two");
}
}

Then, take a guess what will happen after you run the application when you
switch between windows on your machine. The result is pretty supprising for me. Instead of showing "Dialog box one" and "Dialog box two" in turn as I
expected for a multi-thread situation, it keeps looping "Dialog box one". I replaced this by another self defined object which doesn't solve the
problem.
If this is a single thread case(get rid of the lock in DoUpdate), the result is the same. How can it jump from "Dialog box one" to Activated event
without finishing DoUpdate.

Our guess is C# keeping the excuting methods in a stack and somehow gives
the event handling a higher priority.
Could anyone explain this?

-Jinlin

Jul 21 '05 #2

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

Similar topics

4
4648
by: Frank Jona | last post by:
Intellisense with C# and a multi-file assembly is not working. With VB.NET it is working. Is there a fix availible? We're using VisualStudio 2003 Regards Frank
6
8156
by: cody | last post by:
What are multi file assemblies good for? What are the advantages of using multiple assemblies (A.DLL+B.DLL) vs. a single multi file assembly (A.DLL+A.NETMODULE)?
6
4875
by: Joe | last post by:
I have 2 multi-list boxes, 1 displays course categories based on a table called CATEGORIES. This table has 2 fields CATEGORY_ID, CATEGORY_NAME The other multi-list box displays courses based on a table called COURSES. This table has 2 fields CATEGORY_ID, COURSE_NAME. The CATEGORY_ID is a FK in COURSES and a PK in CATEGORIES. I want...
2
3031
by: google | last post by:
Hello everyone, I am having an issue using the "Multi Select" option in a list box in MS Access 2003. I am making a form that users can fill out to add an issue to the database. Each issue can be associated with multiple categories. I have an "Issue," "IssueCategory," and "Category" in the database (among other tables). The form has a...
5
5974
by: Shane Story | last post by:
I can seem to get the dimensions of a frame in a multiframe tiff. After selecting activeframe, the Width/Height is still really much larger than the page's actual dimensions. When I split a TIFF to several PNG files this causes a problem, becuase the resulting image is (the page to the far left and a lot of black space surrounding it and...
17
10636
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, Wide character and multi-byte character are two popular encoding schemes on Windows. And wide character is using unicode encoding scheme. But each time I feel confused when talking with another team -- codepage -- at the same time. I am more confused when I saw sometimes we need codepage parameter for wide character...
0
2312
by: Sabri.Pllana | last post by:
We apologize if you receive multiple copies of this call for papers. *********************************************************************** 2008 International Workshop on Multi-Core Computing Systems (MuCoCoS'08) Barcelona, Spain, March 4 - 7, 2008; in conjunction with CISIS'08. <http://www.par.univie.ac.at/~pllana/mucocos08>...
1
9295
by: mknoll217 | last post by:
I am recieving this error from my code: The multi-part identifier "PAR.UniqueID" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part identifier "PAR.UniqueID" could not be bound. The multi-part identifier "PAR.PAR_Status" could not be bound. The multi-part identifier "Salary.New_Salary" could not...
2
4650
by: Aussie Rules | last post by:
Hi, I have a site that Iwant to either display my text in english or french, based on the users prefernces ? I am new to webforms, but I know in winforms, this is pretty easy with a resource file. What is the best way to acheive this with webforms ?
4
7321
by: =?Utf-8?B?SGVucmlrIFNjaG1pZA==?= | last post by:
Hi, consider the attached code. Serializing the multi-dimensional array takes about 36s vs. 0.36s for the single-dimensional array. Initializing the multi-dimensional array takes about 4s vs. 0.3s for the single-dimensional array. (I know initializing is not necessary in this simple example,
0
7918
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...
0
7843
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...
1
7967
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6621
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5713
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...
0
5392
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...
0
3840
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...
1
2353
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
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.