When you select a startup form in a VB.NET app, it is created by the application framework automatically and it IS the default instance of its type. Your problem is that you are trying to refer to the default instance of Form1 in a method that is NOT executed on the UI thread. As I explain in my blog post, default instances are thread-specific, so that actually creates a new Form1 instance.
If you wanted to keep going down that road then what you could is, instead of using the default instance of Form1 in your module, you could use My.Application.ApplicationContext.MainForm to access the startup form.
That's only going to work for the startup form though. It would be better if you did it the "proper" way. In the DoWork event handler, like every event handler, there is a 'sender' parameter. That refers to the object that raised the event, i.e. the BackgroundWorker object. You can then pass that object to any method that needs to report progress and that method can access the BackgroundWorker directly, without caring if and what form it's on. Your code for Form1 then becomes:and the module becomes:Code:Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load BackgroundWorker1.RunWorkerAsync() End Sub Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork Dim worker = DirectCast(sender, System.ComponentModel.BackgroundWorker) showTest(worker) Module1.showTestFromModule1(worker) End Sub Private Sub showTest(worker As System.ComponentModel.BackgroundWorker) worker.ReportProgress(0, "This shows on the main form.") End Sub Private Sub progress(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged Dim message As String = CStr(e.UserState) TextBox1.AppendText(message & vbCrLf) End Sub End ClassYou don't actually need to do it within the form itself, given that that is already working as is, but I've done it there for consistency.Code:Module Module1 Public Sub showTestFromModule1(worker As System.ComponentModel.BackgroundWorker) worker.ReportProgress(0, "This doesn't show up on the main form.") End Sub End Module




Reply With Quote
