Delegates and Threads

H

Howard Weiss

I am writing a Managed C++ image processing application which consists of a
main window and a number of image processing windows. The main window is a
Windows Forms and each image window is an instance of a different Windows
Form. The image windows are created by creating New instances of the image
form. The image forms become visible when I invoke show method.

Initially, I wrote my application as a single threaded application.
However, it takes considerable time to generate the images and I wish to
keep my UI interactive, so I am converting to a multi-threaded application.
At first, I created the image and invoked the show method for the images in
the worker thread. The images disappeared as soon as the worker thread
completes. I therefore created a delegate to invoke the show method from
within my UI thread.

Code in my main thread looks like

showImage(Image *Image)
{
Image->Show();
}

Code in my worker thread looks like

__delegate void delegateImageShow(Image *Image);

....
Image *pImageToDisplay;
delegateImageShow *pShow = new delegateImageShow(this, showImage);
pShow->Invoke(pImageToDisplay)
....

However, if I look at the HashCode of the thread which is running showImage,
it is the HashCode of the worker thread and not the HashCode of my UI
thread.

I am puzzled

Howard Weiss
__delegate void delegateImageActivated(int Index);
 
H

Howard Weiss

The solution was to use Invoke as follows

....
Object *pObject[] = {__box(pImage)};
Invoke(pShow, pObject);
.....

rather than

pShow_>Invoke()

Howard Weiss
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top