Click here to Skip to main content
15,908,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im trying to do this:
C#
using (var albm = isoStore.CreateFile("shared/media/" + dlSongInfo.id + "-NP"))
                        {
                            Deployment.Current.Dispatcher.BeginInvoke(() =>
                             {
                                 BitmapImage image = new BitmapImage();
                                 image.SetSource(e.Result);
                                 WriteableBitmap wb = new WriteableBitmap(image);

                                 wb.SaveJpeg(albm, 358, 358, 0, 100);
                             });
                        }


But it said that the stream is closed when doing the SaveJpeg. I guess that because there in different threads it can't access it? How can I achieve this. Or is is an alternative way of writing a bitmap image from a stream without needing to use a UI thread?

P.S. This is execute on a ThreadPool.
Posted
Updated 26-Aug-12 6:40am
v2

1 solution

The using statement disposes your object before (in most cases) your Delegate gets executed. On single core machines you could see a race here, because sometimes it could work but mostly it won't. Simply don't put all this in a using statement but instead create the albm object normally and dispose it in the delegate you're calling.

C#
var albm = isoStore.CreateFile("shared/media/" + dlSongInfo.id + "-NP"));
Deployment.Current.Dispatcher.BeginInvoke(() => {
  try {
    BitmapImage image = new BitmapImage();
    image.SetSource(e.Result);
    WriteableBitmap wb = new WriteableBitmap(image);
    wb.SaveJpeg(albm, 358, 358, 0, 100);
  } finally {
    albm.Dispose();
  }
});
 
Share this answer
 
Comments
Zumicts 2-Sep-12 13:22pm    
Great! this was the problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900