473,396 Members | 2,129 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,396 software developers and data experts.

Application.DoEvents eating messages?

Background:

I am helping someone debug a problem with their form that I have traced to a
strange interaction between a click event handler and Application.DoEvents().
I'm trying to talk him into using mutithreading.

Problem in its essence:

create a simple Form with 2 buttons and a label. button1 starts a
long-running counting process, button 2 stops the counting process. The
first click of the mouse after the button 1 click gets lost. The second and
subsequent clicks work fine.

What's going on?

code snippet (C# .NET 2.0)

bool cancel = false;
int count = 0;

private void button1_Click(object sender, EventArgs e)
{
cancel = false;
while (cancel == false)
{
label1.Text = count.ToString();
Application.DoEvents();
count ++;
}

}

private void button2_Click(object sender, EventArgs e)
{
cancel = true;
}

Thanks
--
Joe
Feb 28 '06 #1
2 1504
Why not using timer?

int count = 0;

private void button1_Click(object sender, System.EventArgs e)
{
timer1.Enabled = true;
}

private void button2_Click(object sender, System.EventArgs e)
{
timer1.Enabled = false;
}

private void timer1_Tick(object sender, System.EventArgs e)
{
label1.Text = count.ToString();
count ++;
}

You made a loop inside the button1_click event so the event haven't
completed. You can see whatever you click on the form, the focus is still
kept in button1. Therefore, your second click is treated as button1_click
but focus changed to your new control. Then the consequence click works
fines. I don't know whether it is bug or normal behavior. Anyway, looping
for events is not a good programming style.
"joeforbroke" wrote:
Background:

I am helping someone debug a problem with their form that I have traced to a
strange interaction between a click event handler and Application.DoEvents().
I'm trying to talk him into using mutithreading.

Problem in its essence:

create a simple Form with 2 buttons and a label. button1 starts a
long-running counting process, button 2 stops the counting process. The
first click of the mouse after the button 1 click gets lost. The second and
subsequent clicks work fine.

What's going on?

code snippet (C# .NET 2.0)

bool cancel = false;
int count = 0;

private void button1_Click(object sender, EventArgs e)
{
cancel = false;
while (cancel == false)
{
label1.Text = count.ToString();
Application.DoEvents();
count ++;
}

}

private void button2_Click(object sender, EventArgs e)
{
cancel = true;
}

Thanks
--
Joe

Feb 28 '06 #2
"Tedmond" wrote:
Why not using timer?


Thanks for your response.

I agree that looping for events is not good programming style.

A timer works well for this simple example. In the real program, the button
click starts a lon-running process that can't be broken up into discrete
steps the way that incrementing a variable can. I believe the right choice
in this particular case is running a worker thread.

Be that as it may, there still seems to be a bug or at least undocumented
weirdness in calling Application.DoEvents() from a Click event handler. If
the handler does not call DoEvents() the second click is delivered correctly
(after the handler finishes running). If it does call DoEvents() the second
click never arrives anywhere. Third and subsequent clicks all work correctly.

If this is normal behavior, then the Application.DoEvents() examples that
I've seen also have problems.

--
Joe


Feb 28 '06 #3

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

Similar topics

24
by: bazad | last post by:
Hi, I'd like to understand consequences of Application.DoEvents call. Does it create a new thread? Thank you
6
by: foldface | last post by:
Hi I am doing drag and drop between controls (listview and treeview) using mouse_event. What I am after is a book/resource to explain the behaviour below. If I do the various Win32 api calls...
6
by: Ollie Riches | last post by:
I understand the use of Application.DoEvents() to process all outstanding messages on the message queue in a winforms application if you have long running process on the UI thread. But can anyone...
3
by: Lumpierbritches | last post by:
I have an application my partner wrote that would allow an autoresponse to any Mapi compliant email that apparently in .Net won't, can someone assist me with fixing this? Here is the code: ...
8
by: TrtnJohn | last post by:
I have an application where I would like to block waiting on an asynchronous event and still process messages. I can implement a hard loop to block such as: Do While StillWaiting...
13
by: Amjad | last post by:
Hi, Is there an equivalent to the "Application.Doevents" method in modules or Windows services? I want to make a Windows service that calls a DLL. The DLL would have all my functions and it...
8
by: Jeremy S. | last post by:
Application.DoEvents(); Why would I ever want to do that? When is it (1) required, or (2) a "good idea" or (3) a "bad idea" Thanks.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...

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.