ImageEn for Delphi and C++ Builder ImageEn for Delphi and C++ Builder

 

ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
Forum membership is Free!  Click Join to sign-up
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 Modifying images in a TImageEnMView from threads?
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

PeterPanino

933 Posts

Posted - Jan 04 2017 :  20:12:45  Show Profile  Reply
For speed reasons, I need to MODIFY many images (custom bitmap-processing) inside a TImageEnMView from multiple parallel threads.

Since VCL controls are not thread-safe, this seems to be an impossible thing.

Or is there a feature to modify many images in a TImageEnMView inside a loop in a thread-safe way?

PeterPanino

933 Posts

Posted - Jan 05 2017 :  08:48:25  Show Profile  Reply
For example, the TImageEnMView class could implement a specific event or similar (both for each single image and for the whole control) where it is safe to modify images in the control from parallel threads.
Go to Top of Page

w2m

USA
1990 Posts

Posted - Jan 05 2017 :  09:31:55  Show Profile  Reply
Modification of TImageEnView, TImageEnVect and TImageEnMView is not necessary to process images in a thread. All three components are "thread safe" if the thread is implemented correctly. See the Threaded Image Processing Demo at http://www.imageen.com/ieforum/topic.asp?TOPIC_ID=1446&whichpage=1.

"To update your application's user interface - main thread - from a secondary thread you need to call the Synchronize method - a thread-safe method that avoids multi-threading conflicts that can arise from accessing object properties or methods that are not thread-safe, or using resources not in the main thread of execution." It is critical to get or set the TIEBitmap in a Synchronize method to be "thread safe".

The key to achieve correct threading is to create the TImageEnProc component and TIEBitmap in the Execute method of the thread and do not access the TImageEnMView or any other component on the form unless the access is done in a Synchronize method.

What I do is get the TIEBitmap from TImageEnMView in a synchronize method at the beginning of the thread's execute method, copy the image to a locally created TIEBitmap, process the image with a TImageEnProc created in the thread and attached to the local TIEBitmap. Do the processing in a non-synchronized procedure in the thread. If you process the TIEBitmap using Synchronize the processing will not be any faster than processing the main thread and you will not speed up your application. After processing the local TIEBitmap, at the end of the execute method, copy the local TIEBitmap back to the TImageEnMView in a synchronize method.

As long as you only access the TImageEnMView or any other component inside of a synchronize method, the thread will be "thread safe". It is a bit complicated to describe how to do this unless you understand threading completely and study some examples. Developing a "safe" thread is not simple and is much different then handling components at the project level.

It took considerable time to learn how to work with threading with ImageEn, but once you understand basic threading principals, processing images in a thread will be successful. Threaded processing is much more difficult than general programming so study the demo and if you have problems let me know where the problem exists and I'll assist you. Once you get the thread implemented correctly, the thread will be "thread safe" and your processing will not slow down the application. The speed of the thread will depend on how well you implement the thread and the speed of the processing that you undertake.

Bill Miller
Adirondack Software & Graphics
Email: w2m@hughes.net
EBook: http://www.imageen.com/ebook/
Custom Commercial ImageEn Development
Go to Top of Page

PeterPanino

933 Posts

Posted - Jan 05 2017 :  11:14:33  Show Profile  Reply
Bill, thank you very much for the good explanation. I will try the demo.

The main problem is that I need to process very many images in a TImageEnMView in the shortest possible time.

So far I had some success with the OmniThreadLibrary, as the processing occurs in a loop in one single worker thread ("Async"). But as soon as multiple threads are processing (each thread different images of course) in the same TImageEnMView the problems start to arise.

So a question before I look at the demo: Do you make your thread processing of images in a TImageEnMView from one SINGLE thread or from MULTIPLE threads? I.e., do you process many images in a TImageEnMView in a loop?
Go to Top of Page

w2m

USA
1990 Posts

Posted - Jan 05 2017 :  11:18:28  Show Profile  Reply
One thing you will notice is that the demo does not use TImageEnMView or process multi-bitmap. You will have add a TImageEnMView and change the demo accordingly to process multibitmaps. But the demo provides you a starting point.

Bill Miller
Adirondack Software & Graphics
Email: w2m@hughes.net
EBook: http://www.imageen.com/ebook/
Custom Commercial ImageEn Development
Go to Top of Page

w2m

USA
1990 Posts

Posted - Jan 05 2017 :  16:50:49  Show Profile  Reply
 
So a question before I look at the demo: Do you make your thread processing of images in a TImageEnMView from one SINGLE thread or from MULTIPLE threads? I.e., do you process many images in a TImageEnMView in a loop?

The demo does not support multiple images.

I took a few minutes today and modified the original demo to include a TImageEnMView to process all the images. The demo has been simplified and at present only includes auto enhance, but it should put you in a better place for threading multi bitmap processing.

There is only one thread, but the application remains active when running the processing thread. All of the images are processed in a loop in the thread. If you need faster processing with large images, then you should change the code to process both thumbnails or full images that shows a preview then add a "Apply" button similar to that in the original demo to process full sized images.

Processing a large number of full size images in a thread is inherently going to take some time, depending on the number of images and the dimensions of the images.

This revised demo has not been extensively tested, but it should lead you the direction you want to go.

attach/w2m/201715165028_ThreadedMultiBitmapProcessing.zip
87.77 KB

Bill Miller
Adirondack Software & Graphics
Email: w2m@hughes.net
EBook: http://www.imageen.com/ebook/
Custom Commercial ImageEn Development
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
Jump To: