简体   繁体   English

C#多线程处理-'System.Reflection.TargetInvocationException'

[英]C# multithreading - 'System.Reflection.TargetInvocationException'

I started multithread programming in C# (WPF) few days ago and here is a problem which I can't solve... I'm using this piece of code: 几天前,我开始使用C#(WPF)进行多线程编程,这是一个我无法解决的问题...我正在使用以下代码:

    void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        Random foodPosition = new Random();
        double x,y;
        Size size = new Size(30,30);
        bool foodCreated = false;
        Ellipse food = null;
        Food foodObject = null;
        Thread foodThread = new Thread(new ThreadStart(() =>
            {
                field.Dispatcher.Invoke(new Action(() =>
                {
                    food = new Ellipse();
                    food.Fill = GenerateColor();
                    food.Width = size.Width;
                    food.Height = size.Height;
                    x = foodPosition.Next(0, (int)(playGroundSize.Width - size.Width) + 1);
                    y = foodPosition.Next(0, (int)(playGroundSize.Height - size.Height) + 1);
                    if (IsFree(x, y, size, 0))
                    {
                        playField.Children.Add(food);
                        Canvas.SetTop(food, y);
                        Canvas.SetLeft(food, x);

                        foodObject = new Food(food, new Point(x, y));
                        foodCollection.Add(foodObject,0);
                        foodCreated = true;
                    }
                }));

                if (foodCreated)
                {
                    for (int i = 0; i < foodAliveTime; i++)
                    {
                        Thread.Sleep(1000);
                        foodCollection[foodObject]++;
                    }
                    field.Dispatcher.Invoke(new Action(() =>
                    {
                        playField.Children.Remove(foodObject.FoodObject);
                        //threadList[foodObject].Abort();
                    }));
                }

            }));
        foodThread.Start();
    }

I think that the problem comes from the upper code. 我认为问题出在上层代码。 There is an exception thrown after about a minute work of my program. 在我的程序经过大约一分钟的工作后,引发了异常。 This is the exception: 这是例外:

An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll mscorlib.dll中发生了类型为'System.Reflection.TargetInvocationException'的未处理异常

Additional information: Exception has been thrown by the target of an invocation. 附加信息:调用的目标已引发异常。

And after that I'm receiving this message: 之后,我收到此消息:

There is no source code available for the current location. 当前位置没有可用的源代码。

I know that my source code is a little bit ugly, I'm going to make it better after I solve this problem. 我知道我的源代码有点丑陋,在解决这个问题后,我将使其变得更好。 Could you please tell me how I can fix the it? 您能告诉我如何解决吗?

I suppose that problem is in line: 我想这个问题符合要求:

field.Dispatcher.Invoke

You should put this part of code inside try/catch block and catch 您应该将这部分代码放入try / catch块中并进行捕获

TargetInvocationException

This exception can provide more information what the problem is (pay attention to its InnerException ). 此异常可以提供有关问题所在的更多信息(请注意其InnerException )。

PS Put the whole body of function inside try/catch block; PS将整个功能放在try / catch块中; and catch not only TargetInvocationException but at least System.Exception after it. 并不仅捕获TargetInvocationException而且还捕获至少System.Exception

To troubleshoot this type of error, get the inner exception. 要解决此类错误,请获取内部异常。 It could be due to a number of different issues. 这可能是由于许多不同的问题。

try
{
    // code causing TargetInvocationException
}
catch (Exception e)
{
    if (e.InnerException != null)
    {
    string err = e.InnerException.Message;
    }
}

When dealing with System.Reflection.TargetInvocationException inner exception should also be looked at . 处理System.Reflection.TargetInvocationException时,还应注意内部异常。 That might tell you where the exception. 那可能会告诉您异常情况在哪里。

Also do you have to do Thread foodThread = new Thread(new ThreadStart(() => If I was use I will use Threadpool.QueueworkerItem or if you have .Net Framework 4.0 use 您还必须执行线程foodThread = new Thread(new ThreadStart(()=>如果我使用的话,我将使用Threadpool.QueueworkerItem或者如果您使用.Net Framework 4.0,

Task.Factory.StartNew() Task.Factory.StartNew()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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