Help with "Invoke or BeginInvoke cannot be called on a control until the window handle has been created."
-
Hi. What I'm trying to accomplish is to call this class, pass some controls and values and then have the new object update the existing control. Help with this would be appreciated? Maybe it is not possible since I cannot pass the handle value to the class? The error is a little misleading as the control is already created?
private void btnGroupsForUserTest_Click(object sender, EventArgs e)
{progressBarControl pbarcontrol = new progressBarControl(); pbarcontrol.pbarIncrementValue = 1; pbarcontrol.pbarMaximum = 100; pbarcontrol.pbarThreadSleepIncrement = 250; pbarcontrol.pbarObject = pBarGroupsForUser; pbarcontrol.pbarLabel = lblGroupsForUserStatus; progressBarIncrementer = new Thread(new ThreadStart(pbarcontrol.pbarIncrementFunction)); progressBarIncrementer.IsBackground = true; progressBarIncrementer.Start(); }
public class progressBarControl : FormGroupMemberShipCheckingTool
{
public ProgressBar pbarObject;// { get; set; }
public Label pbarLabel;// { get; set; }public int pbarMaximum { get; set; } public int pbarIncrementValue { get; set; } public int pbarThreadSleepIncrement { get; set; } public void pbarIncrementFunction() { int incrementValue = pbarIncrementValue; do { this.Invoke((MethodInvoker)delegate() { if (incrementValue < pbarMaximum) { pbarObject.Value = incrementValue; pbarLabel.Text = incrementValue.ToString() + "% complete"; Application.DoEvents(); } }); incrementValue++; Thread.Sleep(pbarThreadSleepIncrement); } while (incrementValue < pbarMaximum); }
}
-
Hi. What I'm trying to accomplish is to call this class, pass some controls and values and then have the new object update the existing control. Help with this would be appreciated? Maybe it is not possible since I cannot pass the handle value to the class? The error is a little misleading as the control is already created?
private void btnGroupsForUserTest_Click(object sender, EventArgs e)
{progressBarControl pbarcontrol = new progressBarControl(); pbarcontrol.pbarIncrementValue = 1; pbarcontrol.pbarMaximum = 100; pbarcontrol.pbarThreadSleepIncrement = 250; pbarcontrol.pbarObject = pBarGroupsForUser; pbarcontrol.pbarLabel = lblGroupsForUserStatus; progressBarIncrementer = new Thread(new ThreadStart(pbarcontrol.pbarIncrementFunction)); progressBarIncrementer.IsBackground = true; progressBarIncrementer.Start(); }
public class progressBarControl : FormGroupMemberShipCheckingTool
{
public ProgressBar pbarObject;// { get; set; }
public Label pbarLabel;// { get; set; }public int pbarMaximum { get; set; } public int pbarIncrementValue { get; set; } public int pbarThreadSleepIncrement { get; set; } public void pbarIncrementFunction() { int incrementValue = pbarIncrementValue; do { this.Invoke((MethodInvoker)delegate() { if (incrementValue < pbarMaximum) { pbarObject.Value = incrementValue; pbarLabel.Text = incrementValue.ToString() + "% complete"; Application.DoEvents(); } }); incrementValue++; Thread.Sleep(pbarThreadSleepIncrement); } while (incrementValue < pbarMaximum); }
}
The
progressBarControl
control has been created, but the window handle for the control has not. You create a new instance of the control, but never show it anywhere, so it never creates a window. Try usingpbarObject.Invoke
instead.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
The
progressBarControl
control has been created, but the window handle for the control has not. You create a new instance of the control, but never show it anywhere, so it never creates a window. Try usingpbarObject.Invoke
instead.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Doh! Thank you, that worked!