[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Accessing form main controls from a created thread.

Posted on 2007-11-19
1
Medium Priority
?
225 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 2000 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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 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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

649 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