views:

236

answers:

5

I need to run 5 algorithms parallely each takes an image as input and gives image as output. After each of these is done, I need to display the 5 output images. I'm using Asynchronous Callback using delegates for this task.

So, I created 5 delegates for these 5 algos and calling them like algo1Delegate.BeginInvoke().

Algorithms are running fine and giving the output too. I'm facing 2 problems in displaying these images.

For displaying images, I created a class ImageViewer (windows form with picturebox element in it).

//ImageViewer constructor
ImageViewer(Image img, String Title)
{
    this.pictureBox1.Image = img;
    this.Text = Title;
}

I'm displaying images like this:

void showImage(Image image, String title)
{
    ImageViewer imageviewer = new ImageViewer(image, title);
    imageviewer.Show();
}

Since I need to display an image after algo. I'm passing new AsyncCallback(showImage) delegate for each of these BeginInvoke() as 3rd parameter


private void showImage(IAsyncResult iasycResult)
{
    MessageBox.Show("white" + Thread.CurrentThread.ManagedThreadId);


    // Retrieve the `caller` delegate.
    AsyncResult asycResult = (AsyncResult)iasycResult;
    caller = (Algo1Delegate)asycResult.AsyncDelegate;//### PROBLEM!!!

    // Retrieve the  string Title that is passed in algodelegate.BeginInvoke().
    string title = (string)iasycResult.AsyncState;
    Image outputImage = caller.EndInvoke(iasycResult);

    showImage(outputImage, title);

}
  1. I think you can see the problem in the above callback function. it only works for Algo1 for other 4 alog's it needs to be casted to Algo2Delegate , Algo3Delegate etc.. because asycResult.AsyncDelegate is of type object. How can I solve this problem? How can I make it work for others too?

  2. The imageViewer window is getting "unresponsive". I don't understand why? ImageViewer object is initialized and displayed on the same thread for each of these algos. Why is it becoming unresponsive.

  3. Any other alternative solutions?

PS: I cannot declare one delegateType for all the algos since there are some differences in input parameters.

EDIT:

Well, I got enough inputs for my 1st and 3rd questions. I used separate callbacks for each of these algorithms. My 2nd problem is still unsolved. I changed the constructor of ImageViewer() Just to check if they are executing on two different threads:

    public ImageViewer(Image img, String title)
    {
        InitializeComponent();
        if (pictureBox1.InvokeRequired) MessageBox.Show("You must Invoke()");
        else MessageBox.Show("No need of Invoke()");

        this.pictureBox1.Image = img;
        this.Text = title + " : Image Viewer";
    }

in every case it says No need of Invoke(). I don't understand what is the problem. Can any one please address this too? I don't get any execptions also. Just the window is becoming unresponsive. I checked if algorithms are causing any trouble. But no, they arent.

A: 

Can you wrap your calls into lambda expression and then, have a method that starts the delegate:

private void run(Action<Image,Image> delegate, Image inputImage)
{
   delegate.BeginInvoke(inputImage, // all the callback stuff here );
}

But then call your run method with lambdas:

run(image => algo1(image, otherVar, otherVar2));
run(image => algo2(image, otherVar, otherVar2, otherVar3, otherVar4));

and so on

LorenVS
I've edited my query can you please have a look at it once?
claws
A: 

Hello Claws, I was doing something similar some months ago, I was using ThreadPool:

It is managing the threads for you and is fairly easy to use for tasks which are not requiring complex multithreading.

BlueTrin
I know how to use threadpools. But I think it wont help here. Because I need callback functionality. http://stackoverflow.com/questions/1837962/asynchronous-delegates-vs-thread-threadpool
claws
+1  A: 

You should substitute the delegates with a consistent hierarchy with the common methods you need.

AsyncCallbackClass caller = (AlgoDelegate)asycResult.AsyncState;

Image img = caller.DoCallBack(iAsyncResult);

then you have a hierarchy with:

class AsyncCallback1 : AsyncCallbackClass
{
   Image DoCallBack(IAsyncResult result)
   {
      // Call specific callback with specific parameters
   }
}

class AsyncCallback2 : AsyncCallbackClass
{
   Image DoCallBack(IAsyncResult result)
   {
      // Call specific callback with specific parameters
   }
}

Basically you'll be constructing your callbacks as a hierarchy of classes so that the "signature" of the main method is the same (a method that takes an IAsyncResult) and returns an image, but the way each "delegate" (which is now a full class) implements the call is unique for each implementation.

Take a look at Replace Delegate with inheritance.

Edit: From the msdn page.

true if the control's Handle was created on a different thread than the calling thread (indicating that you must make calls to the control through an invoke method); otherwise, false.

I assume you're creating the ImageBox in the ImageViewer, and the ImageViewer is being created in the callback so, by definition, the ImageBox has been created by the same thread and therefore does not need to be invoked.

Jorge Córdoba
Sorry! Didn't understand.
claws
I've edited the answer. Hope now it is clear... see the link provided too.
Jorge Córdoba
I've edited my query can you please have a look at it once?
claws
Edited, basically I think you're actually creating the ImageViewer in the thread.
Jorge Córdoba
Yeah, even though they've been created on the same thread. But still why am I facing problem and How could I solve it?Thanks a lot for helping. I've learnt some new methods for solving problem.
claws
A: 

@1. You have five delegates but you have defined a common callback method for each. So you will have a problem finding out what delegate actually has completed. One way is to have different callback method for every delegate.

@2 You should not update the UI from a different thread than on what it was created. If it is true, we use Control.Invoke to make sure the call is marshaled to the UI thread.

    MethodInvoker updateImageViewer = delegate
    {
        ImageViewer imageviewer = new ImageViewer(image, title);
        imageviewer.Show();
    };

    if (this.pictureBox1.InvokeRequired)
        this.pictureBox1.Invoke(updateImageViewer);
    else
        updateImageViewer();
Aseem Gautam
@2: "You should not update the UI from a different thread than on what it was created". Where did I do that? They are created and updated on the same thread.
claws
The callback is created on the same thread as the delegate.So when you are running this statement [ImageViewer imageviewer = new ImageViewer(image, title);] it is updating picturebox1 on the same thread ie the callback thread. Though ImageViewer is created on the same thread, the actual control ie PictureBox might have been created on the UI thread.
Aseem Gautam
claws
The actual picturebox constructor must have been called by the framework during form initialization (designer.cs). I am not sure if you can actually find out the thread ID which created the picturebox control. This usually happens on the main thread.
Aseem Gautam
I've edited my query can you please have a look at it once?
claws
+1  A: 

I can't think of a clean solution to your problem. You'd have to write fugly code like this:

  AsyncResult result = (AsyncResult)iresult;
  if (result.AsyncDelegate is AsyncDelegate1) {
    (result.AsyncDelegate as AsyncDelegate1).EndInvoke(iresult);
  }
  else if (result.AsyncDelegate is AsyncDelegate2) {
    (result.AsyncDelegate as AsyncDelegate2).EndInvoke(iresult);
  }
  //etc...
  ComputationResult answer = result.AsyncState as ComputationResult;

Yuck. You really ought to have an individual callback method for each delegate type. A generic method cannot help here, the constraint cannot be a delegate type. A lambda in the BeginInvoke method call doesn't look that much better:

  var task1 = new AsyncDelegate1(Compute1);
  var result1 = new ComputationResult("task1");
  task1.BeginInvoke(42, result1, 
    new AsyncCallback((ia) => {
      AsyncResult result = ia as AsyncResult;
      (result.AsyncDelegate as AsyncDelegate1).EndInvoke(ia);
      CommonCallback(result.AsyncState as ComputationResult);
    }), 
    result1);

Nah. I'd tackle this by using only one delegate type. The WaitCallback type is suitable, although mis-named, you should write little helper classes that store the arguments for the delegate target so you can pass it through the WaitCallback.state argument.


Your second problem is induced because you are creating the ImageViewer instance in the callback method. The callback executes on a threadpool thread, not the UI thread. InvokeRequired returns false because the PictureBox control was created on the threadpool thread. This threadpool thread is however not suitable to display UI components, it doesn't pump a message loop. And has the wrong apartment state. And it terminates too soon.

InvokeRequired will return the proper value (true) when you use a Control that was created on the UI thread. Your main startup form for example. Or Application.OpenForms[0]. There's little point in using InvokeRequired however, you know for a fact that the callback executes on the wrong thread. Just use BeginInvoke directly. The invoked method should create the ImageViewer instance.


You are well on your way re-inventing the BackgroundWorker class. It does exactly what you are trying to do. But takes care of the gritty details of getting the RunWorkerCompleted event fired on the correct thread. You ought to consider it.

Hans Passant
I've edited my query can you please have a look at it once?
claws
I expanded my answer, below the line.
Hans Passant
claws
Added another comment below the 2nd line.
Hans Passant
Well, I know about Backgroundworkers but I thought that it would cause an overhead of 5 objects. Also I wanted to try "hardway" way just to learn. I solved it using Backgroundworkers. Now for completion sake can you please show how to do it using the Async Callback method we've been discussing?Thanks a lot helping. I learnt a lot from this.
claws