2011-12-23 66 views
2

我现在有一个循环是这样的:WriteableBitmap的在后台工作

foreach(var item in theList) 
{ 
//create writeablebitmap from item.GetPicture() which returns a stream. 
//do stuff with it 
//save it to isolated storage 
} 

这是一个非常长期运行的进程,因为有吨图像。我试图把它放在后台工作,但你不能在那里创建WriteableBitmaps(他们必须在UI线程上)。问题在于界面完全没有反应。

我该如何让它在每个循环中处理一次按键/ UI,以确保它能够响应用户的操作?

+0

这是你需要在手机上做的事吗? – 2011-12-23 09:08:52

回答

2
BackgroundWorker backgroundWorker = new BackgroundWorker(); 
backgroundWorker.DoWork += (s, e) => 
{ 
    foreach(var item in theList) 
    { 
     var item _ = item; 
     Deployment.Dispatcher.BeginInvoke(() => 
     { 
      // do your stuff on UI thread with item_ 
     }); 
     Thread.Sleep(milliseconds); //experiment with delay to find balance between UI responsiveness and performance 
    } 
}; 
backgroundWorker.RunWorkAsync(); 
+1

完美,谢谢! – Matt 2011-12-23 17:11:17