System.Threading.Timer working logic and if an unexpected error happens how to handle

≯℡__Kan透↙ 提交于 2019-12-25 05:16:14

问题


Alright i am using System.Threading.Timer objects to regularly fire events

Q1: As far as i understood it starts new tasks like Task.Factory.StartNew am i correct ?

Here how i use example System.Threading.Timer object

private static Timer _timer;
 _timer = new Timer(checkWaitingUrls, null, PublicSettings.irTimers_Delayed_Start_MiliSeconds, howManySeconds * 1000);

Now i can handle unexpected errors at Task.Factory.StartNew like below

    Task myTask = Task.Factory.StartNew(() =>
    {
    process_Given_Page(drwToProcess);
    });

    myTask.ContinueWith(t => ErrorLogger.LogError
("error " + t), TaskContinuationOptions.OnlyOnFaulted);

Q2: How can i handle System.Threading.Timer callback tasks when an error happened so the application wouldn't crash and timer continues to work?

I don't want to code complete try catch event inside the timer callback function

Ty very much for answers

c# .net 4.5 WPF

Edit I found the answer that use System.Timers.Timer and it will swallow such errors without letting you know however you can't log them

来源:https://stackoverflow.com/questions/25417084/system-threading-timer-working-logic-and-if-an-unexpected-error-happens-how-to-h

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!