I'm writing a program that reads a rather large text file (20,000+ lines) so it takes a few seconds to read the data in. So I decided to put up a couple messages, like "reading" and the file path. My problem is, even though I initiate the messages first, before reading the text file, the messages don't show until the file read is complete.
So is threading the area I need to go to? I vaguely remember using semaphores in C some years ago.
Nilanka DharmadasaPosted Dec 22, 2009, 12:49 AM
Yes. You have to use a separate thread for this. If you use backgroundworker component you can do it easily without worrying about threads.
Check my code.
If you find my answer helpful, please tick 'Do you like this answer' checkbox.
Sam HobbsPosted Dec 21, 2009, 5:27 PM
Note that a ProgressBar control is usually used to show progress.
To ensure responiveness of the UI, use a thread to read the file. Look at the System.Threading Namespace; especially the Thread Class.
A semaphore is just for signalling when something happens or needs to happen. You will need to have a way for the worker thread to communicate with the UI (main) thread. The .Net equivalent of a semaphore is the AutoResetEvent Class. In C#, you can lock objects being exchanged between threads; see Threading and especially Thread Synchronization to learn how it all fits together.