如何在其回調方法中停止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