C#:为什么我的后台工作线程信号已完成但未完成?
C#,使用 VS2010,我得到了一些没有意义的东西。
在启动时,我的程序需要从文本文件加载数百k。确保加载代码工作正常后,我将其放入后台线程中。只要这是从 IDE 中运行,一切都很好,但是当它独立运行时,线程会说它已经完成,但实际上并没有。这当然会繁荣。
触发代码:
BackgroundWorker Background = new BackgroundWorker();
Background.RunWorkerCompleted += new RunWorkerCompletedEventHandler(DatabaseLoaded);
Background.DoWork += new DoWorkEventHandler(delegate { Database.Load(); });
Background.RunWorkerAsync();
正在蓬勃发展的东西在DatabaseLoaded()
中。
我放置了一些消息框来跟踪发生的情况:Load()
方法的第一行和最后一行以及 DatabaseLoaded()
的第一行。
在 IDE 中,这会按我的预期触发:Load()
开始,Load()
完成,DatabaseLoaded()
。然而,当独立运行时,我得到 Load()
开始,DatabaseLoaded()
,然后是未处理的异常框(加载器甚至还没有构建空表,更不用说填写它们。)
是我疯了还是微软疯了?
C#, using VS2010 and I've got something that makes no sense.
At startup my program needs to load several hundred k from text files. After ensuring the loading code was working fine I threw it in a background thread. So long as this is run from within the IDE everything's fine but when it's run standalone the thread says it's done when it isn't. This of course goes boom.
The trigger code:
BackgroundWorker Background = new BackgroundWorker();
Background.RunWorkerCompleted += new RunWorkerCompletedEventHandler(DatabaseLoaded);
Background.DoWork += new DoWorkEventHandler(delegate { Database.Load(); });
Background.RunWorkerAsync();
and the stuff that's going boom is in DatabaseLoaded()
.
I put some messageboxes to trace what's going on: The first and last lines of the Load()
method and the first line of DatabaseLoaded()
.
In the IDE this triggers as I expect: Load()
beginning, Load()
done, DatabaseLoaded()
. However, when run standalone I get Load()
beginning, DatabaseLoaded()
and then the unhandled exception box (the loader hasn't even gotten to build empty tables, let alone fill them.)
Am I nuts or is Microsoft?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果发生错误(例如
Database.Load()
中出现未处理的异常),将调用RunWorkerCompleted
。检查RunWorkerCompletedEventArgs
的Error
属性。RunWorkerCompleted
will be invoked in case of an error (such as an unhandled exception inDatabase.Load()
). Check theError
property of theRunWorkerCompletedEventArgs
.Database.Load()
可能会引发异常。BackgroundWorker
在触发RunWorkerCompleted
事件之前捕获任何未处理的异常。检查DatabaseLoaded
中的RunWorkerCompletedEventArgs.Error
属性。There's probably an exception thrown from
Database.Load()
.BackgroundWorker
catches any unhandled exception before triggering theRunWorkerCompleted
event. Check theRunWorkerCompletedEventArgs.Error
property inDatabaseLoaded
.