I am new to C# and I am using windows forms
I have a problem with doing some work in BackgroundWorker.
I have 2 forms (Form1
and Form2
) and 3 User Controls
UC1
, UC2
and UC3
.
Form1
has only one button
which shows Form2
which you click on it.
Form2
has 4 buttons
(Btn_ShowUC1
, Btn_ShowUC2
, Btn_ShowUC3
and Button_Close
) and 3 BackgroundWorkers
.
What I am trying to do is: when I click on button1
in Form1
, Form2
show up and then I want to show the relevant User control
and hide the rest when I click on any button in Form2
. For example: click on Btn_ShowUC1
user control1
shows up and hide the rest, click on Btn_ShowUC2
user control2
shows up and hide the rest and so on. Now showing and hiding the user controls
in UI sometimes cuases Form2
to freeze therefore I used BackgroundWorkers to Show/Hide user control
process.
I am using 3 BackgroundWorkers
for each relavant button in case one BackgroundWorker
is busy to do the show/hide processs.
In Form1:
Form2 f2 = new Form2();
private void button1_Click(object sender, EventArgs e)
{
f2.ShowDialog();
}
In Form2:
UserControl CurrentUserControl;
UserControl1 uc1 = new UserControl1();
UserControl2 uc2 = new UserControl2();
UserControl3 uc3 = new UserControl3();
public Form2()
{
InitializeComponent();
Controls.Add(uc1);
Controls.Add(uc2);
Controls.Add(uc3);
}
private void Form2_Load(object sender, EventArgs e)
{
foreach (Control ctrl in this.Controls)
{
if (ctrl is UserControl)
{
ctrl.Visible = false;
}
}
uc1.Visible = true;
}
private void Btn_ShowUC1_Click(object sender, EventArgs e)
{
CurrentUserControl = uc1;
backgroundWorker1.RunWorkerAsync();
}
private void Btn_ShowUC2_Click(object sender, EventArgs e)
{
CurrentUserControl = uc2;
backgroundWorker2.RunWorkerAsync();
}
private void Btn_ShowUC3_Click(object sender, EventArgs e)
{
CurrentUserControl = uc3;
backgroundWorker3.RunWorkerAsync();
}
private void Button_Close_Click(object sender, EventArgs e)
{
close();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
foreach (Control ctrl in this.Controls)
{
if (ctrl is UserControl)
{
ctrl.Visible = false;
}
}
CurrentUserControl.Visible = true;
}
private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
{
foreach (Control ctrl in this.Controls)
{
if (ctrl is UserControl)
{
ctrl.Visible = false;
}
}
CurrentUserControl.Visible = true;
}
private void backgroundWorker3_DoWork(object sender, DoWorkEventArgs e)
{
foreach (Control ctrl in this.Controls)
{
if (ctrl is UserControl)
{
ctrl.Visible = false;
}
}
CurrentUserControl.Visible = true;
}
When Form2
loads usercontrol1
shows up and when I click any any button the relevant user control shows up and every thing works well. However, When I close form2
and open it again and click on any button none of the user controls
show up and Form2
freezes and it throws error saying "This backGroundWorker is currently busy and can not run multiple tasks concurrently"
Why I am having this error? I mean I am using 3 different backGroundWorkers
.
Note this is all the code I have in the project.
Anyone knows how can I fix it? I mean I want to show/hide the user controls in separate thread when I click on any button without freezing the form. I will be very happy to hear any different ideas. Thank you