Question Best way to update UI in busy multithreaded app

doovers

Member
Joined
Aug 3, 2014
Messages
5
Programming Experience
1-3
I have been developing a machine polling (windows forms) application for some time now and have successfully implemented all the functionality that is required. However I have faced one significant problem from the outset in that I have been unable to deliver a smooth user experience as far as the user interface is concerned. I have tried many different approaches (to the point where I have lost track of everything I have tried) to prevent the UI from locking but nothing has worked.

My application essentially works as follows:

In a machine shop, there are 100 machines. 50 of which I can poll directly and gather the required information. The other 50 cannot be polled in this way and are configured to spit out a text file at a specific intervals containing machine information. I have 2 classes, one for each machine type which either polls the machine or parses a text file on a network to obtain the data. I have a datagridview bound to a MySQL datasource on the network with a list of IP addresses or file paths for each machine and when the user starts the polling, a new thread is spawned for each machine and the tableadapter is updated with a delegate sub on the UI thread.

This has not yielded an acceptable UX so I want to take a few steps back before I keep battling on with this since I'm a novice programmer and do not know whether I'm way off track with my approach.

1. Is my setup the best way to tackle this problem?
2. If not, what would be a better way?
3. Is there any advantage to using a WPF app?
4. Should I use some sort of local class/collection etc. between the datasource and the UI and how would I implement that?

I would appreciate any advice or tips you might have!

Thanks!
 
If you aren't getting an acceptable UX then no, your setup is not optimal. For one thing, if your grid is bound then you can't modify its data source on a secondary thread. What you should do is package up your data as neatly as possible on the secondary thread and then update the data source on the UI thread last thing.
 
Hi and welcome to the Forum,

Without knowing your program your theoretical logic sounds fine but it is obviously flawed in some way if the UI thread is freezing as you seem to suggest in your post.

It would seem to me that you have not implemented the Threading capabilities of your application correctly to make sure that the UI thread stays responsive.

To that end, and as well as using the BackgroundWorker, since you are using .NET4.5 I would suggest that you have a look into the Async and Await technologies that have been introduced with .NET4.5

Asynchronous Programming with Async and Await (C# and Visual Basic)

This is a great way to write inline code that implements additional Tasks that are completed on worker threads therefore maintaining the responsiveness on the UI thread.

Hope that helps.

Cheers,

Ian
 
Back
Top