Results 1 to 3 of 3

Thread: Multi_Thread - How do I open frm2 in a 2nd thread?

  1. #1

    Thread Starter
    Addicted Member tgf-47's Avatar
    Join Date
    Feb 2010
    Location
    CapeTown, South Africa -34.01244,18.337415
    Posts
    209

    Red face 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:
    1. Public Shared Sub Main()
    2.         Dim t As New Thread(AddressOf ThreadProc)
    3.         t.Start()
    4.         FrmLoading.Show()
    5.     End Sub

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    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:
    1. Imports System.ComponentModel
    2.  
    3. Public Class Form1
    4.     Private bw As New BackgroundWorker With {
    5.             .WorkerReportsProgress = True,
    6.             .WorkerSupportsCancellation = True}
    7.  
    8.     Private frmProgress As New Form2
    9.  
    10.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
    11.         AddHandler bw.DoWork, AddressOf RunLongProcess
    12.         AddHandler bw.ProgressChanged, AddressOf ProgressChanged
    13.         bw.RunWorkerAsync()
    14.     End Sub
    15.  
    16.     Private Sub RunLongProcess(ByVal sender As System.Object, ByVal e As DoWorkEventArgs)
    17.         Dim progress As Integer
    18.  
    19.         For progress = 0 To 100
    20.             System.Threading.Thread.Sleep(1000)
    21.             bw.ReportProgress(progress)
    22.         Next
    23.     End Sub
    24.  
    25.     Private Sub ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs)
    26.         If Not frmProgress.Visible Then frmProgress.Show()
    27.         ' Change to Progress bar or whatever - just a demonstration
    28.         frmProgress.Text = e.ProgressPercentage.ToString()
    29.     End Sub
    30. End Class

  3. #3

    Thread Starter
    Addicted Member tgf-47's Avatar
    Join Date
    Feb 2010
    Location
    CapeTown, South Africa -34.01244,18.337415
    Posts
    209

    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:
    1. MsgBox(dt_Final.Rows.Count)
    2. MsgBox(dt_Final.Rows(1).Item(0))
    but when I do this:
    vb Code:
    1. dgv.DataSource = dt_Final
    No result... The dgv stays empty

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width