[英]Initialization of objects in the background
I need to initialize few objects, it can take a while so I want to do it in some background thread. 我需要初始化一些对象,这可能需要一段时间,所以我想在一些后台线程中完成它。 I also want to display progressBar showing what is the progress of initialization.
我还想显示progressBar,显示初始化进度。
What is the most elegant way to do it? 最优雅的方法是什么?
I was thinking about an interface: 我在考虑一个接口:
interface ILoadable
{
int Progress { get; }
event EventHandler Loaded;
}
Why not just use a BackgroundWorker directly? 为什么不直接使用BackgroundWorker ? It provides events for
DoWork
, ProgressChanged
, and RunWorkerCompleted
. 它为
DoWork
, ProgressChanged
和RunWorkerCompleted
提供事件。
The advantage of this (or a thin wrapper over this) is that you automatically get the threading handled for you, properly, and it's very well tested. 这样做的好处(或为此而做的包装很薄)是,您可以自动为您正确地处理线程,并且已经过了很好的测试。
If you want to make a wrapper around this, I'd actually recommend making yourself an abstract class that encapsulates the BackgroundWorker, and lets you provide Action
delegates for the run operation. 如果您想对此做一个包装,我实际上建议您使自己成为一个抽象类,该抽象类封装BackgroundWorker,并为运行操作提供
Action
委托。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.