奇怪的情况 [英] Strange Situation

查看:93
本文介绍了奇怪的情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我发生了一个非常奇怪的情况。


场景如下。表格中有两个按钮。首先

按钮是加载按钮,第二个是删除按钮。正如名称

建议单击加载按钮时,某些数据会加载到

屏幕上。点击删除按钮后,此数据将被删除,并且

用户无法进一步访问该数据。加载通常需要时间,并且在某些极端情况下加载完整数据需要4-5秒。

不允许用户执行加载和删除操作

同时。因此,当加载处理进行时,Delete

按钮被禁用。即使用户点击了禁用的

(删除)按钮,也会执行其处理程序并将数据从屏幕上移除




加载按钮单击事件处理程序中的代码如下所示:

void Load_Click()

{

DeleteButton .Enabled = false;

....

....

//在这里做重处理

....

....

DeleteButton.Enabled = false;

}

现在考虑这种情况。用户按下加载按钮。处理需要5

秒。在第一行,加载按钮被禁用

并且按钮确实被禁用了。


在5秒内(即在删除按钮期间)用户单击删除按钮时,



现在,Windows将此Delete_Click事件放入队列中。在完成Load_Click的
处理之后,它处理Delete_Click事件。这次是

,它发现DeleteButton.Enabled为true(在Load_Click的最后一个语句中将
设置为true)。因此很高兴

执行Delete_Click事件处理程序并从屏幕上删除所有数据!


任何人都可以告诉,如何解决这种情况?我要求在禁用按钮时忽略用户

点击次数。


祝你好运

Amit Dedhia

解决方案

Amit< am ******** @ yahoo.com>写道:


< snip>

任何人都可以告诉,如何解决这种情况?我要求在禁用按钮时忽略用户的点击次数。




当然 - 不要在UI线程中进行繁重的处理。在那个

时间内,点击实际上并没有得到处理,他们只是在排队等待
。当它们被处理时,删除按钮再次启用




你应该在另一个线程中执行繁重的处理,它会回调

到UI线程后重新启用删除按钮。


参见 http://www.pobox.com/~skeet/csharp/t...winforms.shtml 了解更多信息/>
详细信息。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复该组,请不要给我发邮件


也许在处理过程中实际上隐藏了按钮,而不仅仅是

禁用它.. 。


2005年12月30日03:06: 25-0800,Amit <是******** @ yahoo.com>写道:



我发生了一个非常奇怪的情况。

情景如下。表格中有两个按钮。第一个
按钮是加载按钮,第二个是删除按钮。正如名称
建议单击加载按钮时,某些数据会加载到
屏幕上。点击删除按钮后,此数据将被删除,
用户无法进一步访问该数据。负载通常需要时间,在某些极端情况下,加载完整数据需要4-5秒。
用户不允许同时执行加载和删除操作。因此,当加载处理进行时,删除
按钮被禁用。即使用户单击禁用的
(删除)按钮,也会执行其处理程序并从屏幕上删除数据。

加载按钮单击事件处理程序中的代码看起来像这样:

void Load_Click()
{
DeleteButton.Enabled = false;
...
...
/ /在这里进行繁重的处理
...
...
DeleteButton.Enabled = false;
}

现在考虑这种情况。用户按下加载按钮。处理需要5 /秒。在第一行中,加载按钮被禁用
并且按钮确实被禁用了。

在5秒钟内(即在禁用删除按钮期间),
用户单击删除按钮。

现在,Windows将此Delete_Click事件放入队列中。完成对Load_Click的处理后,它处理Delete_Click事件。在这次,它发现DeleteButton.Enabled为true(在Load_Click的最后一个语句中将其设置为true)。因此它很乐意执行Delete_Click事件处理程序并从屏幕上删除所有数据!!

任何人都可以告诉,如何解决这种情况?我要求在禁用按钮时忽略用户的点击次数。



当按钮被禁用时,移除OnClick事件处理程序,这个

应该在try块中。在finally块中重新启用控件

并重新分配事件处理程序。


有一点需要质疑的是UI和处理代码为何

混合在一起。两种类型的功能需要两个不同的
对象。


问候

AG


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

解决方案

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


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


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.


这篇关于奇怪的情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆