WriteableBitmap在后台工作者中

时间:2011-12-23 08:27:46

标签: multithreading silverlight windows-phone-7

我目前有一个像这样的循环:

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,以确保它能够响应用户的操作?

1 个答案:

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