0

I would like to know how I could run the following loop in a way where it doesn't freeze the GUI, as the loop can take minutes to complete. Thank you.

For i = 0 To imageCount
         'code
Next
TheHippo
  • 61,720
  • 15
  • 75
  • 100
bad_wine
  • 67
  • 5
  • 1
    you can separate the client-logic from the processing, using ajax functionality. – Garrett Apr 16 '13 at 00:08
  • Duplicate: http://stackoverflow.com/questions/1216791/winform-application-ui-hangs-during-long-running-operation – roken Apr 16 '13 at 02:24

2 Answers2

3

The short answer is you run the loop on another thread. The long answer is a whole book and a couple of semesters at university, because it entails resource access conflicts and various ways of addressing them such as locking and queueing.

Since you appear to be using VB.NET I suggest you use the latest version of the .NET framework and take advantage of Async and Await, which you can learn about from MSDN.

These keywords implement a very sophisticated canned solution that will allow you to achieve your goals in blissful ignorance of the nightmare behind them :)


Why experienced parallel coders would bother with async/await

Standout features of async/await are

  • automatic temporary marshalling back to the UI thread as required
  • scope of exception handlers (try/catch/finally) can span both setup and callback code
  • you write what is conceptually linear code with blocking calls on the UI thread, but because you declare calls that block using "await", the compiler rewrites your code as a state machine makes the preceding points true

Linear code with blocking calls is easy to write and easy to read. So it's much better from a maintenance perspective. But it provides an atrocious UX. Async/await means you can have it both ways.

All this is built on TPL; in a quite real sense it's nothing more than a compiler supported design pattern for TPL, which is why methods tagged as async are required to return a Task<>. There's so much to love about this, and no technical downside that I've seen.

My only concern is that it's all too good, so a whole generation will have no idea how tall the giants on whose shoulders they perch, just as most modern programmers have only dim awareness of the mechanics of stack frames in call stacks (the magic behind local variables).

Peter Wone
  • 17,965
  • 12
  • 82
  • 134
  • For .NET 4.0 - better use tasks. – Victor Zakharov Apr 16 '13 at 00:15
  • I could have suggested forms of thread management that work in even older versions of the framework. I didn't, because the original question implies a total ignorance of parallel execution and the problems surrounding it. The only *easy and safe* answer is async/await. Luis has given him BackgroundWorker but I predict cross-thread access grief within the half-hour, followed by deadlock problems over the next week. Then there's the question of how to write an exception handler that spans a callback - trivial with async/await. – Peter Wone Apr 16 '13 at 01:17
  • I did not mean to offend your answer in any way. Just a thought for the OP to look into TPL (task parallel library). I have not yet looked into async, simply because it's .NET 4.5, which will not work in XP and 2003, and most of our clients are still using those OS. So cannot comment on that, even though I heard great rumors about it, particularly that it also spans into web world, seemlessly translating into ajax-style updates. – Victor Zakharov Apr 16 '13 at 01:27
  • 1
    @Neolisk - Thank you for your courtesy. See extended answer for information you may find of interest. – Peter Wone Apr 16 '13 at 23:34
2

You can run the loop on a separate thread. Read about using BackgroundWorker here http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

SOfanatic
  • 5,523
  • 5
  • 36
  • 57
  • If you're going to manage threading yourself then do yourself a favour and read this article that covers surrounding issues and how to deal with them: http://www.albahari.com/threading/ – Peter Wone Apr 16 '13 at 01:31