C # Async Ping: not working with Threading.Timer

I hope to explain correctly. I am working on a project that runs more than 6,000 concurrent asynchronous pins every X minutes. If I try to start the component using a button in the form of a window, this will work. But if you start with "Threading.Timer", it has problems. Sometimes an application gets stuck waiting for ping responses and sometimes throws a NullReferenceException when accessing a variable that cannot be null. But if the execution using the button in the user request form works fine.

Hope someone can help me.

using System.Net.NetworkInformation;
using System.Threading;
+1
source share
3 answers

, . - , , . , .

, gui (Begin)Invoke() ( ). , :

public static class ControlExtensions
{
    public static void InvokeIfRequired(this Control c, Action<Control> action)
    {
        if (c.InvokeRequired)
        {
            c.Invoke(new Action(() => action(c)));
        }
        else
        {
            action(c);
        }
    }

    public static void BeginInvokeIfRequired(this Control c, Action<Control> action)
    {
        if (c.InvokeRequired)
        {
            c.BeginInvoke(new Action(() => action(c)));
        }
        else
        {
            action(c);
        }
    }
}

:

myTextBox.InvokeIfRequired((ctrl) => ctrl.Text == "SomeNewText");
+4

Thank you all for your help! I found a solution (over time). All I did was change the way you destroy ping objects to get your replay. Now destroy each ping object in the PingResult method using the sender parameter (ping reference) as follows:

((Ping)sender).PingCompleted -= PingResult;
((IDisposable)sender).Dispose();

This works for me. I hope this helps others.

Greetings and many thanks for everything!

0
source

All Articles