Reputation: 197
Here's the scenario:
Platform: Windows
IDE: Microsoft Visual Studio 2008
Language: C#
.NET framework: 3.5
My application contains 2 buttons
- "Load Data" and "Stop Loading Data" and a multi-line textbox
. Upon clicking "Load Data" button
some data starts getting loaded in the textbox
. To prevent the user clicking on the "Load Data" button
multiple times, I have disabled that button
once it is clicked. When the entire data gets loaded in the textbox
then the "Load Data" button
gets activated again. On the other hand on clicking "Stop Loading Data" button
the loading of data is stopped (if user wishes to stop it before loading the entire content).
As stated earlier, to prevent the user clicking on the "Load Data" button
multiple times, I have disabled that button
with the intention that user can only click on "Stop Loading Data" button
or else wait for the entire data to be loaded in the textbox
. I implemented this. At first glance it seemed to work well. But while testing I found that even though the "Load Data" button
is disabled, if the user clicks on that button
, although nothing happens at that instant but as soon as the entire data gets loaded and the button
becomes enabled again, that click made during the disabled state is found to be executed. As if the program was recording the keystrokes and mouse clicks and waiting for the button to become active again. But there are no such keystrokes or mouse-clicks recording facility in my program. What is causing such an activity? How can I prevent such behavior?
Thanks.
Upvotes: 0
Views: 1207
Reputation: 62248
Well, another silution again:
On begin load simply hide a load
button and in place of it (say) show a progress bar. On finish of loading or on stop loading
click make load
button visible again. In this case you avoid "chain clicks" management you complains about.
Or manage one button. First it is load
, on click, instead, becomes stop load
. Solution like this you would find often in mobile environment, considering the limited screen space. But I think it can be applied to desktop with great sucess too. Why not?
Upvotes: 0
Reputation: 70369
One option would be to work with a reentrancy sentinel:
You could define an int
field (initialize with 0) and update it via Interlocked.Increment
on entering the method and only proceed if it is 1. At the end just do a Interlocked.Decrement
.
To make it visible for the user you can disable the button at the beginning of execution and enable it when the execution is finished...
BTW: long-running tasks should be done async (via a separate thread for example)...
Upvotes: 3
Reputation: 16761
I bet that you can't even move the form until data is loaded, and you also can't stop loading data. The problem is that the whole form freezes until loading is done. You must move the loading part in separate thread.
Upvotes: 0
Reputation: 1518
Check out this post:
http://www.codeguru.com/forum/showthread.php?t=480279
My first thought was removing the event handler and rebinding it at the end of the click event. This thread suggests using a BackgroundWorker and making it async.
Upvotes: 0
Reputation: 21810
if you make your call a synchronous one, it will lock up the entire page until loading finishes.
Otherwise you'd be just doing the method you've already tried, i'd like to see your code for the disabled state, because something tells me you just made it appear to be disabled, and it was still a functional button
Upvotes: 0