C# 应用程序结束时不会抛出异常
下面的代码C# 应用程序结束时不会抛出异常,c#,.net,visual-studio-2010,debugging,exception,C#,.net,Visual Studio 2010,Debugging,Exception,下面的代码 new System.Threading.Timer(state => { var query = "Select Caption from Win32_OperatingSystem"; var objectQuery = new WqlObjectQuery(query); var searcher = new ManagementObjectSearcher(objectQuery); searcher.Get(); //
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);
立即结束我的应用程序而不引发异常。
如果将这些行复制并粘贴到新的控制台项目中,则可能无法重现该问题。
上面的代码是我的Windows服务的一小部分,可能是问题的原因。上述代码由单元测试执行,单元测试正常运行,不会引发异常。
问题发生前最后执行的行并不总是相同的。
我该怎么办?我替换了
this.timer = new Timer(
TimerCallback,
null,
Settings.Default.CheckDelay,
Settings.Default.CheckInterval);
借
这更难看,但解决了我无法解决的问题
new Thread(delegate()
{
Thread.Sleep(Settings.Default.CheckDelay);
while (isRunning)
{
TimerCallback(null);
Thread.Sleep(Settings.Default.CheckInterval);
}
}).Start();