我可以在一个方法中定义后台工作吗?
private void DownLoadFile(string fileLocation){
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler((obj, args) => {
// Will be executed by back ground thread asynchronously.
args.Result = Download(fileLocation);
});
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler((obj, args) => {
// will be executed in the main thread.
Result r = args.Result as Result;
ReportResult(r);
});
worker.RunWorkerAsync(fileLocation);
}
问题:如果Download()函数需要长时间来下载文件,GC能否在RunWorkerCompleted()执行之前回收worker对象?
QueueUserWorkItem
将其添加到线程池队列中:http://msdn.microsoft.com/zh-cn/library/system.threading.threadpool.queueuserworkitem.aspx - J...