.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

Exception While using BackGround Worker Exception has been thrown by the target of an invocation

Posted By: Yatin     Posted Date: December 09, 2010    Points:2   Category :C#
I am using BackgroudWorker in my application...It will give Exception like
"Exception has been thrown by the target of an invocation." Why this exception occures and What to do to avoid this Exception

My COde Is like Bellow


private void bgwACSC_DoWork(object sender, DoWorkEventArgs e)
{

scAutoCompletedgv.Clear();
string StrSel = "select productname from products order by productname";
DataTable dt = new DataTable();
dt = ObjSql.Select(StrSel);
if (dt != null && dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
System.Threading.Thread.Sleep(50);
bgwACSC.ReportProgress(i);
scAutoCompletedgv.Add(dt.Rows[i]["productName"].ToString());
}
}
MessageBox.Show("The task has been completed.");
}


private void bgwACSC_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
MessageBox.Show("The task has been cancelled");
}

else
{
MessageBox.Show("The task has been completed. Results: " + e.Result.ToString());
}
}

private void btnStart_Click(object sender, EventArgs e)
{
bgwACSC.RunWorkerAsync();

}


Responses
Author: Rajesh             
Posted Date: December 21, 2010     Points: 5   

I don't think that you can dierctly call scAutoCompletedgv.Clear() directly inside the DoWork event.
Instead please call it like the following,

private void bgwACSC_DoWork(object sender, DoWorkEventArgs e)
{

ClearDataGrid();// NOTE that I have commented the previous line //scAutoCompletedgv.Clear();
string StrSel = "select productname from products order by productname";
DataTable dt = new DataTable();
dt = ObjSql.Select(StrSel);
if (dt != null && dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
System.Threading.Thread.Sleep(50);
bgwACSC.ReportProgress(i);
scAutoCompletedgv.Add(dt.Rows[i]["productName"].ToString());
}
}
MessageBox.Show("The task has been completed.");
}

//The following are the new delegate and method to clear the datagrid data

private delegate void InvokeClearDataGrid();
// make sure that this is fire-and-forget for BeginInvoke usage
internal void ClearDataGrid()
{
if (InvokeRequired)
{
// we're not on the right thread; switch back to the thread that owns the master-form
BeginInvoke(new InvokeClearDataGrid(ClearDataGrid), null);
return;
}
try
{
scAutoCompletedgv.Clear();
}
catch (Exception ex)
{
throw ex;
}
}




Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend