在Excel的C#函数中使用线程

时间:2022-09-02 08:57:18

I am writing a ping function for Excel. This function suppose to take a column of IP address (about 200 address) and ping each one of them.

我正在为Excel编写ping函数。此函数假设采用一列IP地址(大约200个地址)并对其中的每一个进行ping操作。

To avoid Excel getting stuck, I have decided to use a thread that will send the pings instead of the main thread. The problem is that the thread can't access the cell values and the Excel crashes.

为了避免Excel卡住,我决定使用一个线程来发送ping而不是主线程。问题是线程无法访问单元格值并且Excel崩溃。

I hope that someone could help me with this.

我希望有人可以帮助我。

Here is the code :

这是代码:

    private LinkedList<string> Iplist;
    private Thread t;
    public NetworkPing()
    {            
    }

    public int CalcPingColumn(Range IPcells, Range ANScells)
    {
        CreateIpList(IPcells);
        this.t = new Thread(() => CalcPing_method(ANScells));
        this.t.Start();
        return 1;
    }
    //THE PROBLEM IS PREOBBLY HERE
    private void CalcPing_method(Range ANScells)
    {
        foreach (Range ans in ANScells.Cells)
        {
            ans.Value2 = Ping(Iplist.Last.Value);
            Iplist.RemoveLast();
            Thread.Sleep(50);
        }
    }

    private void CreateIpList(Range IPcells)
    {
        Iplist = new LinkedList<string>();
        foreach (Range ip in IPcells.Cells)
        {
            Iplist.AddFirst(ip.Value2);
        }
    }

2 个解决方案

#1


0  

Try to use the BackgroundWorker-class instead of the regular Thread. It makes it more easy to "send" objects/instances between threads. From that you can also get progress: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker(v=vs.110).aspx

尝试使用BackgroundWorker类而不是常规Thread。它使得在线程之间“发送”对象/实例变得更加容易。从那里你也可以获得进步:http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker(v=vs.110).aspx

#2


0  

why not one function?

为什么不是一个功能?

    public string PingIpadress(string ipadress)
    {
        var pingSender = new Ping();
        var options = new PingOptions();

        // Use the default Ttl value which is 128,
        // but change the fragmentation behavior.
        options.DontFragment = true;

        // Create a buffer of 32 bytes of data to be transmitted.
        string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
        byte[] buffer = Encoding.ASCII.GetBytes(data);
        int timeout = 120;
        PingReply reply = pingSender.Send(ipadress, timeout, buffer, options);
        if (reply.Status == IPStatus.Success)
        {
            return "alive " + reply.Options.Ttl;
        }
        return "no response";
    }

#1


0  

Try to use the BackgroundWorker-class instead of the regular Thread. It makes it more easy to "send" objects/instances between threads. From that you can also get progress: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker(v=vs.110).aspx

尝试使用BackgroundWorker类而不是常规Thread。它使得在线程之间“发送”对象/实例变得更加容易。从那里你也可以获得进步:http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker(v=vs.110).aspx

#2


0  

why not one function?

为什么不是一个功能?

    public string PingIpadress(string ipadress)
    {
        var pingSender = new Ping();
        var options = new PingOptions();

        // Use the default Ttl value which is 128,
        // but change the fragmentation behavior.
        options.DontFragment = true;

        // Create a buffer of 32 bytes of data to be transmitted.
        string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
        byte[] buffer = Encoding.ASCII.GetBytes(data);
        int timeout = 120;
        PingReply reply = pingSender.Send(ipadress, timeout, buffer, options);
        if (reply.Status == IPStatus.Success)
        {
            return "alive " + reply.Options.Ttl;
        }
        return "no response";
    }