getting asynchronous behavior where none is expected

  • Thread starter Thread starter vmspro
  • Start date Start date
V

vmspro

I need a sanity check and am looking for help. I'm writing a C#
Windows Forms application where when the user clicks a particular
button the event handler for that button executes a rather lengthy
piece of code (it goes off talking to various servers across the
network using TCP sockets). I was planning to eventually implement
this piece of code using a delegate so that I could run it
asynchronously and allow the user to cancel it from the main thread.

Much to my surprise when I run this app (without having implemented any
of the asynch stuff yet) I find that after I click the button and while
it's executing this long piece of code I can click the button again
and it will fire the event handler again and start a second invocation
of this long process. My past experience is that my app and the GUI
would be blocked till the first event handler finished and returned.

I'm guessing that some magic is going on such that while the first
event handler is doing some IO (TCP calls) the main thread is using the
free cycles to check the event queue and process more events but I have
never heard of .NET doing this and am thus confused.

Any enlightenment would be appreciated.

Version is .NET 1.1.

Mark
 
Hi vmspro,

I need a sanity check and am looking for help. I'm writing a C#
Windows Forms application where when the user clicks a particular
button the event handler for that button executes a rather lengthy
piece of code (it goes off talking to various servers across the
network using TCP sockets). I was planning to eventually implement
this piece of code using a delegate so that I could run it
asynchronously and allow the user to cancel it from the main thread.

Much to my surprise when I run this app (without having implemented any
of the asynch stuff yet) I find that after I click the button and while
it's executing this long piece of code I can click the button again
and it will fire the event handler again and start a second invocation
of this long process. My past experience is that my app and the GUI
would be blocked till the first event handler finished and returned.

I'm guessing that some magic is going on such that while the first
event handler is doing some IO (TCP calls) the main thread is using the
free cycles to check the event queue and process more events but I have
never heard of .NET doing this and am thus confused.

Any enlightenment would be appreciated.

Version is .NET 1.1.
Mark

Can you post the handler?

TT
 
Tom,

The code is long and quite complex so I'd rather not post it. I'm
really more interested in the general behavior of Windows Forms, i.e.
will it always block till the current event handler is finished or will
it sometimes service other user events and fire additional event
handlers. Any info on this issue is appreciated.

Mark
 
Hi

Tom,

The code is long and quite complex so I'd rather not post it. I'm
really more interested in the general behavior of Windows Forms, i.e.
will it always block till the current event handler is finished or will
it sometimes service other user events and fire additional event
handlers. Any info on this issue is appreciated.

Mark


The UI thread will block until the handler is executed, that's how it should
work.

TT.
 
I need a sanity check and am looking for help. I'm writing a C#
Windows Forms application where when the user clicks a particular
button the event handler for that button executes a rather lengthy
piece of code (it goes off talking to various servers across the
network using TCP sockets). I was planning to eventually
implement this piece of code using a delegate so that I could run
it asynchronously and allow the user to cancel it from the main
thread.

Much to my surprise when I run this app (without having implemented
any of the asynch stuff yet) I find that after I click the button and
while it's executing this long piece of code I can click the button
again and it will fire the event handler again and start a second
invocation of this long process. My past experience is that my app
and the GUI would be blocked till the first event handler finished
and returned.

I'm guessing that some magic is going on such that while the first
event handler is doing some IO (TCP calls) the main thread is using
the free cycles to check the event queue and process more events but
I have never heard of .NET doing this and am thus confused.

Any enlightenment would be appreciated.

Version is .NET 1.1.

Mark

Set a breakpoint in the button's click event. Continue if it's reached the
first time. When it's hit the 2nd time - when it actually shouldn't be
possible because the task is running - have a look at the call stack. It
should be something like this:

button_click
....
long running task
....
button_click

Analyze the call stack to find out what causes this reentrance.

Armin
 
I avoided the issue by running the long task asynchronously using a
delegate and popping up a modal dialog box to allow them to cancel it.
I'm guessing what was happening was that the TCP library I was using
(which implemented SSL and was from a company called Dart) was calling
Application.DoEvents which does allow events to be processed.

Thanks for the help.

Mark
 
Back
Top