首页 新闻 会员 周边 捐助

Task的取消

0
悬赏园豆:10 [已关闭问题] 关闭于 2014-09-30 15:41
 public static string Connection()
        {

            string MesssageNotice = "";

            CancellationTokenSource c = new CancellationTokenSource();
            CancellationToken token = c.Token;
            Task<string> task = new Task<string>(() =>
            {
                try
                {
                    token.ThrowIfCancellationRequested();
                    TcpClient client = new TcpClient(AddressFamily.InterNetwork);
                    client.SendTimeout = 3000;
                    client.ReceiveTimeout = 3000;
                    client.Connect(IPAddress.Parse("XXXX.XXX.XXXX.XXX"), int.Parse("456"));
                    if (client.Connected)
                    {
                        Byte[] data = System.Text.Encoding.UTF8.GetBytes("ABCDEFGDSGFWEREW");
                        using (NetworkStream stream = client.GetStream())
                        {
                            stream.Write(data, 0, data.Length);
                            data = new Byte[client.ReceiveBufferSize];
                            string responseData = string.Empty;

                            
                            int bytes = stream.Read(data, 0, data.Length);
                            responseData = Encoding.UTF8.GetString(data, 0, bytes);
                            client.Close();
                            stream.Close();
                            MesssageNotice = responseData;
                            return MesssageNotice;
                        }
                    }
                    else
                    {
                        token.ThrowIfCancellationRequested();
                        c.Cancel();
                        MesssageNotice = "failed";
                        return MesssageNotice;
                    }
                }
                catch (SocketException)
                {
                    token.ThrowIfCancellationRequested();
                    c.Cancel();

                    //throw;
                    throw new Exception("failed");
                }
                catch (Exception)
                {
                    token.ThrowIfCancellationRequested();
                    c.Cancel();
                    throw;
                }
            }, token);
            task.Start();

            if (task.Wait(3000, token))
            {
                return task.Result;
            }
            else
            {
                c.Cancel();
                return "failed";
            }
        }

有的时候 服务器 不会立马将数据返回……

我加了3秒的等待,
为什么  我写的超时就取消不管用,过了 50-60秒,代码还会跳转到

需要格局的主页 需要格局 | 老鸟四级 | 园豆:2145
提问于:2014-08-27 18:34
< >
分享
所有回答(1)
0

全部贴出来看看呗,你说的超时处理,没看到。

而且我可以告诉你,你这段还有隐患

Byte[] data = System.Text.Encoding.UTF8.GetBytes("ABCDEFGDSGFWEREW");

int bytes = stream.Read(data, 0, data.Length);

responseData = Encoding.UTF8.GetString(data, 0, bytes);

 

如果 bytes  <  data.Length  ,data 中就有垃圾数据了。

牧师/preacher | 园豆:500 (菜鸟二级) | 2014-08-28 16:06
 if (task.Wait(3000, token))
            {
                return task.Result;
            }
            else
            {
                c.Cancel();
                return "failed";
            }

我这里有处理啊

支持(0) 反对(0) 需要格局 | 园豆:2145 (老鸟四级) | 2014-08-28 18:39
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册