你会如何使用C#中最新的线程技术重写它?
var dateRange = new DateRange(date, date.AddDays(1));
var extracter = new ConversionsExtracter(dateRange, AdvertiserId);
var loader = new ConversionsLoader();
var extracterThread = extracter.Start();
var loaderThread = loader.Start(extracter);
extracterThread.Join();
loaderThread.Join();
Run Code Online (Sandbox Code Playgroud)
loader和extract对象都有一个Start方法:
public Thread Start()
{
var thread = new Thread(Extract);
thread.Start();
return thread;
}
Run Code Online (Sandbox Code Playgroud)
Jim*_*hel 11
有任务:
var t1 = Task.Factory.StartNew(() => extracter.Start(), TaskCreationOptions.LongRunning);
var t2 = Task.Factory.StartNew(() => loader.Start(), TaskCreationOptions.LongRunning);
// some arbitrary amount of code here that's executed on the main thread.
// Wait for both threads to complete before continuing.
t1.Wait();
t2.Wait();
// Code here cannot execute until the loader and extractor are finished.
Run Code Online (Sandbox Code Playgroud)
任务有许多功能,使它们比明确管理线程更容易使用,包括支持取消,延续等.值得研究.