那么佚然 2016-06-15 06:48 采纳率: 57.1%
浏览 1111

c#网口,winform中的timer

网口接收数据进行展示,用timer进行定时显示,由于数据量比较大,显示的时候比较耗时,导致timer的触发好像延时了,所以缓存的数据也不对了

  • 写回答

1条回答 默认 最新

  • hotmee 2016-06-17 17:33
    关注

    别用定时器,那是不行的。实例化一个线程,并设置为哦、后台
    主方法:
    {
    Thread thread = new Thread(ReceiveMessage);
    thread.IsBackground = true;
    thread.Start(sokConnection);
    }

    private void ReceiveMessage(object sokConnection)
    {
    Socket sokClient = sokConnection as Socket;
    while (true)
    {
    byte[] arrMsgRec = new byte[1024*64];
    int length = 0;
    try
    {
    length = sokClient.Receive(arrMsgRec);
    if (length==0)
    {
    continue;
    }
    }
    catch (SocketException)
    {
    登陆列表.Dispatcher.Invoke(
    new Action(
    delegate
    {
    if (登陆列表.Items.Contains(sokClient.RemoteEndPoint.ToString()))
    {
    dict.Remove(sokClient.RemoteEndPoint.ToString());
    dictThread.Remove(sokClient.RemoteEndPoint.ToString());
    登陆列表.Items.Remove(sokClient.RemoteEndPoint.ToString());
    }
    })
    );
    }
    byte[] newByte = new byte[length];
    Array.Copy(arrMsgRec, 0, newByte, 0, length);
    ///newByte就是收到的数据直接在这里写好就可以了
    //如果是WPF多线程调用
    //this.Dispatcher.Invoke(
    // new Action(
    // delegate
    // {
    //这里写要执行的内容
    // }
    // ));
    //如果是Winfrom则是
    //this.Invoke(
    // new Action(
    // delegate
    // {
    //这里写要执行的内容
    // }
    // ));
    }
    }

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog