(टिप्पणी के रूप में बेन से लिखित)
सिर्फ System.Windows.Forms.Timer का उपयोग करें। टाइमर को 5 सेकंड के लिए सेट करें, और टिक ईवेंट को संभालें। जब घटना आग लगती है, तो बात करें।
...और दूसरी बार दबाने के लिए अपने काम करने से पहले टाइमर (IsEnabled = false) को अक्षम करें।
टिक घटना एक और धागा कि आपका GUI संशोधित नहीं कर सकते पर क्रियान्वित किया जा सकता है, तो आप इस पकड़ कर सकते हैं:
private System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
private void StartAsyncTimedWork()
{
myTimer.Interval = 5000;
myTimer.Tick += new EventHandler(myTimer_Tick);
myTimer.Start();
}
private void myTimer_Tick(object sender, EventArgs e)
{
if (this.InvokeRequired)
{
/* Not on UI thread, reenter there... */
this.BeginInvoke(new EventHandler(myTimer_Tick), sender, e);
}
else
{
lock (myTimer)
{
/* only work when this is no reentry while we are already working */
if (this.myTimer.Enabled)
{
this.myTimer.Stop();
this.doMyDelayedWork();
this.myTimer.Start(); /* optionally restart for periodic work */
}
}
}
}
बस संपूर्णता के लिए: async/का इंतजार है, एक कुछ पर अमल में देरी कर सकते के साथ बहुत आसान (एक शॉट, मंगलाचरण दोहराने कभी नहीं):
private async Task delayedWork()
{
await Task.Delay(5000);
this.doMyDelayedWork();
}
//This could be a button click event handler or the like */
private void StartAsyncTimedWork()
{
Task ignoredAwaitableResult = this.delayedWork();
}
अधिक के लिए, देखें "async और इंतजार" MSDN में। पोस्ट जवाब के रूप में चिह्नित में
बस System.Windows.Forms.Timer' का उपयोग '। टाइमर को 5 सेकंड के लिए सेट करें, और 'टिक' ईवेंट को संभालें। जब घटना आग लगती है, तो बात करें। – Ben
हम सी # 5 में एक नई सुविधा जोड़ रहे हैं जो इस प्रकार के एसिंक्रोनस ऑपरेशन को बहुत आसान बनाता है; यदि आप एक चुपके चोटी पाने में रुचि रखते हैं, तो इस ब्लॉग पोस्ट से शुरू करें: http://blogs.msdn.com/b/csharpfaq/archive/2011/04/13/async-ctp-sp1-refresh.aspx –
वे माइक्रोसॉफ्ट जाने के लिए! –