Accessing System.Drawing.Bitmap from GTK # Thread throws Object An exception is currently being used elsewhere

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); //Long image processing } 

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); }); // cannot directly convert Bitmap to Pixbuff,so doing this 
+6
source share
1 answer

The problem is that you are processing the image in two places (two streams) at the same time, and the image operation in .Net (GDI) does not allow this. Because you did not provide a lot of information, I just guess here.

When manipulating bitmap images in GDI, behind the scenes are bitmap data that needs to be locked and unlocked. This mechanism simply makes the image available in memory for reading / writing. But AFAIK, when you block a BitmapData that is already locked, you get a similar exception: System.InvalidOperationException, the Bitmap scope is already locked.

It seems to me that you get such an error, but in other words, because you obviously do not block bits of bitmart data. GDI will simply tell you: "I need to lock the bit bits of the data, but I can not, because the object is already in use (locked) elsewhere."

The solution here may be to try to use a synchronized bitmap (where bit lock can be enabled) between the stream whenever this can happen. Thus, you can use the keyword lock or a similar mechanism:

So try something similar to the following:

 private static object _imageLock = new object(); static void LargeComputation () { lock(_imageLock) { image=new Bitmap(backupimage); //Long image processing ... } } static void AnotherImageOperationSomewhereElse() { lock(_imageLock) { //Another image processing on backupImage or something derived from it... } } 
+3
source

Source: https://habr.com/ru/post/980601/


All Articles