Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 522195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:19:45+00:00 2026-05-13T08:19:45+00:00

I need to run 5 algorithms parallely each takes an image as input and

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:19:46+00:00Added an answer on May 13, 2026 at 8:19 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.