user selects an image (using fileOpenDialog) then I need to run 3 algorithms sequentially (output of开发者_StackOverflow中文版 one algo becomes input of another) on this image. so, I'm using background worker for running these algorithms.
After each stage I want to see the result image. For this I created a ImageViewer class, which is just a simple form containing PictureBox control. This is what going on in
backgroundworker1_dowork()
{
Image img1 = runAlgo(img); //this statment is executing fine
ImageViewer imgviewer1 = new ImageViewer(img1);
imgviewer1.show();
}
now imgviewer1 is becoming unresponsive (in title its shown NOT RESPONDING) . And there is no image in it.
//ImageViewer constructor
ImageViewer(Image img)
{
this.pictureBox1.Image = img;
}
I suggest you use a BackgroundWorker for each algorithm and put the following code in the RunWorkerComplete event of the BackgroundWorker:
ImageViewer imgviewer1 = new ImageViewer((Image)e.Result);
imgviewer1.show();
Pass the result back in the DoWork event using e.Result = img;
I wouldn't recommend updating the UI from the DoWork event as it runs on another thread and you have to implement a bit of a hack/invoking, etc, to get it to work. It's much better to use the BackgroundWorker as it was intended. I.e. do the work and then return the result.
A background worker's dowork will execute on another thread than the UI thread. You can use the Dispatcher.Invoke to make sure some code that will upadte the UI will run on the UI thread.
I suggest you use a strategy of creating one ImageViewer instance, write code to set the Image in the internal PictureBox in this instance (either a method or a property).
Be sure and handle the RunWorkerCompleted Event for each BackGroundWorker call, and check for errors : see this for some detailed considerations of possible complications :
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.runworkercompleted.aspx
After each complete return from each of your successive BackGroundWorker calls, set the image in the single instance of the ImageViewer, and use 'Invalidate or whatever to force its screen redraw. best,
Does the initialize components called in the ImageViewer constructor before assignment.
精彩评论