Multi_Thread - How do I open frm2 in a 2nd thread?
Please correct me if I'm wrong, but multithreading will do the following:
I have a form frmInvMaint which loads about 800 000 records from my Pick database. I have a 2nd form frmLoading with a animated gif on it.
The reason for this is the application takes about 30seconds to return the selection from the DB. During that time, the application seems to be hanging (not responding). I added the frmLoading to show, before the selection executes. This way the user will see a animated loading gif on his screen, assuring him that the application is working in the background.
This is where the booboo hit the fan. My lame attempt on multi threading failed. The gif on frmLoading hangs, infact, the entire form hangs.
Will multithreading fix this if done properly? If yes, can anybody show me how to do this?
This is the code I used:
vb.net 2010 Code:
Public Shared Sub Main()
Dim t As New Thread(AddressOf ThreadProc)
t.Start()
FrmLoading.Show()
End Sub
Re: Multi_Thread - How do I open frm2 in a 2nd thread?
Try this (Form1 and Form2 must be present in the project):
vb Code:
Imports System.ComponentModel
Public Class Form1
Private bw As New BackgroundWorker With {
.WorkerReportsProgress = True,
.WorkerSupportsCancellation = True}
Private frmProgress As New Form2
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
AddHandler bw.DoWork, AddressOf RunLongProcess
AddHandler bw.ProgressChanged, AddressOf ProgressChanged
bw.RunWorkerAsync()
End Sub
Private Sub RunLongProcess(ByVal sender As System.Object, ByVal e As DoWorkEventArgs)
Dim progress As Integer
For progress = 0 To 100
System.Threading.Thread.Sleep(1000)
bw.ReportProgress(progress)
Next
End Sub
Private Sub ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs)
If Not frmProgress.Visible Then frmProgress.Show()
' Change to Progress bar or whatever - just a demonstration
frmProgress.Text = e.ProgressPercentage.ToString()
End Sub
End Class
Re: Multi_Thread - How do I open frm2 in a 2nd thread?
Thanks. Now I have another issue with the thread.
In my thread sub, i populate a datatable
the datatable now has a rowcount of 97.
I can view the values if I do this:
vb Code:
MsgBox(dt_Final.Rows.Count)
MsgBox(dt_Final.Rows(1).Item(0))
but when I do this:
vb Code:
dgv.DataSource = dt_Final
No result... The dgv stays empty