计时器与winforms中的线程 [英] Timer With Threading in 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 ontrue
when you start to copy the file, then to use atry-finally
block and infinally
to set back the flag onfalse
. And before to start the new copy to check this flag and if istrue
to wait until the current file is copying (by using a simpleif
andreturn
). Then do not forgot to release the streams used for accessing and copying the file.
这篇关于计时器与winforms中的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!