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

    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;
    using System.Xml;
    namespace WorkFlow
    {
        public partial class Server : Form
        {
            private delegate void FlushClient();//代理
            private delegate void ReadTime(); //设置时间代理
            Thread thread = null;
            Thread threadTime = null; //计时器
            int counter = 0;
            private static readonly string startTime = DateTime.Now.ToString();
            public Server()
            {
                InitializeComponent();
            }
            /// <summary>
            /// 加载load事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void Server_Load(object sender, EventArgs e)
            {
                DoWorkList();
                this.txtSetTime.Text = Convert.ToString(getTimeValue());
                DoWorkTime();
            }
            #region 加载运行日志列表
            /// <summary>
            /// 线程工作
            /// </summary>
            public void DoWorkList()
            {
                thread = new Thread(CrossThreadFlush);
                thread.IsBackground = true;
                thread.Start();
            }
            private void CrossThreadFlush()
            {
                while (true)
                {   //将sleep和无限循环放在等待异步的外面
                    Thread.Sleep(getTimeValue());
                    ThreadFuntion();
                }
            }
            /// <summary>
            /// 获取数据
            /// </summary>
            /// <returns></returns>
            public void ThreadFuntion()
            {
                if (this.dtReadList.InvokeRequired)//等待异步
                {
                    FlushClient fc = new FlushClient(ThreadFuntion);
                    this.Invoke(fc); //通过代理调用刷新方法
                }
                else
                {
                    counter += 1;
                    WorkFlow workflow = new WorkFlow();
                    Hashtable ht = new Hashtable();
                    //   ht.Add("ID","");
                    //   workflow.SaveWorkFlow(ht);
                    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; //workflow.GetAllOrderData().Tables[0].Rows.Count ;
                }
            }
            #endregion
            #region 查看程序已运行时间
            /// <summary>
            /// 加载
            /// </summary>
            public void DoWorkTime()
            {
                this.lblTime.Text = DateTime.Now.ToString("yyyy年mm月dd日 HH:mm:ss");
                threadTime = new Thread(timeThread);//开启时间线程
                threadTime.IsBackground = true;
                threadTime.Start();
            }
            /// <summary>
            /// 时间线程
            /// </summary>
            private void timeThread()
            {
                while (true)
                {   //将sleep和无限循环放在等待异步的外面
                    Thread.Sleep(1000);
                    TimeThreadFuntion();
                }
            }
            /// <summary>
            /// 时间计算方式
            /// </summary>
            public void TimeThreadFuntion()
            {
                if (this.dtReadList.InvokeRequired)//等待异步
                {
                    FlushClient fc = new FlushClient(TimeThreadFuntion);
                    this.Invoke(fc); //通过代理调用刷新方法
                }
                else
                {
                    DateTime beginTime1 = DateTime.Parse(startTime);
                    DateTime endTime1 = DateTime.Parse(DateTime.Now.ToString());
                    TimeSpan midTime = endTime1 - beginTime1;
                    this.txtGetTime.Text = midTime.ToString();
                }
            }
            #endregion
            #region 设置每次执行的时间
            /// <summary>
            /// 设置时间
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnSetTime_Click(object sender, EventArgs e)
            {
                Thread.Sleep(3000);
                XmlDocument xmlDoc = new XmlDocument();  //声明xml
                xmlDoc.Load("SetTimeXMLFile.xml");
                //获取bookshop节点的所有子节点 
                XmlNodeList nodeList = xmlDoc.SelectSingleNode("setTime").ChildNodes;
                //遍历所有子节点 
                foreach (XmlNode xn in nodeList)
                {
                    XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型 
                    XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点 
                    foreach (XmlNode xn1 in nls)//遍历 
                    {
                        XmlElement xe2 = (XmlElement)xn1; //转换类型
                        if (xe2.Name == "value")//如果找到 
                        {
                            xe2.InnerText = this.txtSetTime.Text;//则修改 
                            break;
                        }
                    }
                }
                xmlDoc.Save("SetTimeXMLFile.xml"); //保存其更改
                MessageBox.Show("设置成功");
                Thread.Sleep(1000);
            }
            /// <summary>
            /// 获取时间
            /// </summary>
            /// <returns></returns>
            public static int getTimeValue()
            {
                int time = 50000;
                XmlDocument xmlDoc = new XmlDocument();  //声明xml
                xmlDoc.Load("SetTimeXMLFile.xml");
                //获取bookshop节点的所有子节点 
                XmlNodeList nodeList = xmlDoc.SelectSingleNode("setTime").ChildNodes;
                //遍历所有子节点 
                foreach (XmlNode xn in nodeList)
                {
                    XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型 
                    XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点 
                    foreach (XmlNode xn1 in nls)//遍历 
                    {
                        XmlElement xe2 = (XmlElement)xn1; //转换类型
                        if (xe2.Name == "value")//如果找到 
                        {
                            time = Convert.ToInt32(xe2.InnerText);
                            break;
                        }
                    }
                }
                return time;
            }
            #endregion
        }
    }
  • 相关阅读:
    VC CUtilityLZW 效率还行的LZW压缩算法,随机数加密
    VC CQHashNTBuffer 牛逼的Hash表 UINT32
    VC CHashBuffer 牛逼的hash表算法,字符串查找块了100倍
    关闭Fedora防火墙
    gnome 屏幕截图
    zynq -- arm-xilinx-eabi-路径
    Fedora 14安装出现的错误
    fedora19安装后,需要安装的一些必备的软件包
    zynq -- cannot find -lxil
    Zynq -- 启动过程
  • 原文地址:https://www.cnblogs.com/dullbaby/p/4332033.html
Copyright © 2011-2022 走看看