2

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?

Jason Down
  • 21,731
  • 12
  • 83
  • 117
  • 3
    What is the inner exception? Also, I asked another question that deals with the same thing: http://stackoverflow.com/questions/4269463/how-are-these-two-invocations-different it may be of use to you, although I never got a correct answer – Corey Dec 24 '10 at 21:09
  • 1
    Seconded; the .InnerException is key here – Marc Gravell Dec 24 '10 at 21:36

3 Answers3

2

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;
    }
}
JustBeingHelpful
  • 18,332
  • 38
  • 160
  • 245
2

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.

Vladimir
  • 1,781
  • 13
  • 12
0

When dealing with System.Reflection.TargetInvocationException inner exception should also be looked at . 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

Task.Factory.StartNew()

Ash
  • 2,531
  • 2
  • 29
  • 38