C# multithreading - 'System.Reflection.TargetInvocationException'

user551761 picture user551761 · Dec 24, 2010 · Viewed 22.4k times · Source

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:

    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

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?

Answer

Vladimir picture Vladimir · Dec 24, 2010

I suppose that problem is in line:

field.Dispatcher.Invoke

You should put this part of code inside try/catch block and catch

TargetInvocationException

This exception can provide more information what the problem is (pay attention to its InnerException).

P.S. Put the whole body of function inside try/catch block; and catch not only TargetInvocationException but at least System.Exception after it.