Asp.net zamanlanmış görev
3
●1.137
- 27-11-2014, 10:09:37Kimlik doğrulama veya yönetimden onay bekliyor.Merhaba arkadaşlar bir projede zamanlanmış görev ayarlamam gerekiyor ama server tarafından yapmadan bunu sadece kod tarafından tetiklemek mevcut mu? 5 dakikada bir tetiklenecek bir metod çağırmam gerekiyor nasıl yapabilirim sunucu olaylarını v.s. karıştırmadan.
- 27-11-2014, 10:39:20
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. - 01-12-2014, 11:04:52sayfanda her zaman ziyaretçi bulunuyormu ? . çünkü server kodu ile yaparsan (asp.net) , kodun sayfaya ziyaretçi geldiğinde aktif olacaktır.
Asp.net kullanarak şöyle yapabilirsin :
Veritabanına bir field açarsın ve o field'da son çalışma zamanı gibi bir değer tutarsın .
daha sonra sayfa her yüklendiğinde şimdiki zaman ile son çalışma zamanı arasındaki farkı kontrol edersin eğer 5 dk'tan fazlaysa çağırmak istediğin fonksiyonu çağırısın .
Ek olarak ;
eğer server taraflı yapma imkanın varsa o şekilde daha sağlıklı olur. task scheduler'a bir görev eklersin o çalışır.