最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】C#程序异常:Additional information: Exception has been thrown by the target of an invocation

C# crifan 11618浏览 0评论

【问题】

C#中,运行程序,异常:

Additional information: Exception has been thrown by the target of an invocation.

 

run doevents error Additional information Exception has been thrown by the target of an invocation

【解决过程】

1.后来,自己去看代码,好像看出问题了。

因为本身此处用的是BackgroundWorker,在completed函数中,用到了e.Result:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
private void bgwDownload_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    // The background process is complete. We need to inspect
    // our response to see if an error occurred, a cancel was
    // requested or if we completed successfully.
 
    // Check to see if an error occurred in the
    // background process.
    if (e.Error != null)
    {
        //MessageBox.Show(e.Error.Message);
        return;
    }
 
    // Check to see if the background process was cancelled.
    if (e.Cancelled)
    {
        //MessageBox.Show("Cancelled ...");
    }
    else
    {
        bNotCompleted_download = false;
 
        // Everything completed normally.
        // process the response using e.Result
        //MessageBox.Show("Completed...");
        gRealReadoutLen = (int)e.Result;
    }
}

但是,由于刚才不小心,在DoWork中,没有赋值e.Result:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    private void bgwDownload_DoWork(object sender, DoWorkEventArgs e)
    {
    //    // The sender is the BackgroundWorker object we need it to
    //    // report progress and check for cancellation.
    //    BackgroundWorker gBgwDownload = sender as BackgroundWorker;
。。。
        try
        {
。。。
            do
            {
。。。
                curReadoutLen = binStream.Read(respBytesBuf, curBufPos, (int)expectReadoutLen);
                if (curReadoutLen > 0)
                {
。。。
                    int currentPercent = (int)((currentLength * 100) / totalLength);
                     
                    if (currentPercent < 0)
                    {
                        currentPercent = 0;
                    }
 
                    if (currentPercent > 100)
                    {
                        currentPercent = 100;
                    }
 
                    gBgwDownload.ReportProgress(currentPercent);
                }
            } while (curReadoutLen > 0);
        }
        catch (Exception ex)
        {
            realReadoutLen = -1;
        }
 
        //return realReadoutLen;
        //e.Result = realReadoutLen;
 
        gBgwDownload.ReportProgress(100);
    }

所以,此处,当调用BackgroundWorker的complete时候,即在bgwDownload_RunWorkerCompleted中,会执行到

1
gRealReadoutLen = (int)e.Result;

而此处e.Result,没有赋值,所以估计是null值,所以去执行:

1
realReadoutLen = gRealReadoutLen;

才出错的。

所以,去把e.Result加上:

1
2
3
4
5
6
7
8
9
private void bgwDownload_DoWork(object sender, DoWorkEventArgs e)
{
。。。
    //return realReadoutLen;
 
    gBgwDownload.ReportProgress(100);
 
    e.Result = realReadoutLen;
}

【总结】

是自己不小心,在BackgroundWorker的DoWork中,把e.Result赋值漏掉了,所导致此问题。

加上后,就解决了。

转载请注明:在路上 » 【已解决】C#程序异常:Additional information: Exception has been thrown by the target of an invocation

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.427 seconds, using 22.17MB memory