如何在其回调方法中停止System.Threading.Timer
。我引用了MSDN
,但找不到任何有用的东西。请帮忙。如何在回调方法中停止System.Threading.Timer
回答
您可以直接致电myTimer.Change(Timeout.Infinite, Timeout.Infinite)
。
从技术上讲,只有第一个参数(dueTime
)需要指定为Timeout.Infinite
以使计时器停止。
欲了解更多信息,请参阅Timer.Change Method。
最好将它放置,因为它能更好地捕捉语义。它可能也会更快。 – usr 2012-08-13 16:47:05
这取决于用户是否永久停止计时器,或者如果他们暂停一段时间并可能在稍后重新启动计时器。如果使用'.Dispose()'来简单地暂停计时器,那么如果要重新启动计时器,或者只是更改dueTime或句点,就会遇到问题。在这些情况下,使用'timer.Change()'更安全(更简单)。 – 2012-08-13 17:19:33
试试这个:
如果你愿意,你可以让计时器继续射击回调方法,包括以下
private void CreatorLoop(object state)
{
if (Monitor.TryEnter(lockObject)
{
try
{
// Work here
}
finally
{
Monitor.Exit(lockObject);
}
}
}
退房这个环节太代码:
首先,回调方法必须具有定时器实例的范围。
那么简单的咒语
timerInstance.Change(Timeout.Infinite , Timeout.Infinite) ;
将关闭计时器。有可能定时器可能会在更改后再次调用回调方法,我相信这取决于它所处的状态。
我发现改变(Timeout.Infinite,Timeout.Infinite)的方式很麻烦, t相当可靠,并切换到AutoReset = false的System.Timers.Timer。
Timer的问题在于它可能会在处理其所有者类后调用。以下实现通过使用Timer初始值设定项的状态对象为我工作。消耗之前,堆不会移除该对象。这是我优雅地清理计时器回调的唯一方法。
using System;
using System.Threading;
namespace TimerDispose
{
/// <summary>
/// A timer-containing class that can be disposed safely by allowing the timer
/// callback that it must exit/cancel its processes
/// </summary>
class TimerOwner : IDisposable
{
const int dueTime = 5 * 100; //halve a second
const int timerPeriod = 1 * 1000; //Repeat timer every one second (make it Timeout.Inifinite if no repeating required)
private TimerCanceller timerCanceller = new TimerCanceller();
private Timer timer;
public TimerOwner()
{
timerInit(dueTime);
}
byte[] dummy = new byte[100000];
/// <summary>
///
/// </summary>
/// <param name="dueTime">Pass dueTime for the first time, then TimerPeriod will be passed automatically</param>
private void timerInit(int dueTime)
{
timer = new Timer(timerCallback,
timerCanceller, //this is the trick, it will be kept in the heap until it is consumed by the callback
dueTime,
Timeout.Infinite
);
}
private void timerCallback(object state)
{
try
{
//First exit if the timer was stoped before calling callback. This info is saved in state
var canceller = (TimerCanceller)state;
if (canceller.Cancelled)
{
return; //
}
//Your logic goes here. Please take care ! the callback might have already been called before stoping the timer
//and we might be already here after intending of stoping the timer. In most cases it is fine but try not to consume
//an object of this class because it might be already disposed. If you have to do that, hopefully it will be catched by
//the ObjectDisposedException below
dummy[1] = 50; //just messing up with the object after it might be disposed/nulled
//Yes, we need to check again. Read above note
if (canceller.Cancelled)
{
//Dispose any resource that might have been initialized above
return; //
}
if (timerPeriod != Timeout.Infinite)
{
timerInit(timerPeriod);
}
}
catch (ObjectDisposedException ex)
{
Console.WriteLine("A disposed object accessed");
}
catch (NullReferenceException ex)
{
Console.WriteLine("A nulled object accessed");
}
catch (Exception ex)
{
}
}
public void releaseTimer()
{
timerCanceller.Cancelled = true;
timer.Change(Timeout.Infinite, Timeout.Infinite);
timer.Dispose();
}
public void Dispose()
{
releaseTimer();
dummy = null; //for testing
GC.SuppressFinalize(this);
}
}
class TimerCanceller
{
public bool Cancelled = false;
}
/// <summary>
/// Testing the implementation
/// </summary>
class Program
{
static void Main(string[] args)
{
var list = new System.Collections.Generic.List<TimerOwner>();
Console.WriteLine("Started initializing");
for (int i = 0; i < 500000; i++)
{
list.Add(new TimerOwner());
}
Console.WriteLine("Started releasing");
foreach (var item in list)
{
item.Dispose();
}
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
}
}
- 1. 如何停止System.Threading.Timer
- 2. 我可以停止System.Threading.Timer
- 3. System.Threading.Timer将在几次后停止
- 4. 在其回调方法中停止计时器
- 5. Flex - 如何中止/停止RemoteObject方法调用?
- 6. Java方法停止调用
- 7. 如何在android中按回按钮后停止finish()方法?
- 8. 如何强制停止方法exection从赎回<Object>调用方法
- 9. 如何停止在Mockito的另一个类中调用方法?
- 10. System.Threading.Timer内的方法
- 11. 如何停止AsyncTask?为什么在调用完成方法时不停止?
- 12. ASPxGridView停止回调
- 13. Dropbox Sync API停止下载文件和调用回调方法
- 14. 回调中的停止方法和循环
- 15. 如何停止Jquery中的动画效果(停止方法)
- 16. System.Threading.Timer回调没有被击中
- 17. System.Threading.Timer回调中的关键部分
- 18. 如何停止jquery回调函数?
- 19. 如何停止Browserfield requestContent()方法,当返回到mainScreen在黑莓
- 20. 如何在onPause方法被调用时停止服务?
- 21. 如何停止正在调用的方法的一部分?
- 22. asp.net mvc - 如何停止正在调用的操作方法
- 23. 如何停止正在调用自定义ViewPager的onCreateView方法
- 24. System.Threading.Timer回调永远不会被调用
- 25. 如何停止从java中的另一种方法调用方法?
- 26. 如何在python中回调方法?
- 27. 如何在TestNG中停止回滚?
- 28. 如何停止在Python中返回零?
- 29. 如何在MDB中停止回滚?
- 30. 如果方法返回true,代码如何停止?
也http://stackoverflow.com/questions/6379541/reliably-stop-system-threading-timer – 2012-08-13 16:37:03
是的,我已经检查了它 – Proceso 2012-08-13 18:12:29