Maybe it’s a novice question, but I a am a novice. I have a program which does a lengthy task, while it does it, the form stays frozen, if its minimized it can't be maximized Etc. How can I fix that. Also, the program runs in a loop, and in each iteration prints info to a list box, line by line. Problem is, the list box doesn't get written, untill after the whole loop finishes. How can I correct that.
Asked
Active
Viewed 63 times
0
-
1Look into `BackgroundWorker`. – Blorgbeard Jul 21 '15 at 21:16
-
It's because you're doing stuff on the UI thread. You can't interact with the UI while it's busy doing your calculations. You need to do them in a separate thread (i.e. a Task or BackgroundWorker). Plenty of questions about that on SO and resources on Google. – Pierre-Luc Pineault Jul 21 '15 at 21:18
-
1possible duplicate of [WinForm Application UI Hangs during Long-Running Operation](http://stackoverflow.com/questions/1216791/winform-application-ui-hangs-during-long-running-operation) – Pierre-Luc Pineault Jul 21 '15 at 21:19
-
This is a use for threading (sometimes called tasks). This will allow your long task to be done while the UI is still being processed (and therefore responds) on the initial thread. Here is a threading tutorial from msdn https://msdn.microsoft.com/en-us/library/aa645740(v=vs.71).aspx If you have questions once you get started on threads please post them and we will help. – Doug E Fresh Jul 21 '15 at 21:22
-
big thanks i will look into it – burech Jul 22 '15 at 03:16
1 Answers
2
You can use either a Thread or a BackgroundWorker to do your work.
Calling lengthy code from the UI will block the UI thread and the app 'freezes'.

stalem
- 219
- 1
- 13