473,385 Members | 2,180 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,385 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 1386
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Neil | last post by:
I have a strange situation. I have a stored procedure that is hanging upon execution, but only some machines and not others. The db is an Access 2000 MDB using ODBC linked tables and a SQL 7 back...
3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
25
by: Neil Ginsberg | last post by:
I have a strange situation with my Access 2000 database. I have code in the database which has worked fine for years, and now all of a sudden doesn't work fine on one or two of my client's...
1
by: davidw | last post by:
I encountered strange issues. I have code like this sqlReader = SqlHelper.ExecuteReader(connString, System.Data.CommandType.Text,sql); It calls Microsoft.ApplicationBlocks.Data to execute a...
0
by: unknown | last post by:
Hi, I am developing an online book store with shopping cart. My shopping cart is represented as a Xml server control and I am using an XSLT to render it at the client side. I am using an...
6
by: Hannibal111111 | last post by:
I am getting a strange situation with a .NET 1.1 web application that we have deployed. I have made an update to a code behind page in 1 file, and then made an update to another code behind page. ...
2
by: peter | last post by:
Hi, I have very strange situation but first description ;) I have: 1) project in VB.NET, in this f.e. 1 function: Public Function Login(ByVal UserName As String, ByVal UserPassword As...
5
by: Developer.Man4 | last post by:
when i try using HttpContext.Current.Server.MapPath(path) from a desktop application built using c# i get a System.NullReferenceException can anyone tell me why and what to do?? m using...
112
by: Prisoner at War | last post by:
Friends, your opinions and advice, please: I have a very simple JavaScript image-swap which works on my end but when uploaded to my host at http://buildit.sitesell.com/sunnyside.html does not...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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...

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.