Reputation: 111
I have a program written in C#, it does a lot of read from files from the disk and then output a few lines of text, not many possibly 100 at tops into a text files.
The program freezes about half way through, I'm new to c# and programming in general from research it seems that I need to use a separate thread from the one that controls the form. Two questions,
Should I used a new thread for the read and a new one for the write or just one for the read function?
What would be the best way of doing this?
I hope this makes sense and I really appreciate your help!
Upvotes: 1
Views: 1377
Reputation: 47520
BackgroundWorker is the best thing to use here. Here is a good tutorial which describe all of its events and properties.
In brief this is what you should do.
The most important thing to keep in mind is both ProgressChanged and RunWorkerCompleted events are executing in the main thread where you can update the UI by using the background worker results. Only the DoWork event executing in the Background thread.
Upvotes: 2
Reputation: 9857
If the full read must occur before the write, you only need one thread - otherwise, you can use two.
Check this out for a threading example: http://msdn.microsoft.com/en-us/library/aa645740(v=vs.71).aspx
Upvotes: 0