# Display most recent image generated by multiple processing threads running in parallel

**URL:** <https://forum.commonvisionblox.com/t/display-most-recent-image-generated-by-multiple-processing-threads-running-in-parallel/128>\
**Category:** C-style API\
**Tags:** display, activex, multithreading, mfc\
**Created:** [May 30, 2017, 12:04pm UTC](https://forum.commonvisionblox.com/t/display-most-recent-image-generated-by-multiple-processing-threads-running-in-parallel/128 "2017-05-30T12:04:00Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![VCmonkey](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/vcmonkey/32/91_2.png) [@VCmonkey](https://forum.commonvisionblox.com/u/VCmonkey)\
**Post date:** [May 30, 2017, 12:04pm UTC](https://forum.commonvisionblox.com/t/display-most-recent-image-generated-by-multiple-processing-threads-running-in-parallel/128/1 "2017-05-30T12:04:00Z")

</div>

I want to build up an application for a multiple-camera-system. The application provides multiple asynchronous image processing jobs (one for each camera) that run in parallel. The display in the GUI should always show the processed image of the most recently finished image processing job. I want to use the CVB display class.

Is it save to call the display method refresh() inside one of the asynchronous image processing threads?

Is it possible to refresh the display as soon as a processing thread has finished his job, although the display object is instanced in a separate thread (e.g. a GUI thread)?

---

<div class="post-metadata">

**Author:** ![parsd](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/parsd/32/7_2.png) [@parsd](https://forum.commonvisionblox.com/u/parsd)\
**Post date:** [May 31, 2017, 6:11am UTC](https://forum.commonvisionblox.com/t/display-most-recent-image-generated-by-multiple-processing-threads-running-in-parallel/128/2 "2017-05-31T06:11:41Z")

</div>

Hi @VCmonkey,

do you mean the _Display ActiveX Control_? If yes, then calling the `Refresh()` method on it is not safe from non-UI threads as ActiveX controls are COM controls and UI elements and thus life in the [Single Threaded Apartment](https://msdn.microsoft.com/en-us/library/windows/desktop/ms680112(v=vs.85).aspx). So correctly calling that function depends on your programming language and used framework. Are you coding in C/C++? Which UI-Framework do you use?

How do you acquire the images? Do you use the DLL functions `G2Grab`, `G2Wait`, and `G2Freeze` in your own thread or the _Image ActiveX Control_?

---

<div class="post-metadata">

**Author:** ![VCmonkey](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/vcmonkey/32/91_2.png) [@VCmonkey](https://forum.commonvisionblox.com/u/VCmonkey)\
**Post date:** [May 31, 2017, 9:10am UTC](https://forum.commonvisionblox.com/t/display-most-recent-image-generated-by-multiple-processing-threads-running-in-parallel/128/3 "2017-05-31T09:10:18Z")

</div>

Hello @parsd ,

thank you for your fast reply 🙂!

Correct, I mean the Display ActiveX Control.  
I am coding in C++ and my application is based on the _Visual C++ MFC_ example project that I found in the Programming with CVB section of the CVB documentation. I am using the _Visual C++ OCX Wrappers_ for _64 bit_.

The DLL functions _G2Grab_, _G2Wait_ and _G2Freeze_ are called in separate image processing threads. Each thread loads the CVB driver, changes the port and then starts grabbing and processing in a loop.

In order to preserve thread safety, I will have to find an alternative approach to update the display instead of calling _Refresh()_ inside the processing threads.

Do you have any suggestions?

---

<div class="post-metadata">

**Author:** ![parsd](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/parsd/32/7_2.png) [@parsd](https://forum.commonvisionblox.com/u/parsd)\
**Post date:** [May 31, 2017, 1:00pm UTC](https://forum.commonvisionblox.com/t/display-most-recent-image-generated-by-multiple-processing-threads-running-in-parallel/128/4 "2017-05-31T13:00:52Z")

</div>

The good old _MFC_… 😅 Sadly it has no built in way to execute something on the UI thread. If you just want to refresh the simple solution below would work.

> **Full implementation instructions...**
>
> I assume you do all the work in a `CWnd` derived class, like a dialog.
> 
> Register Window Message by putting this in the private section of your class declaratoin or somewhere above the implementation in the cpp-file:
> 
> ```auto
> enum
> {
> WM_ASYNC_REFRESH = WM_USER
> // other custom messages are entered here...
> };
> 
> ```
> 
> Put the custom message handler method declaration somewhere in the class’ private section:
> 
> ```auto
> private:
> afx_msg LRESULT OnAsyncRefresh(WPARAM wParam, LPARAM lParam);
> 
> ```
> 
> and add the implementation to your cpp-file (exchange `CYourClass` with your actual class name and m\_cvDisp with your actual Display OCX name):
> 
> ```auto
> LRESULT CYourClass::OnAsyncRefresh(WPARAM wParam, LPARAM lParam)
> {
> UNREFERENCED_PARAMETER(wParam);
> UNREFERENCED_PARAMETER(lParam);
> 
> m_cvDisp.Refresh();
> }
> 
> ```
> 
> Finally add the method to the MFC’s message map (search for `BEGIN_MESSAGE_MAP` in your class’ cpp file):
> 
> ```auto
> BEGIN_MESSAGEMAP(CYourClass, CWnd)
> // many handlers...
> ON_MESSAGE(WM_ASYNC_REFRESH, OnAsyncRefresh)
> END_MESSAGE_MAP()
> 
> ```
> 
> Now everything is prepared and you can either call
> 
> ```auto
> SendNotifyMessage(WM_ASYNC_REFRESH, 0, 0);
> 
> ```
> 
> on your `CYourClass` object to refresh asynchronously if called from a non-UI-thread or synchronously when called from the UI-thread. If you always want to have it synchronously call
> 
> ```auto
> SendMessage(WM_ASYNC_REFRESH);
> 
> ```
> 
> on your `CYourClass` object. This will **wait** for the `Refresh()` being executed. I you never want to wait, call
> 
> ```auto
> PostMessage(WM_ASYNC_REFRESH);
> 
> ```
> 
> on `CYourClass` object. This always refreshes asynchronously, even when you are on the UI thread.
> 
> Asynchronous messages are fire and forget here (you cannot wait for it).

---

<div class="post-metadata">

**Author:** ![VCmonkey](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/vcmonkey/32/91_2.png) [@VCmonkey](https://forum.commonvisionblox.com/u/VCmonkey)\
**Post date:** [May 31, 2017, 1:33pm UTC](https://forum.commonvisionblox.com/t/display-most-recent-image-generated-by-multiple-processing-threads-running-in-parallel/128/5 "2017-05-31T13:33:15Z")

</div>

Thanks parsd!

Your detailed implementation instructions are a good start 😀.

---

<div class="post-metadata">

**Author:** ![illusive](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/illusive/32/61_2.png) [@illusive](https://forum.commonvisionblox.com/u/illusive)\
**Post date:** [May 31, 2017, 2:14pm UTC](https://forum.commonvisionblox.com/t/display-most-recent-image-generated-by-multiple-processing-threads-running-in-parallel/128/6 "2017-05-31T14:14:05Z")

</div>

Sorry to be a wise-ass here 😜, but there’s a simpler solution for this particular use-case. If the goal is only to refresh a display that already has the correct image handle set, it would be sufficient to call

```cpp
SendMessage(m_cvDisp.GethWnd(), WM_PAINT, 0, 0);

```

or

```cpp
PostMessage(m_cvDisp.GethWnd(), WM_PAINT, 0, 0);

```

in your non-UI thread to update the display control’s content.

However, @parsd provided the perfect entry point for a case where a simple `m_cvDisp.Refresh()` is not sufficient because e.g. you need to call `m_cvDisp.SetImage()` because your processing generates a new image rather than updating the content of an existing image.

In that case, use the code from @parsd and send the new image handle through the `LPARAM` of your custom window message and modify the `OnAsyncRefresh` as follows:

```cpp
LRESULT CYourClass:OnAsyncRefresh(WPARAM wParam, LPARAM lParam)
{
  UNREFERENCED_PARAMETER(wParam);
  m_cvDisp.SetImage(reinterpret_cast<intptr_t>(lParam));
}

```

(well, maybe we should rename it to something like `OnAsyncSetImage` in that case, but I guess you get what I am up to…)

The code from @parsd can also be easily extended to other use cases that are forbidden from within a non-UI thread like adding labels or overlay plugins to a display etc.

Generally speaking, doing anything that alters the state of the user interface from withing a non-UI thread is a no-go (some things might work most of the time, but effectively as soon as you touch your UI from a non-UI thread you enter the territory of _undefined behavior_).

---

<div class="post-metadata">

**Author:** ![parsd](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/parsd/32/7_2.png) [@parsd](https://forum.commonvisionblox.com/u/parsd)\
**Post date:** [May 31, 2017, 2:50pm UTC](https://forum.commonvisionblox.com/t/display-most-recent-image-generated-by-multiple-processing-threads-running-in-parallel/128/7 "2017-05-31T14:50:25Z")

</div>

@illusive: I can also nitpick 😜:

If you just do

```auto
m_cvDisp.SetImage(reinterpret_cast<intptr_t>(lparam));

```

you won’t see a refresh if the image handle did not change. So it should be

```auto
auto hNewImage = reinterpret_cast<intptr_t>(lparam);
if(m_cvDisp.GetImage() != hNewImage)
  m_cvDisp.SetImage(hNewImage);
else
  m_cvDisp.Refresh();

```

Also @illusive: why did you choose the `lparam`? Both `WPARAM` and `LPARAM` are pointer sized and I would prefer the first parameter. So I could write

```auto
PostMessage(processedImage);

```

instead of

```auto
PostMessage(0, processedImage);

```

---

<div class="post-metadata">

**Author:** ![illusive](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/illusive/32/61_2.png) [@illusive](https://forum.commonvisionblox.com/u/illusive)\
**Post date:** [May 31, 2017, 2:51pm UTC](https://forum.commonvisionblox.com/t/display-most-recent-image-generated-by-multiple-processing-threads-running-in-parallel/128/8 "2017-05-31T14:51:51Z")

</div>

Getting a bit extreme, are we… 😉

But the point about the unchanged handle is actually valid…

---

<div class="post-metadata">

**Author:** ![parsd](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/parsd/32/7_2.png) [@parsd](https://forum.commonvisionblox.com/u/parsd)\
**Post date:** [May 31, 2017, 3:09pm UTC](https://forum.commonvisionblox.com/t/display-most-recent-image-generated-by-multiple-processing-threads-running-in-parallel/128/9 "2017-05-31T15:09:01Z")

</div>

> **To sum the solution up from @illusive and me**
>
> Put this in the private section of your `CWnd` derived class:
> 
> ```auto
> private:
> afx_msg LRESULT OnAsyncRefresh(WPARAM wParam, LPARAM lParam);
> 
> ```
> 
> and that in the cpp-file:
> 
> ```auto
> LRESULT CYourClass::OnAsyncRefresh(WPARAM wParam, LPARAM lParam)
> {
> UNREFERENCED_PARAMETER(lParam);
> intptr_t hNewImage = reinterpret_cast<intptr_t>(lparam);
> if(m_cvDisp.GetImage() != hNewImage)
> m_cvDisp.SetImage(hNewImage);
> else
> m_cvDisp.Refresh();
> }
> 
> ```
> 
> Search for `BEGIN_MESSAGE_MAP` in the class’ cpp and add
> 
> ```auto
> enum
> {
> WM_ASYNC_REFRESH = WM_USER
> // other custom messages are entered here...
> };
> 
> ```
> 
> before it and
> 
> ```auto
> BEGIN_MESSAGEMAP( ... )
> // many handlers...
> ON_MESSAGE(WM_ASYNC_REFRESH, OnAsyncRefresh)
> END_MESSAGE_MAP()
> 
> ```
> 
> into it (the `ON_MESSAGE`-part).
> 
> Then you can call
> 
> ```auto
> PostMessage(WM_ASYNC_REFRESH, myProcessImage);
> 
> ```
> 
> to asynchronously set/refresh the image in the display.

@VCmonkey, if you like the solution and it solved your problem, would you please mark an entry as the solution? Thank you 👍

---

<div class="post-metadata">

**Author:** ![illusive](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/illusive/32/61_2.png) [@illusive](https://forum.commonvisionblox.com/u/illusive)\
**Post date:** [June 7, 2017, 2:21pm UTC](https://forum.commonvisionblox.com/t/display-most-recent-image-generated-by-multiple-processing-threads-running-in-parallel/128/10 "2017-06-07T14:21:31Z")

</div>

Hi @VCmonkey! If one of the suggestions turned out to be a viable solution to your problem it might be helpful for future visitors if you could mark it as _the_ solution 😉

---

<div class="post-metadata">

**Author:** ![VCmonkey](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/vcmonkey/32/91_2.png) [@VCmonkey](https://forum.commonvisionblox.com/u/VCmonkey)\
**Post date:** [June 14, 2017, 5:43am UTC](https://forum.commonvisionblox.com/t/display-most-recent-image-generated-by-multiple-processing-threads-running-in-parallel/128/11 "2017-06-14T05:43:35Z")

</div>

Hi @parsd and @illusive. Thank you for your help 😀. I am currently working on another topic. I will give you feedback as soon as I switch back to this topic.

---

<div class="post-metadata">

**Author:** ![VCmonkey](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/vcmonkey/32/91_2.png) [@VCmonkey](https://forum.commonvisionblox.com/u/VCmonkey)\
**Post date:** [June 21, 2017, 5:57am UTC](https://forum.commonvisionblox.com/t/display-most-recent-image-generated-by-multiple-processing-threads-running-in-parallel/128/12 "2017-06-21T05:57:03Z")

</div>

Hi @parsd and @illusive. I went back coding and fortunately with your help it worked out for me.

All images generated by the processing thread have the same dimensions.  
So, I will use `SetImage` in the GUI to assign an IMG to the display.  
Then, the processing threads will update the content of that image, but will not change the number of planes neither the dimensions.

The GUI thread calls

```cpp
      IMG processedImage;
      CreateGenericImage(1, 640, 480, false, processedImage);
      m_cvDisp.SetImage(reinterpret_cast<intptr_t>(processedImage));

```

once.

Each processing thread overwrites the content of the image and refreshes the display

```cpp
    Processing(…, processedImage);
    PostMessage(m_cvDisp.GethWnd(), WM_PAINT, 0, 0);

```
