开发者

C# multithreading - 'System.Reflection.TargetInvocationException'

开发者 https://www.devze.com 2023-01-31 22:39 出处:网络
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:

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 tha开发者_C百科t 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

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.


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 . 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()

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号