Strange Situation

A

Amit

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
 
J

Jon Skeet [C# MVP]

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/threads/winforms.shtml for more
details.
 
P

Paul Henderson

Perhaps actually hide the button during processing, rather than just
disabling it...
 
R

Registered User

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.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


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
 
J

Jon Skeet [C# MVP]

<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us> said:
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 :)
 
I

Ignacio Machin \( .NET/ C# MVP \)

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.


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.
 
I

Ignacio Machin \( .NET/ C# MVP \)

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 :(
 
J

Jon Skeet [C# MVP]

<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us> said:
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...
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.
 
J

Jon Skeet [C# MVP]

<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us> said:
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 :)
 
N

Nicholas Paldino [.NET/C# MVP]

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. =)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top