drcelus
drcelus

Reputation: 479

Proper way of updating the UI controls

I am developing a software that needs to constantly read data from a serial port and show the values to the user.

So I have a thread with a loop that constantly reads the serial port. Which is the proper way of updating the controls of the Form in order to have a smooth user experience ? So far I have found that a timer won't work because the reading thread blocks it's execution and if I create a new thread with a Thread::Sleep(300) inside the thread doesn't work either.

BTW, I am new in c++/cli as you may already have noticed.

Upvotes: 0

Views: 55

Answers (1)

Aram Tchekrekjian
Aram Tchekrekjian

Reputation: 935

Consider using the BackgroundWorker Component in C++/CLI. The BackgroundWorker as defined in msdn:

The BackgroundWorker class allows you to run an operation on a separate, dedicated thread. Time-consuming operations like downloads and database transactions can cause your user interface (UI) to seem as though it has stopped responding while they are running. When you want a responsive UI and you are faced with long delays associated with such operations, the BackgroundWorker class provides a convenient solution.

Put your looping code and your code that communicates with the UI controls within the DoWork event.

Check this tutorial that explains in steps how to use BackgroundWorker in C++/CLI

C++/CLI Background Worker Tutorial

But before stepping into the tutorial, I recommend you to learn about the background worker in .net so you can understand how it works.

BackgroundWorker Class

Upvotes: 1

Related Questions