I'm using BackGrondWorker in my Desktop application form to load some data from the Internet, but I faced a strange problem. after load the form I click on radio button on the form called "RdMain" that button to fire the BGW:
private void RdMain_CheckedChanged(object sender, EventArgs e)
{
if (BgwCodeLevel.IsBusy)
{
BgwCodeLevel.CancelAsync();
}
xAccounts.xClsMain.ShowPB();
BgwCodeLevel.RunWorkerAsync();
}
so it's working well but when I click On another radio button called "RdSub" that has the same code exactly as "RdMain" I get an error BGW is currently busy although it's not!!!

when you see the code you will see that everything is ok as I think at least.
private void BgwCodeLevel_DoWork(object sender, DoWorkEventArgs e)
{
try
{
//CheckConn
if (xAccounts.xClsDb.CheckConn() == false) { Settings.Default.ErrType = 1; return; }
//Get Code + Level
xAccounts.AccParent = Convert.ToInt64(CboAccParent.SelectedValue);
if (RdMain.Checked)
{
xAccounts.AccType = "main";
}
else
{
xAccounts.AccType = "sub";
}
xAccounts.Get_AccCode();
xAccounts.Get_AccLevel();
Settings.Default.ErrType = 0;
}
catch
{
Settings.Default.ErrType = 2;
}
finally
{
xAccounts.xClsDb.CloseConn();
}
}
//End
private void BgwCodeLevel_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
switch (Settings.Default.ErrType)
{
case 0:
TxtAccCode.Text = xAccounts.AccCode.ToString();
TxtAccLevel.Text = xAccounts.AccLevel.ToString();
xAccounts.xClsMain.HidePB();
break;
case 1:
xAccounts.xClsMain.Msg_ErrCon();
break;
case 2:
xAccounts.xClsMain.Msg_ErrOp();
break;
case 3:
xAccounts.xClsMain.Msg_ErrDbl();
break;
}
}
note: it's not the first time that I have used BGW and it works very well in all parts of my projects but here I don't know what's the problem!
I found the problem. the problem is that when I change the "Rdmain" status the event of "RdMain" will be fired but there is another event that will be fired also, it's "RdSub" and both events will run the same BGW at the same time.