Note that there are some explanatory texts on larger screens.

plurals
  1. POLong running operation - updating UI with BitmapImage doesn't work
    text
    copied!<p>I'm so frustrated trying to solve this. Why is creating threads and updating UI after completing them <strong>so complicated</strong>?! I have tried <a href="http://msdn.microsoft.com/en-us/magazine/cc163328.aspx#S4" rel="nofollow" title="WPF Threads: Build More Responsive Apps With The Dispatcher">this</a>, <a href="http://elegantcode.com/2009/07/03/wpf-multithreading-using-the-backgroundworker-and-reporting-the-progress-to-the-ui/" rel="nofollow" title="Elegant Code » WPF Multithreading: Using the BackgroundWorker and Reporting the Progress to the UI.">this</a> and <a href="http://www.a2zdotnet.com/View.aspx?id=93#.T1L5T4d-fxM" rel="nofollow" title="A2Zdotnet.com - How to Create multithreaded UI application in WPF">this</a> and I still can't get my head around the whole multi-threading thing clearly at all. It was so easy in Java...</p> <p>Anyway here's a <strong>simplified version</strong> of my code as the result of third link:</p> <pre><code>private delegate void SimpleDelegate2(BitmapImage bi); private delegate void SimpleDelegate(string st1, string st2); private void Process(string st1) { try { string st2 = "test st2"; SimpleDelegate del = new SimpleDelegate(LongRunningProcess); del.BeginInvoke(st1, st2, null, null); } catch { //do some failsafe thing } } private void LongRunningProcess(string st1, string st2) { //do some long processing with the strings BitmapImage bi = new BitmapImage();//there will be an actual bitmap here SimpleDelegate2 del1 = delegate(BitmapImage bimg) { ImageControlOnWPFform.Source = bimg; //null; }; this.Dispatcher.BeginInvoke(DispatcherPriority.Send, del1, bi); } </code></pre> <p>The problem here is that I can't set the <code>Source</code> value of the Image control to <code>bimg</code> <strong>but I can set it to <code>null</code>!</strong> Whenever I try to set it to <code>bimg</code> I get the exception that says <em>the calling thread cannot access this object because a different thread owns it</em>. I have also tried setting it directly to <code>bi</code> which also gives the same exception.</p> <p>But I can set the source to <code>null</code> without any problem, which means I can modify the <code>Source</code> value of the Image control. But how do I access <code>bi</code> or <code>bimg</code>? Am I doing something wrong?</p> <p><strong>Also:</strong> I notice that the given arguments of the last <code>BeginInvoke</code> do not match any overload of the method <em>but</em> they are still accepted as valid and works properly. When I point at <code>BeginInvoke</code> I am shown a completely different set of method overloads than the ones that appear when I type a <code>(</code> following the method name. Why?</p>
 

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