I’m using the following code to disable form controls while i’m doing some longTask() thread. But the controls are not getting disabled.. Here is my code for disable() method.
public void disableFormControls()
{
if (InvokeRequired)
{
this.BeginInvoke(new Action(disableFormControls));
return;
}
groupBoxInput.Enabled = false;
groupBoxOutput.Enabled = false;
btnGen.Enabled = false;
btnReset.Enabled = false;
}
Here is how i’m calling it..
NOTE: LongTask() will be running in a separate thread.
private void LongTask()
{
disableFormControls();
Console.WriteLine("Started Records::" + DateTime.Now);
//Doing my long tasks here
enableFormControls();
}
Can you please let me know where i’m wrong..
Try this, I changed the Invoke of
enableFormto instead calldisableFormControls. Also I did this synchronously instead of asynchronously so your background thread isn’t doing anything until the UI is updated properly. I’m also assumingdisableFormControlsis a method on your Form.