简体   繁体   中英

Exception has been thrown by the target of an invocation in C#

I am using BackGround Worker for Loading data from DB(MS Access). In my Form_Load i use:

 bgw.RunWorkerAsync();

In my DoWork event I laod the data from DB

private void bgw_DoWork(object sender, DoWorkEventArgs e)
    {

        int iResult = OpenDB();
        if (iResult != 0)
        {
            MessageBox.Show("Error in Opening DataBase", Constants.TITLE);
            return ;
        }
        DataSet ds = GetAllUserInfo();
        e.Result = ds;


    }

And in My RunWorkerCompleted i assign data to DataGridView.

    private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {

        dgUsers.Rows[0].Cells[0].Value = e.Result; 


    }

//Error Comes here after executing above code

[STAThread]
        static void Main()
        {
            Application.Run(new frmMain());
        }

Any help is appreciated. Thanks.

This happens when an exception happens on another thread.

Check that exceptions inner exception to find the real exception that was thrown by your DoWork method..

Use SyncronizationContext mechanism for UI update . Example here http://www.codeproject.com/KB/threads/SynchronizationContext.aspx

1 .Have this as a global object :

System.Threading.SynchronizationContext synchronizationContext;
  1. Instantiate the object "synchronizationContext" on Form_Load event :

    synchronizationContext = System.Threading.SynchronizationContext.Current;

  2. Modify bgw_RonWorkerCompleted to :

    private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {

      synchronizationContext.Post(new SendOrPostCallback( delegate { dgUsers.DatSource =e.Result as DataSet; }), null); 

    }

You should'nt handle the exception in bgw_DoWork.

 private void bgw_DoWork(object sender, DoWorkEventArgs e)
 {
            OpenDB();
            e.Result = GetAllUserInfo();
 }

instead you have to check the Error Property of RunWorkerCompletedEventArgs back on the UI thread.

 private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
 {
     if (e.Error ==null)
        dgUsers.Rows[0].Cells[0].Value = e.Result; 
     else
       MessageBox.Show("Error in Opening DataBase", Constants.TITLE);

 }

You may have a cross-thread operation : the RunWorkerCompleted isn't raised in the UI thread, resulting in a cross-thread operation on .Value . Could you try this :

                    private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
                {
                    if (this.InvokeRequired)
                    {
                        this.Invoke(bgw_RunWorkerCompleted, sender, e);
                        return;
                    }
                    dgUsers.Rows[0].Cells[0].Value = e.Result; 
                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM