dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Real Time reading and drawing

  1. #1
    Join Date
    Sep 2015
    Posts
    2

    Real Time reading and drawing

    Hello there,
    I am looking for the best Way/Strategy to "Continuously read data from the HW in real-time and draw some GUI-diagram in Parallel". Performance would be a big matter here since the graph has to be drawn in real-time.

    Its just coming to my mind about 1) creating 1 worker-thread for reading process, and 2) another for GUI drawing.
    But I am looking for perfection and suggestions here.

    1) Is there any specific Design-Pattern that would fit this?. If so, please suggest.
    2) Whats the best way(container) to read data by the worker-thread and keep it for the GUI-thread to process. FIFO? or some other class?

    Thanks in advance for your suggestion and advice.

  2. #2
    Join Date
    Nov 2003
    Posts
    1,902

    Re: Real Time reading and drawing

    Is there anything else that needs to be done to the data, or can the GUI thread discard the data after processing it?

    gg

  3. #3
    Join Date
    Sep 2015
    Posts
    2

    Re: Real Time reading and drawing

    Thanks for the reply.
    Yes, preferably the data need to be saved for some other purpose later, even after the GUI is been drawn.

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Real Time reading and drawing

    1) windows is not a real time system. You have to allow for frames to get dropped, and this need becomes highher the higher the framerate you need to go. This shouldn't be a problem because if you're updating faster than say 5 frames per second, nobody will notice an intermediate flash and 'recognize' it. If it's about smooth motion, then anything above 25-30frames is enough to fool the human eye into thinking it's continuous.

    2) simple solution is you have 3 memory DC's with an associated bitmap.
    You need 3 to:
    - A one for the image that is currently pending for display or in the process of being displayed
    - B one for the data thread to create a new image in
    - C one free for the data thread to swap over if data is arriving faster than the display can handle.

    The data thread fetches data, puts it on the bitmap, swaps B with C (thread safe), informs the main thread an image is ready
    GUI thread waits for an update notification, swaps A with B (thread safe), paints A.

    if the data thread gets so much data that it can't process it to a bitmap fast enough, you need an extra layer, and an extra thread. with databuffers swapping between the data reader thread, and the bitmap calculation thread.

  5. #5
    Join Date
    Nov 2003
    Posts
    1,902

    Re: Real Time reading and drawing

    Here is an example of a simple data structure for sending data from one thread to another:
    http://cboard.cprogramming.com/windo...tml#post966940
    Notice the queue_MT::enqueue(It first, It last) and queue_MT:equeue_all() methods. By adding to the queue in batches, and consuming the entire queue at once, lock contention can be significantly reduced. The GUI thread could then pipeline the data to a third thread for writing to a file. Or use overlapped IO to write the data to file and render it at the same time in the GUI thread.

    gg

  6. #6
    Join Date
    Mar 2015
    Location
    Bangladesh
    Posts
    18

    Re: Real Time reading and drawing

    Idea isn't bad. Great thinking as usual. carry on!

  7. #7
    Join Date
    Mar 2015
    Location
    Bangladesh
    Posts
    18

    Re: Real Time reading and drawing

    Quote Originally Posted by FrankStar View Post
    Thanks for the reply.
    Yes, preferably the data need to be saved for some other purpose later, even after the GUI is been drawn.
    Right! Data should be save for future purpose.

  8. #8
    Join Date
    Sep 2016
    Posts
    1

    Re: Real Time reading and drawing

    any idea for body structure sketch.

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,728

    Re: Real Time reading and drawing

    Quote Originally Posted by JamesHoward View Post
    any idea for body structure sketch.
    ...and this relates to the OP how?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.1.6)

  10. #10
    Join Date
    Sep 2016
    Location
    pune
    Posts
    7

    Re: Real Time reading and drawing

    Its good article shoews all detail for multithreading.

  11. #11

    Re: Real Time reading and drawing

    I'm not aware of another method that tells you that you can have exclusive ownership of a file. Trying again a couple times a second won't hurt.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)