Solved

Accessing form main controls from a created thread.

Posted on 2007-11-19
1
222 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

705 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