
Originally Posted by
mickey_pt
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:
Imports System.Threading
Imports System.ComponentModel
Public Class MainForm
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Using dialogue As New WorkingDialogue(AddressOf BackgroundWorker_DoWork)
dialogue.ShowDialog()
End Using
MessageBox.Show("Task complete")
End Sub
Private Sub BackgroundWorker_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs)
Dim worker = DirectCast(sender, BackgroundWorker)
For i = 1 To 100
Thread.Sleep(100)
worker.ReportProgress(i)
Next
End Sub
End Class
vb.net Code:
Imports System.ComponentModel
Public Class WorkingDialogue
Public Sub New(doWorkHandler As DoWorkEventHandler)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
AddHandler Me.BackgroundWorker1.DoWork, doWorkHandler
End Sub
Private Sub WorkingDialogue_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Me.ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Me.Close()
End Sub
End Class