Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to solve these problems with Asynchronous Callback?
    primarykey
    data
    text
    <p>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.</p> <p>So, I created 5 delegates for these 5 algos and calling them like algo1Delegate.BeginInvoke().</p> <p>Algorithms are running fine and giving the output too. I'm facing 2 problems in displaying these images.</p> <p>For displaying images, I created a class ImageViewer (windows form with picturebox element in it). </p> <pre><code>//ImageViewer constructor ImageViewer(Image img, String Title) { this.pictureBox1.Image = img; this.Text = Title; } </code></pre> <p>I'm displaying images like this: </p> <pre><code>void showImage(Image image, String title) { ImageViewer imageviewer = new ImageViewer(image, title); imageviewer.Show(); } </code></pre> <p>Since I need to display an image after algo. I'm passing <code>new AsyncCallback(showImage)</code> delegate for each of these BeginInvoke() as 3rd parameter </p> <hr> <pre><code>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); } </code></pre> <ol> <li><p>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 <code>object</code>. How can I solve this problem? How can I make it work for others too?</p></li> <li><p>The imageViewer window is getting "unresponsive". I don't understand why? <code>ImageViewer</code> object is initialized and displayed on the same thread for each of these algos. Why is it becoming unresponsive.</p></li> <li><p>Any other alternative solutions?</p></li> </ol> <p>PS: I cannot declare one delegateType for all the algos since there are some differences in input parameters.</p> <h2>EDIT:</h2> <p>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:</p> <pre><code> 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"; } </code></pre> <p>in every case it says <code>No need of Invoke()</code>. 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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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