0

我正在为我们的程序构建一个 IP 扫描仪。它目前可以工作,但运行速度非常慢。超时似乎无法正常工作。代码如下所示:

    public Tracker Discover(Uri hostUri, string organizationTrackerId)
    {
        var binding = new NetTcpBinding();
        binding.ReceiveTimeout = TimeSpan.FromSeconds(2);
        binding.SendTimeout = TimeSpan.FromSeconds(2);
        binding.OpenTimeout = TimeSpan.FromSeconds(2);
        var endpointAddress = new EndpointAddress(hostUri);
        using (var factory = new ChannelFactory<ITrackerService>(binding, endpointAddress))
        {
            try
            {
                factory.Open(TimeSpan.FromSeconds(2));
                var channel = factory.CreateChannel();
                Tracker tracker = channel.Discover(organizationTrackerId);
                factory.Close();
                return tracker;
            }
            catch (Exception)
            {
                factory.Abort();
            }
        }
        return null;
    }

这段代码是从循环 255 个 IP 的 for 循环中调用的。调用此函数时,即使我已尝试将超时设置为 2,目前仍需要约 16 秒才能完成。有什么想法吗?

4

0 回答 0