I am trying to manipulate an image using system.drawing in GTk #. I want the user interface to update the image on the screen as soon as the user has updated the text box. To implement this, I tried to use a background worker from winforms, it worked, but when the text field is updated at a faster speed, the application gets stuck without errors.
So, I looked at multithreading in GTK here http://www.mono-project.com/docs/gui/gtksharp/responsive-applications/ and created a thread.
void textboxchanged() { Thread thr = new Thread (new ThreadStart (ThreadRoutine)); thr.Start (); } static void ThreadRoutine () { LargeComputation (); } static void LargeComputation () { image=new Bitmap(backupimage);
It works poorly than the working background casting an object that is currently being used elsewhere here. image=new Bitmap(backupimage); when the speed of input into the text box is even a little fast. What am I doing wrong?
Update 1:
I do not process the same image using two different threads that perform 2 different operations at the same time. I call a thread that does the same operation until the old thread completes. As a background worker, I need a way to check if the old thread has finished before starting a new one. Basically, what is looking for is a way to check if an instance of the same thread is running. In winforms, I used if(backgroundworker.isbusy==false) then do stuff
Update 2
Decreased Solution
As @voo suggested Replacing the global bitmap helped solve the problem. What I did, instead of using a global bitmap. I created a global string (file name). Now I am using img=new Bitmap(filename) . Testing is quick because I cannot have an error. Thus, to update the GUI, I used the call as suggested here mono-project.com/docs/gui/gtksharp/responsive-applications/ . The fact is that an error does not occur, and the image is updated, but when the input operation is fast enough, there is a wait. Productivity has deteriorated. This was not the case with the background worker. Is there a way to improve performance.
At the end of a great image processing method, I added this to update the GUI
Gtk.Application.Invoke (delegate { MemoryStream istream=new MemoryStream(); img.Save (istream, System.Drawing.Imaging.ImageFormat.Png); istream.Position = 0; workimagepixbuff = new Gdk.Pixbuf (istream); image1.Pixbuf = workimagepixbuff.ScaleSimple (400, 300, Gdk.InterpType.Bilinear); });