Quote Originally Posted by mickey_pt View Post
Thanks boops boops

I know how to use the background worker and that's not what i intend to do...

The main form should remain blocked, it's a critical operation, so i need someway to tell the user that the operation it's running.
Like i wrote i can make the form the top most, but then it'll be over other applications and i don't like that.
The other option by setting the owner of the form, i can bypass the cross thread error, setting the CheckForIllegalCrossThreadCalls = False, before starting the new thread and then everything works fine... I just don't want to use this, but if there isn't any other way...

Thanks
A BackgroundWorker is perfect for this... if you use it properly. What you can do is design a form that has a ProgressBar on it and contains a BackgroundWorker. You then declare a constructor in that form that takes a DoWorkEventHandler delegate as an argument. In your main form, you write a DoWork event handler but without a Handles clause and when you create the progress form you pass a delegate for that method to it. That method is then used as the handler for the DoWork event of the BackgroundWorker in the progress form. E.g.
vb.net Code:
  1. Imports System.Threading
  2. Imports System.ComponentModel
  3.  
  4. Public Class MainForm
  5.  
  6.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  7.         Using dialogue As New WorkingDialogue(AddressOf BackgroundWorker_DoWork)
  8.             dialogue.ShowDialog()
  9.         End Using
  10.  
  11.         MessageBox.Show("Task complete")
  12.     End Sub
  13.  
  14.     Private Sub BackgroundWorker_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs)
  15.         Dim worker = DirectCast(sender, BackgroundWorker)
  16.  
  17.         For i = 1 To 100
  18.             Thread.Sleep(100)
  19.             worker.ReportProgress(i)
  20.         Next
  21.     End Sub
  22.  
  23. End Class
vb.net Code:
  1. Imports System.ComponentModel
  2.  
  3. Public Class WorkingDialogue
  4.  
  5.     Public Sub New(doWorkHandler As DoWorkEventHandler)
  6.         ' This call is required by the designer.
  7.         InitializeComponent()
  8.  
  9.         ' Add any initialization after the InitializeComponent() call.
  10.  
  11.         AddHandler Me.BackgroundWorker1.DoWork, doWorkHandler
  12.     End Sub
  13.  
  14.     Private Sub WorkingDialogue_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  15.         Me.BackgroundWorker1.RunWorkerAsync()
  16.     End Sub
  17.  
  18.     Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
  19.         Me.ProgressBar1.Value = e.ProgressPercentage
  20.     End Sub
  21.  
  22.     Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
  23.         Me.Close()
  24.     End Sub
  25. End Class