471,350 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

Strange Situation

Hi

I have occured a very strange situation.

The scenario is as follows. I have two buttons in the form. First
button is Load button and the second one is Delete button. As the name
suggests when 'Load' button is clicked, some data is loaded on the
screen. When the Delete button is hit, this data is removed and the
user does not have access to it further. Load usually takes time and in
some extreme situations it takes 4-5 seconds to load the complete data.
User is not allowed to have Load and Delete operation being executed
simultaneously. Hence while the Load processing is going on, the Delete
button is disabled. Even though the user clicks on the disabled
(Delete) button, its handler is executed and the data is removed from
the screen.

The code in Load button click event handler looks like this:

void Load_Click()
{
DeleteButton.Enabled = false;
....
....
//Do heavy processing here
....
....
DeleteButton.Enabled = false;
}
Now consider this scenario. The user pressed Load button. It took 5
seconds to processing. In the first line, the Load button is disabled
and the button is indeed disabled visibly.

Within the 5 second time (i.e. during the Delete button is disabled),
the user clicks on the Delete button.

Now, the windows puts this Delete_Click event in the queue. After the
processing for Load_Click is done, it handles Delete_Click event. At
this time, it finds that the DeleteButton.Enabled is true (it was set
to true in the last statement of Load_Click). And hence it happily
executes the Delete_Click event handler and removes all the data from
the screen!!

Can anyone tell, how to resolve this situation? I require that the user
clicks should be ignored when the button is disabled.

Best regards
Amit Dedhia

Dec 30 '05 #1
10 1314
Amit <am********@yahoo.com> wrote:

<snip>
Can anyone tell, how to resolve this situation? I require that the user
clicks should be ignored when the button is disabled.


Sure - don't do your heavy processing within the UI thread. During that
time, the clicks aren't actually being processed, they're just being
queued up. By the time they're processed, the delete button is enabled
again.

You should do the heavy processing in another thread, which calls back
to the UI thread to re-enable the delete button afterwards.

See http://www.pobox.com/~skeet/csharp/t...winforms.shtml for more
details.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 30 '05 #2
Perhaps actually hide the button during processing, rather than just
disabling it...

Dec 30 '05 #3
On 30 Dec 2005 03:06:25 -0800, "Amit" <am********@yahoo.com> wrote:
Hi

I have occured a very strange situation.

The scenario is as follows. I have two buttons in the form. First
button is Load button and the second one is Delete button. As the name
suggests when 'Load' button is clicked, some data is loaded on the
screen. When the Delete button is hit, this data is removed and the
user does not have access to it further. Load usually takes time and in
some extreme situations it takes 4-5 seconds to load the complete data.
User is not allowed to have Load and Delete operation being executed
simultaneously. Hence while the Load processing is going on, the Delete
button is disabled. Even though the user clicks on the disabled
(Delete) button, its handler is executed and the data is removed from
the screen.

The code in Load button click event handler looks like this:

void Load_Click()
{
DeleteButton.Enabled = false;
...
...
//Do heavy processing here
...
...
DeleteButton.Enabled = false;
}
Now consider this scenario. The user pressed Load button. It took 5
seconds to processing. In the first line, the Load button is disabled
and the button is indeed disabled visibly.

Within the 5 second time (i.e. during the Delete button is disabled),
the user clicks on the Delete button.

Now, the windows puts this Delete_Click event in the queue. After the
processing for Load_Click is done, it handles Delete_Click event. At
this time, it finds that the DeleteButton.Enabled is true (it was set
to true in the last statement of Load_Click). And hence it happily
executes the Delete_Click event handler and removes all the data from
the screen!!

Can anyone tell, how to resolve this situation? I require that the user
clicks should be ignored when the button is disabled.

When the button is disabled remove the OnClick event handler, this
should be in a try block. In the finally block re-enable the control
and re-assign the event handler.

One thing to question is why the UI and processing code are
co-mingled. The two types of functionality call out for two distinct
objects.

regards
A.G.

Dec 30 '05 #4
Hi,

"Amit" <am********@yahoo.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...


Can anyone tell, how to resolve this situation? I require that the user
clicks should be ignored when the button is disabled.


Why dont you just use a WaitCursor ? in this way your form cannot receive
any click.

Also, consider a more userfriendly interface, like using a thread to load
the data this will prevent the locking of the UI

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Dec 30 '05 #5
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us>> wrote:
Can anyone tell, how to resolve this situation? I require that the user
clicks should be ignored when the button is disabled.
Why dont you just use a WaitCursor ? in this way your form cannot receive
any click.


Yes it can. Try this:

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;

public class Test
{
static int count;

[STAThread]
static void Main()
{
Form f = new Form();
f.Size = new Size (200, 200);
f.Location = new Point (100, 100);

Button b = new Button();
b.Size = new Size (190, 190);
b.Location = new Point (0, 10);
b.Text = "Click me";
b.Click += new EventHandler(ClickHandler);

f.Controls.Add(b);

Application.Run (f);
}

static void ClickHandler(object sender, EventArgs e)
{
Control c = (Control) sender;
count++;
c.Text = count.ToString();
Cursor.Current = Cursors.WaitCursor;
Thread.Sleep (2000);
}
}

Start this up, and then click rapidly times on the button, several
times. You'll see the event getting fired multiple times, even though
most of the clicks are performed when the cursor is a wait cursor.
Also, consider a more userfriendly interface, like using a thread to load
the data this will prevent the locking of the UI


That's the right answer :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 30 '05 #6
Hi,

Start this up, and then click rapidly times on the button, several
times. You'll see the event getting fired multiple times, even though
most of the clicks are performed when the cursor is a wait cursor.


Interesting, I had never tried that

I was about to post it when I though that the above problem will happens no
matter what. if you are quick enough you can send a couple of events to the
queue before the handler is even invoked.
Also, consider a more userfriendly interface, like using a thread to load
the data this will prevent the locking of the UI


That's the right answer :)


More like part of it, still the UI needs to reflect the fact that an action
is being performed in the background at least to disable/enable the controls
accordingly.
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Dec 30 '05 #7
Hey Jon,

If you are in UK it should be night already , why aren;t you drinking a
pint?

It's just 2PM here and I have one more hour to leave :(


Dec 30 '05 #8
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us>> wrote:
Start this up, and then click rapidly times on the button, several
times. You'll see the event getting fired multiple times, even though
most of the clicks are performed when the cursor is a wait cursor.


Interesting, I had never tried that

I was about to post it when I though that the above problem will happens no
matter what. if you are quick enough you can send a couple of events to the
queue before the handler is even invoked.


Yes, that's true. Possibly the easiest way to solve that problem is to
check whether the "sender" of the event is enabled, and to ignore it if
it's not. I *think* that would work...
Also, consider a more userfriendly interface, like using a thread to load
the data this will prevent the locking of the UI


That's the right answer :)


More like part of it, still the UI needs to reflect the fact that an action
is being performed in the background at least to disable/enable the controls
accordingly.


Oh certainly.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 30 '05 #9
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us>> wrote:
If you are in UK it should be night already , why aren;t you drinking a
pint?
I'm still on Christmas holiday - back to work on Tuesday.
It's just 2PM here and I have one more hour to leave :(


LOL. These days I post more from home than from work anyway - hence my
rather higher number of posts this month than normal. (I don't know
what Nick's excuse is :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 30 '05 #10
Don't let Jon tell you that he doesn't go out for a pint. I know first
hand that he can handle it.

As for my excuse, I like to let the computer screen warm me on those
cold, lonely nights. =)
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us>> wrote:
If you are in UK it should be night already , why aren;t you drinking a
pint?


I'm still on Christmas holiday - back to work on Tuesday.
It's just 2PM here and I have one more hour to leave :(


LOL. These days I post more from home than from work anyway - hence my
rather higher number of posts this month than normal. (I don't know
what Nick's excuse is :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Dec 31 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Neil | last post: by
3 posts views Thread by Bruno van Dooren | last post: by
25 posts views Thread by Neil Ginsberg | last post: by
reply views Thread by unknown | last post: by
6 posts views Thread by Hannibal111111 | last post: by
5 posts views Thread by Developer.Man4 | last post: by
112 posts views Thread by Prisoner at War | last post: by

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.