मैं विंडोज सेवा लिख रहा हूं जो हर कुछ मिनटों में "कुछ" संसाधित करेगा।निष्पादन अवधि से अधिक समय लेता है जब टाइमर व्यवहार?
public Service()
{
this.InitializeComponent();
this.ServiceName = Name;
this.CanPauseAndContinue = true;
this.CanShutdown = true;
this.eventLog.Source = Name;
// initialize timer
this.timer.Elapsed += this.TimerElapsed;
}
private void TimerElapsed(object sender, ElapsedEventArgs e)
{
eventLog.WriteEntry("Starting syncronization...", EventLogEntryType.Information);
if (this.processor.PrepareToRun())
{
this.processor.Run();
}
}
मुझे आश्चर्य है अगर this.processor.Run()
लंबे समय और अगले TimerElapsed
घटना ले जाएगा क्या होगा बढ़ा दी जाएगी:
यहाँ कुछ कोड है? क्या यह छोड़ देगा? क्या यह समाप्त होने के बाद एएसएपी इंतजार करेगा और चलाएगा? क्या मुझे उन परिदृश्यों और कोडों पर विचार करना चाहिए?
मैं उपयोग कर रहा हूँ System.Timers.Timer
संपादित:
private void TimerElapsed(object sender, ElapsedEventArgs e)
{
eventLog.WriteEntry("Starting syncronization...", EventLogEntryType.Information);
try
{
this.timer.Stop();
if (this.processor.PrepareToRun())
{
this.processor.Run();
}
}
catch (Exception ex)
{
LoggingAndNotifications.LogAndNotify(ex);
}
finally
{
this.timer.Start();
}
}
संपादित 2
public Service()
{
this.InitializeComponent();
this.ServiceName = Name;
this.CanPauseAndContinue = true;
this.CanShutdown = true;
this.eventLog.Source = Name;
// initialize timer
this.timer.AutoReset = false;
this.timer.Elapsed += this.TimerElapsed;
}
private void TimerElapsed(object sender, ElapsedEventArgs e)
{
eventLog.WriteEntry("Starting syncronization...", EventLogEntryType.Information);
try
{
if (this.processor.PrepareToRun())
{
this.processor.Run();
}
}
catch (Exception ex)
{
LoggingAndNotifications.LogAndNotify(ex);
throw;
}
finally
{
this.timer.Start();
}
}
में मेरी विशिष्ट स्थिति के लिए # 4 पसंद है। क्या मेरा संपादन (मूल पोस्ट में) सही दिखता है यदि मैं उस परिदृश्य को चाहता हूं? – katit
टाइमर को रोकने में मदद नहीं मिलेगी, अगर प्रक्रिया में बहुत से सक्रिय टीपी थ्रेड हैं तो विलम्बित ईवेंट हैंडलर कॉल में देरी हो सकती है। ऑटोरसेट संपत्ति को इसके बजाय गलत पर सेट करें। –
मैं बहुत सावधान रहूंगा 2. 1 या तो स्पष्ट रूप से काम करता है या नहीं, लेकिन जब आपको वास्तव में हर एक्स मिनट की आवश्यकता होती है तो यह बहुत अच्छा होता है। 3 और 4 के बीच का अंतर "कुछ छोड़े गए प्रत्येक एक्स मिनट" और "अंतिम समाप्त होने के बाद हर एक्स मिनट" के बीच होता है। दोनों की जगह है और अक्सर या तो ठीक है। आपको '.Stop() 'टाइमर की आवश्यकता नहीं है, यह आसान और सुरक्षित है (केवल कुछ मामलों में कुछ अजीब मामलों का मतलब है कि आप इसे दूर तक नहीं मिला है) ताकि इसे पहले स्थान पर ऑटो-रीसेट न किया जा सके। 'ऑटोरसेट' को गलत पर सेट करें, या सिस्टम का उपयोग करें। थ्रेडिंग। टाइमर 'टाइमआउट। अनंत' अवधि के साथ। –