zoukankan      html  css  js  c++  java
  • Winform 异步调用

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.Collections;
    using System.Threading;
    using System.Threading.Tasks;
    namespace WorkFlow
    {
        public partial class Server : Form
        {
            private delegate void FlushClient();//代理
            Thread thread = null;
            int counter = 0;
            public Server()
            {
                InitializeComponent();
            }
            /// <summary>
            /// 加载load事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void Server_Load(object sender, EventArgs e)
            {
                thread = new Thread(CrossThreadFlush);
                thread.IsBackground = true;
                thread.Start();
            }
            private void CrossThreadFlush()
            {
                while (true)
                {   //将sleep和无限循环放在等待异步的外面
                    Thread.Sleep(1000);
                    ThreadFuntion();
                }
            }
            /// <summary>
            /// 获取数据
            /// </summary>
            /// <returns></returns>
            public void ThreadFuntion()
            {
                if (this.dtReadList.InvokeRequired)//等待异步
                {
                    FlushClient fc = new FlushClient(ThreadFuntion);
                    this.Invoke(fc); //通过代理调用刷新方法
                }
                else
                {
                    counter += 1;
                    int index = this.dtReadList.Rows.Add();
                    this.dtReadList.Rows[index].Cells[0].Value = DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss");
                    this.dtReadList.Rows[index].Cells[1].Value = counter;
                }
            }
        }
    }
  • 相关阅读:
    swoole学习(四):websocket
    LeetCode--SQL 查询:体育馆的人流量
    LeetCode--SQL 查询:有趣的电影
    centos7下mysql5.7忘记密码
    LeetCode--SQL 查询:删除重复的电子邮箱。
    swoole学习(三):HTTP服务
    swoole学习(二):TCP/UDP服务器-客户端
    swoole学习(一):初识swoole
    LeetCode--SQL 查询:查找部门工资前三高的职员
    报文、报文段、分组、包、数据报、帧、数据流的概念区别
  • 原文地址:https://www.cnblogs.com/dullbaby/p/4329391.html
Copyright © 2011-2022 走看看