private System.Timers.Timer _Watcher = null;
public System.Timers.Timer Watcher
{
get { return _Watcher; }
set { _Watcher = value; }
}
private static BackgroundWorker BackgroundWorkerForJob { get; set; }
public bool WorkInProcess { get; set; }
public void Start()
{
GenerateWatcher();
}
public void Stop()
{
this.Watcher.Stop();
this.Watcher.Dispose();
_start = false;
}
private void GenerateWatcher()
{
this.Watcher = new System.Timers.Timer(5000);
if (BackgroundWorkerForJob != null)
{
_start = false;
BackgroundWorkerForJob.CancelAsync();
BackgroundWorkerForJob.Dispose();
BackgroundWorkerForJob = null;
}
BackgroundWorkerForJob = new BackgroundWorker();
BackgroundWorkerForJob.DoWork += new DoWorkEventHandler(Watcher_DoWork);
_start = true;
BackgroundWorkerForJob.RunWorkerAsync();
}
private bool _start = false;
private void Watcher_DoWork(object sender, DoWorkEventArgs e)
{
while (_start)
{
DoWork();
System.Threading.Thread.Sleep(5000);
}
}
private void DoWork()
{
try
{
if (!this.WorkInProcess)
{
this.WorkInProcess = true;
this.Watcher.Interval = 5000;
//Çalıştıracağın metotu buraya yazacaksın
this.WorkInProcess = false;
}
}
catch (Exception ex_)
{
try
{
//DoCreateErrorLog(ex_);
//Eğer çalıştırdığın metot hata verirse burada loglama yapabilirsin
}
catch
{
}
this.WorkInProcess = false;
}
}Start Metodu görevi başlatır.
Stop metodu ise durdurur.Umarım yardımcı olur bu kodlar sana.