Solved

Accessing form main controls from a created thread.

Posted on 2007-11-19
1
218 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 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