1

I'm so frustrated trying to solve this. Why is creating threads and updating UI after completing them so complicated?! I have tried this, this and this and I still can't get my head around the whole multi-threading thing clearly at all. It was so easy in Java...

Anyway here's a simplified version of my code as the result of third link:

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);
}

The problem here is that I can't set the Source value of the Image control to bimg but I can set it to null! Whenever I try to set it to bimg I get the exception that says the calling thread cannot access this object because a different thread owns it. I have also tried setting it directly to bi which also gives the same exception.

But I can set the source to null without any problem, which means I can modify the Source value of the Image control. But how do I access bi or bimg? Am I doing something wrong?

Also: I notice that the given arguments of the last BeginInvoke do not match any overload of the method but they are still accepted as valid and works properly. When I point at BeginInvoke I am shown a completely different set of method overloads than the ones that appear when I type a ( following the method name. Why?

H.B.
  • 166,899
  • 29
  • 327
  • 400
ADTC
  • 8,999
  • 5
  • 68
  • 93
  • http://stackoverflow.com/questions/3034902/how-do-you-pass-a-bitmapimage-from-a-background-thread-to-the-ui-thread-in-wpf – Serj-Tm Mar 04 '12 at 07:59
  • Thanks. I didn't know it was a BitmapImage-related problem, hence I didn't find that question. I thought it was something wrong with my multithreading and use of delegates. Anyway thanks. Btw, can you approve my edit on your answer? (Not sure if you're allowed to do that or not.) **PS: I'm still looking for an answer for my *Also* part. :)** – ADTC Mar 04 '12 at 08:56
  • last BeginInvoke matched to signature BeginInvoke(DispatcherPriority priority, Delegate method, Object arg, params Object[] args). Params argument treated as optional(no arguments): http://msdn.microsoft.com/en-us/library/w5zay9db.aspx – Serj-Tm Mar 04 '12 at 09:16
  • 1
    @DarkGray I found the answer to it. I have edited your answer to include the explanation. Waiting for peer review. – ADTC Mar 04 '12 at 10:31

1 Answers1

2

Answer for main question

Execute bi.Freeze() before using the BitmapImage in the UI thread. The Freeze operation will make the BitmapImage read-only and thread-safe.

    ...
    //do some long processing with the strings
    BitmapImage bi = new BitmapImage();//there will be an actual bitmap here
    bi.Freeze();

    SimpleDelegate2 del1 = delegate(BitmapImage bimg)
    ...

Answer for Also

The class System.Windows.Threading.Dispatcher has five signatures for the BeginInvoke method:

public DispatcherOperation BeginInvoke(Delegate method, params object[] args);
public DispatcherOperation BeginInvoke(DispatcherPriority priority, Delegate method);
public DispatcherOperation BeginInvoke(Delegate method, DispatcherPriority priority, params object[] args);
public DispatcherOperation BeginInvoke(DispatcherPriority priority, Delegate method, object arg);
public DispatcherOperation BeginInvoke(DispatcherPriority priority, Delegate method, object arg, params object[] args);

However, three of the signatures (those with DispatcherPriority priority as their first argument) have the following tags on them:

[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]

This is why they do not show up when typing a ( after the method name.

ADTC
  • 8,999
  • 5
  • 68
  • 93
Serj-Tm
  • 16,581
  • 4
  • 54
  • 61
  • I need to set the BitmapImage to the Image control (WPF) and it comes as a result of the long-running operation. I'm doing the operation in another thread, but I can't set the resulting BitmapImage to the Image control in the UI thread. – ADTC Mar 04 '12 at 07:14
  • which kind of long-running operation? – Serj-Tm Mar 04 '12 at 07:20
  • Are details of the operation necessary? It's just doing some image processing (for 15-30 seconds) and in the end it gives me a BitmapImage. I have to show this resulting BitmapImage on the UI (through the Image control on WPF form). Is there another way to do this? I'm trying to use delegates which is a recommended way to update UI from a non-UI thread, but somehow it just refuses to work (except for null value). – ADTC Mar 04 '12 at 07:23
  • Strangely that works!!! I have no idea why it does. Please update your answer and, if you could, explain why it works. – ADTC Mar 04 '12 at 07:37