开发者

How to solve these problems with Asynchronous Callback?

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 开发者_StackOverflowfor 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.


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.


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.


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


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

  • http://msdn.microsoft.com/en-us/library/3dasc8as%28VS.80%29.aspx

  • http://www.switchonthecode.com/tutorials/csharp-tutorial-using-the-threadpool

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


@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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜