Solved

Accessing form main controls from a created thread.

Posted on 2007-11-19
1
217 Views
Last Modified: 2008-02-01
I have a function that updates a progressbar in a loop. I want to create a thread for a progress bar update. How can I get formMain in the ThreadTask() function? Im unsure how to call this function and access the form controls.

Main function:
Imports System.Threading

Private trd As Thread

Public Sub Main(ByVal myExcelDataset As DataSet, ByRef frm As frmMain)
     formMain = frmMain

     'reset progress bar
     formMain.ProgBar.Value = 0
     formMain.Refresh()
     For i As Integer = 0 To myExcelDataset.Tables("ExcelDoc").Rows.Count - 1
          'need to update the progressbar thread here!
     Next i
     formMain.lbStatus.Items(1) = "Inserting Projects... 100%"
     formMain.lbStatus.Items(1) = "Inserting Projects... Complete!"
End Sub

Private Sub ThreadTask()
      frm.lbStatus.Items(1) = "Copying to database..." & Int(formMain.ProgBar.Value * 100 / formMain.ProgBar.Maximum) & "%"
      frm.ProgBar.Value = formMain.ProgBar.Value + 1
      frm.Refresh()
End Sub
0
Comment
Question by:bertino12
1 Comment
 
LVL 3

Accepted Solution

by:
wizrr earned 500 total points
ID: 20315008
I see that your ThreadTask method is not shared (or "static" like in C#). You can access your class fields from that method, but control's are not thread safe and control member's can't be accessed (this is possible, but problems can occur) from thread another than thread where that control's was created.

You must marshal code which access members of control's to thread in which that control's was created using Invoke or BeginInvoke of Control class, actually that must be instance of your form.
C# code:
 

private class ProgressState {

   public string state;

}
 

private void MethodWhichAccessUnsafeControlMembers(Object stateObj) {

  ProgressState state = (ProgressState)stateObj;

  form.lblStatus.Text = state.state;

}
 

private void ThreadTask() {

  ProgressState state = new ProgressState();

  state.state = "Copying to database...";

  // let's check is control created (has windows handle) and

  // member's can be accessed

  if ( form.InvokeRequered ) {

    form.Invoke(new ParameterizedThreadStart

       (MethodWhichAccessUnsafeControlMembers), state);

  } else {

    MethodWhichAccessUnsafeControlMembers( state );

  }

}

Open in new window

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now