Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF and backgroundworker problem
    primarykey
    data
    text
    <p>In my program there is a BackgroundWorker class that preloads the images to BitmapImage object. I need to pass that preloaded image to the main application(WPF), where it will be copied to another BitmapImage object. This seems to be working, however, when i try</p> <pre><code>imgViewer.Source = imgNext; //imgNext is a main app copy of the preloaded image </code></pre> <p>an error occurs meaning that that object(imgNext) is owned by another thread and it cannot be used.</p> <p>Any ideas how to get rid of it and get the code working? </p> <p>Thanks everybody for answering!</p> <p>In fact, I managed to solve this problem by creating a static <code>BitmapImage</code> inside <code>App</code> class. Before using it, I do</p> <pre><code> App.iNext = null; </code></pre> <p>Then I load the actual image and freeze it, so this static property can be accessed from everywhere. When the cycle repeats many times, assigning null prevents 'object is frozen' errors.</p> <p>And of course, there was a lot of work with managing single BGW instance, queuing tasks etc.</p> <p><em>(Currently I'm using ImagesContainer class defined also in my program that has two BitmapImage properties. I use it to receive preloaded images from backgroundworker. )</em></p> <p><em>imgNext is a public variable defined in MainWindow. (main thread)</em></p> <pre><code> void bwImgLoader_DoWork(object sender, DoWorkEventArgs e) { backgrLoadNextPrevList list = e.Argument as backgrLoadNextPrevList; ImagesContainer result = new ImagesContainer(); if (list.HasNextPath) result.imgPrev = PrepareImage(list.NextPath); if (list.HasPrevPath) result.imgNext = PrepareImage(list.PrevPath); e.Result = result; } void bwImgLoader_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { ImagesContainer result = e.Result as ImagesContainer; if (result.imgNext != null) { setNextDelegate s = new setNextDelegate(setNext); object[] t = { result.imgNext }; imgNext.Dispatcher.Invoke(s, t); } // do not take into account this line, just ignore it. //if (result.imgPrev != null) imgPrev = result.imgPrev; } public void setNext(BitmapImage b) { imgNext = b; } public delegate void setNextDelegate(BitmapImage b); </code></pre> <p>Freezing the bitmapimage helps only on the first background load(see the comment under the answer below). When I call BackgroundWorker second time, an errors occures that the object is frozen and cannot be modified. Is there a way to un-freeze it?</p> <p>Or, is there any way to copy data from one thread to another without copying an attribution to thread?</p> <hr> <hr> <p><strong>UPDATED</strong></p> <p>Thanks everybody for answering!</p> <p>In fact, I managed to solve this problem by creating a static <code>BitmapImage</code> inside <code>App</code> class. Before using it, I do</p> <pre><code> App.iNext = null; </code></pre> <p>Then I load the actual image and freeze it, so this static property can be accessed from everywhere. When the cycle repeats many times, assigning null prevents errors.</p> <p>And of course, there was a lot of work with managing single BGW instance, queuing tasks etc.</p> <p>But these efforts were worth the result - I got +125% in performance!!! Thank everyone!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload