0

Let say I have an application with two forms, I'm in the main form and want to create the other form in a separate thread as its a status monitor for a very long process being executed in the main form. So I want change a text property of status monitor text box but the problem is that second form on another thread.

The code I'm using

 Dim _thread As Threading.Thread = New Threading.Thread(Sub()
                                    Using frm As New frmProcessMonitor
                                       Application.Run(frm)
                                           End Using
                                           End Sub)
      _thread.Start()

so please how to do that in this methodology.

Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
Ahmed Nazmy
  • 391
  • 1
  • 8
  • 20
  • 1
    Even if it may work (in some checked conditions) you should **never** mix different threads on for your UI. Just create a form and .Show() it (from main thread, long running process notifications can be BeginInvoke() to it). No need it start a message pump in parallel. Moreover right (if we can say right for this) is to create a Form then display it with ShowDialog(), Application.Run() does much more than that. – Adriano Repetti Jan 04 '14 at 09:07
  • Adriano, Please could you give me an explanation example. – Ahmed Nazmy Jan 05 '14 at 07:22
  • Adriano, What do you think if the second form has a progress bar with marquee style it will be freezed until calling thread finishes it's job. – Ahmed Nazmy Jan 05 '14 at 07:29
  • You have to do this: create status form in main thread, display with `Show()` (or `ShowDialog()` if modal). Run long time task in another thread (or `BackgroundWorker`). Progress bar won't be frozen and you'll be able to close status form (from the other thread) with a simple `BeginInvoke()` to its `Close()` method. – Adriano Repetti Jan 05 '14 at 07:44
  • For an example (using a `BackgroundWorker`) take a look to [this post](http://stackoverflow.com/questions/16901110/open-a-modal-form-from-a-background-thread-to-block-ui-thread-without-also-block). It's not _exactly_ same thing but it's one way to do it. – Adriano Repetti Jan 05 '14 at 07:47
  • As general rule just assume (can be relaxed only if you really know what you're doing) that: UI is single threaded and all controls must be created and used from that thread only (only exceptions are `Invalidate()`, `InvokeRequired`, `Invoke()`, `BeginInvoke()` and `EndInvoke()`). All messages (from other threads) must me dispatched. Few more details [here](http://stackoverflow.com/questions/10170448/how-to-invoke-an-ui-method-from-another-thread/10170699#10170699). – Adriano Repetti Jan 05 '14 at 07:53

1 Answers1

-1

I would look into System.Threading.Tasks.Task..

Something like

var frmCreationTask = Task.TaskFactory.StartNew(() => { /*Form generation code here */ }).ContinueWith(() => { //code to change the property } );

The code is in c# but should be easy enough to port to VB.

Flexo
  • 87,323
  • 22
  • 191
  • 272