• 0

[C#] Foreach freezing application


Question

I have a very long foreach loop that searches the hard drive. However, once the application enters the foreach loop, you can't interact with the application. I want to create a progress bar, but since the application is frozen up until the loop is finished, it's pretty much purposeless. How can I use foreach loops without freezing up the application?

Link to comment
https://www.neowin.net/forum/topic/582558-c-foreach-freezing-application/
Share on other sites

10 answers to this question

Recommended Posts

  • 0

These long-running threads seem to lag down my computer. Also, when I close the application, Visual Studio still shows it's running (the pause and stop buttons are still active). It appears it's still running the function as Visual Studio still throws exceptions eventually when I search a directory I do not have access to (several minutes after I already closed the application). How can I fix this?

Edited by WiredUp
  • 0

If it's just for the GUI, use a delegate to update the GUI before the thread closes, instead of getting the GUI thread to do the work when the thread exits.

Alternatively, you could try doing something with the Thread.isAlive property.

You could also use a boolean to determine when the thread completes...

i.e. Declare a boolean at class level, then when the thread has finished processing the function that was in the thread should set the Boolean to TRUE. Other functions could then check this boolean.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.