开发者

Application ends without throwing exceptions

The following code

new System.Threading.Timer(state =>
{
     var query = "Select Caption from Win32_OperatingSystem";
     var objectQuery = new WqlObjectQuery(query);
     var searcher = new ManagementObjectSearcher(objectQuery);
     searcher.Get();
     //unreachable code
}, null, 0, 1000);

ends my App 开发者_StackOverflow中文版immediately without throwing exceptions. If you copy and paste these lines in a new Console project you probably won't be able to reproduce the problem. The code above is a small portion of my Windows Service and it is likely the cause of the problem. The above code is exercised by unit tests which run normally without throwing exceptions. The last executed line before the problem occurs is not always the same. What should I do?


I replaced

this.timer = new Timer(
    TimerCallback,
    null,
    Settings.Default.CheckDelay,
    Settings.Default.CheckInterval);

by

new Thread(delegate()
    {
        Thread.Sleep(Settings.Default.CheckDelay);
        while (isRunning)
        {
            TimerCallback(null);
            Thread.Sleep(Settings.Default.CheckInterval);
        }
    }).Start();

Which is uglier, but solves my undebuggable problem

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜