I have a program running several threads simultaneously. Each thread connects a database and transfers data from one table to another. Now I want to assign a panel to each thread in the MainForm so I can change color of the Panel to green if the connection is successful or to red if it’s broken after a number of retries.
So how can I tell a thread which Panel is its own Panel?
When you create a thread class, add a variable to store panel id:
For every thread create a panel and set it’s Tag value to this Id:
When your thread needs to update data on panel, it should send a specially defined message to the main form:
In wParam of this message you pass PanelId:
In MainForm you catch this message, find the panel and update it:
Same with WmDisconnected.
The important thing here is that you CANNOT and NEVER should try to update visual components from threads other than the main thread. If you need to update user controls, you should post messages to the main form and create handler procedures, like in this example. These handler procedures will then be automatically called from the main thread.