计时器与winforms中的线程 [英] Timer With Threading in winforms

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

问题描述





在我的winform应用程序中,我使用两个具有不同功能的计时器,但其中一个正在复制一个文件(大文件)并保存到另一个文件夹。另一个向我展示我的申请时间



但问题是文件从一个位置复制到另一个位置而不是应用程序挂起。



我得到的解决方案是我必须使用线程,其中一个计时器在一个线程中而另一个计时器在另一个线程中。



你能告诉我这是怎么发生的(通过给出任何一个例子的多个线程的片段,以便我能理解如何使用它)......





在此先感谢很多.....

Hi,

In my winform application, I use two timers both having different functionality, but one of it is copying one file (big file) and save it to another folder. Another showing me the time on my application

But the problem is when file is copy from one location to another than application become hanging.

The solution I get here is that I have to use threading in which one timer is in one thread and another timer is in another thread.

Can You Tell Me How This Happens (by giving a snippet of multiple threading of any example so that i can understand how to use it)......


Thanks In Advance A Lot.....

推荐答案

1.首先,您不需要使用混合的计时器线程,因为timer事件自动执行到一个单独的线程中。



2.也许你的问题是由你的定时器间隔(可能是几秒)匹配的时间短于复制大文件所需的时间,所以对于同一个文件,你的计时器事件将被激活多次。



3.解决方案是使用一些标志变量,如: bool isCopyingFile = false; 并在开始复制文件时在 true 上设置此变量,然后使用尝试-finally 阻止并在 finally 中设置 false 上的标志。在开始新副本以检查此标志之前,如果 true 等待当前文件复制(使用简单的 if 返回)。然后不要忘记释放用于访问和复制文件的流。
1.First you don't need to use timers mixed with threads, because the timer event is automatically executed into a separate thread.

2.Maybe your problem is created by the fact, that your timer interval (maybe some seconds) is match shorter then the time needed to copy your big file, so your timer event will be fired more then one time for the same file.

3.The solution is to use some flag variable like: bool isCopyingFile = false; and to set this variable on true when you start to copy the file, then to use a try-finally block and in finally to set back the flag on false. And before to start the new copy to check this flag and if is true to wait until the current file is copying (by using a simple if and return). Then do not forgot to release the streams used for accessing and copying the file.


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

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