什么是现代C#方式启动两个线程然后等待它们结束?

时间:2013-07-26 01:09:28

标签: c# multithreading

如何使用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();

loader和extract对象都有Start方法:

public  Thread Start()
{
    var thread = new Thread(Extract);
    thread.Start();
    return thread;
}

2 个答案:

答案 0 :(得分: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.

任务有许多功能,使它们比明确管理线程更容易使用,包括支持取消,延续等。值得深入研究。

答案 1 :(得分:0)

使用这个定义的方法:

async Task<TResult> RunLoadAsync(/* args */) {
  var dateRange = new DateRange(date, date.AddDays(1));
  var extracter = new ConversionsExtracter(dateRange, AdvertiserId);
  var loader    = new ConversionsLoader();

  var intermediate = await Task.Factory.StartNew(
      () => extracter.Start(), TaskCreationOptions.LongRunning
  );
  return await Task.Factory.StartNew(
      () => loader.Start(intermediate), TaskCreationOptions.LongRunning
  );
}

像这样调用它:

   var result = await RunLoadAsync(/* parameters */);

更新:以下是一个更完整的使用示例:

private async void PanelBoard_MouseCtlClick(object sender, HexEventArgs e) {
  GoalHex = e.Coords;
  // other processing to prepare.
  try { Path = await MapBoard.GetDirectedPathAsync(MapBoard[StartHex], MapBoard[GoalHex]); } 
  catch (OperationCanceledException) { Path = default(IDirectedPath); }
}

public static async Task<IDirectedPath> GetDirectedPathAsync(
  this IBoard<IHex> @this, 
  IHex start,  IHex goal
) {
  if (@this == null) throw new ArgumentNullException("this");
  return @this.GetDirectedPath(start, goal);
}

IDirectedPath Path {
  set { /* code here to refresh display when Path is ready */ }
}

public static Task<IDirectedPath> GetDirectedPathAsync(
  this IBoard<IHex> @this, 
  IHex start,  IHex goal
) {
  if (@this == null) throw new ArgumentNullException("this");
  return Task.Run<IDirectedPath>(
      () => @this.GetDirectedPath(start, goal)
  );
}

方法 GetDirectedPathAsync()上的 async 限定符使其成为等待的对象。