tags:

views:

418

answers:

4

first I need to say that I´m noob with WPF and C#. Application: Create Mandelbrot Image (GUI) My disptacher works perfektly this this case:

  private void progressBarRefresh(){

       while ((con.Progress) < 99)
       {
           progressBar1.Dispatcher.Invoke(DispatcherPriority.Send, new Action(delegate
                {
                    progressBar1.Value = con.Progress;
                }
              ));
       }
  }

I get the Message (Title) when tring to do this with the below code:

bmp = BitmapSource.Create(width, height, 96, 96, pf, null, rawImage, stride);

this.Dispatcher.Invoke(DispatcherPriority.Send, new Action(delegate
            {                     
                img.Source = bmp;
                ViewBox.Child = img;  //vllt am schluss
            }
          ));

I will try to explain how my program works. I created a new Thread (because GUI dont response) for the calculation of the pixels and the colors. In this Thread(Mehtod) I´m using the Dispatcher to Refresh my Image in the ViewBox after the calculations are ready.

When I´m dont put the calculation in a seperate Thread then I can refresh or build my Image.

+2  A: 

You're creating the bitmap (bmp) on your worker(?) thread and then passing it to the UI thread - it's this that's failing.

You need to create the image on the UI thread. You'll probably need some way of referencing which image you want to display and pass that information to the UI.

ChrisF
You don't need to create the image on the UI thread. You just can't use it from one thread while it's being modified on another (and .NET assumes that unless you mark it *frozen*, it will be modified again).
Ben Voigt
A: 

MSDN says: "A frozen Freezable can be shared across threads."

Maybe this thread will help: http://social.msdn.microsoft.com/Forums/en-US/windowswic/thread/9223743a-e9ae-4301-b8a4-96dc2335b686

Ben Voigt
Thx u both very. It helped a lot.
A: 

You could also use a queuing mechanism to pass messages between threads. After all, that's how the Windows architecture works. That's what the dispatcher is doing. You are passing a reference into the delegate, which is not owned by the WPF thread.

So yes, you have the basic idea, but you need to actually pass the bitmap to the other thread by using Action<T>(T object), or in your case:

Dispatcher.Invoke(DispatcherPriority.Send, new Action<Bitmap>(delegate(Bitmap img) {
    do things here...
}), bmp);
Tim
Won't make a difference, you're still sharing the object being referenced.
Ben Voigt
A: 

It helped thx u :) I love this forum <3

ChrisF