2012-05-15 32 views
6

Nếu mã sau đây chạy trên chủ đề nền, làm cách nào để 'Tiếp tụcWith' trên chuỗi chính?Tiếp tục công việc trên chuỗi giao diện người dùng, khi bắt đầu từ chủ đề nền

var task = Task.Factory.StartNew(() => Whatever()); 
    task.ContinueWith(NeedThisMethodToBeOnUiThread), TaskScheduler.FromCurrentSynchronizationContext()) 

Ở trên sẽ không hoạt động vì ngữ cảnh đồng bộ hóa hiện tại đã là chủ đề nền.

Trả lời

6

Bạn cần nhận tham chiếu đến TaskScheduler.FromCurrentSynchronizationContext() từ chuỗi giao diện người dùng và chuyển nó đến tiếp tục.

Tương tự như vậy. http://reedcopsey.com/2009/11/17/synchronizing-net-4-tasks-with-the-ui-thread/

private void Form1_Load(object sender, EventArgs e) 
{ 
    // This requires a label titled "label1" on the form... 
    // Get the UI thread's context 
    var context = TaskScheduler.FromCurrentSynchronizationContext(); 

    this.label1.Text = "Starting task..."; 

    // Start a task - this runs on the background thread... 
    Task task = Task.Factory.StartNew(() => 
     { 
      // Do some fake work... 
      double j = 100; 
      Random rand = new Random(); 
      for (int i = 0; i < 10000000; ++i) 
      { 
       j *= rand.NextDouble(); 
      } 

      // It's possible to start a task directly on 
      // the UI thread, but not common... 
      var token = Task.Factory.CancellationToken; 
      Task.Factory.StartNew(() => 
      { 
       this.label1.Text = "Task past first work section..."; 
      }, token, TaskCreationOptions.None, context); 

      // Do a bit more work 
      Thread.Sleep(1000); 
     }) 
     // More commonly, we'll continue a task with a new task on 
     // the UI thread, since this lets us update when our 
     // "work" completes. 
     .ContinueWith(_ => this.label1.Text = "Task Complete!", context); 
} 
+0

Tôi sợ điều đó. Cảm ơn câu trả lời của bạn. – user981225

Các vấn đề liên quan