如何:在C#中使用线程计时器 [英] How to: threading timer in C#

查看:620
本文介绍了如何:在C#中使用线程计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在处理一个包含菜单的表单。我在菜单中有一个特定项目,一旦点击某个进程(进程A)正在完成大约需要60秒,同时我希望如果再次单击此项目,则显示进程A正在进行中的消息。 />


我尝试过做一些OnTimedEvent但是找不到如何实现干净解决方案的方法。



任何提示,请问最好的方法是什么。



我是什么尝试过:



我尝试过使用OnTimedEvent。

I am currently working on a form that has a menu. I have one particular item in the menu that once clicked some process (Process A) is being done which takes approximately 60 seconds, in the meantime I want that if this item is clicked again a message showing that Process A is in progress.

I have tried doing some OnTimedEvent but cannot find a way on how to implement a clean solution.

Any hints please of how is the best way to do it.

What I have tried:

I have tried with an OnTimedEvent.

推荐答案

最简单的方法将使用BackgroundWorker,并处理 RunWorkerCompleted [ ^ ] event。

创建一个类级私有booltaskInProgress并在启动任务之前将其设置为true。

在事件处理程序中,将其设置为false。

然后你需要做的就是检查bool并报告消息或启动任务。
The simplest way would be to use a BackgroundWorker, and handle the RunWorkerCompleted[^] event.
Create a class level private bool "taskInProgress" and set it true just before you start the task.
In the event handler, set it to false.
Then all you have to do is check the bool and report the message or start the task.


这篇关于如何:在C#中使用线程计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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