zoukankan      html  css  js  c++  java
  • C# 一个多线程操作控件的例子.#######

    //多线程间 控件的操作例子
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
    using System.Data.SqlClient;
    using System.Collections;

    namespace AutoMessager
    {
        
    delegate void myDelegate();
        
    delegate void SetTextCallback(string text);

        
    public partial class frmAutoMsg : Form
        {
            
    event myDelegate myEvent;
            
    string connStr = string.Empty;
            Thread thd;
            
    //private Icon eyeIcon;        

            
    //private NotifyIconEx notifyIconA;
            
    //private NotifyIconEx notifyIconB;
            private bool canClosed = false

            
    public frmAutoMsg()
            {
                
    this.ShowInTaskbar = false;
                InitializeComponent();
                
    //eyeIcon = new Icon(GetType(), "EYE.ICO");
                notifyIcon1.ContextMenu = contextMenuB; 
            }

            
    private void SetText(string text)
            {
                
    // InvokeRequired required compares the thread ID of the
                
    // calling thread to the thread ID of the creating thread.
                
    // If these threads are different, it returns true.
                if (this.txtMsgStatus.InvokeRequired)
                {
                    SetTextCallback d 
    = new SetTextCallback(SetText);
                    
    this.Invoke(d, new object[] { text });
                }
                
    else
                {
                    
    this.txtMsgStatus.Text += text;
                }
            }


            
    private void frmAutoMsg_Load(object sender, EventArgs e)
            {            
                connStr 
    = System.Configuration.ConfigurationManager.AppSettings["ConnString"];
                thd 
    = new Thread(new ThreadStart(doEvent));
                thd.IsBackground 
    = true;
                thd.Start();
                
    //doEvent();            
                
    //notifyIcon1.Visible = true;            
            }

            
    /// <summary>
            
    /// 员工合同到期提醒
            
    /// </summary>
            void UpUserState()
            {
                SqlConnection conn 
    = null;
                DateTime now 
    = DateTime.Now;
                SqlTransaction tran 
    = null;
                SqlDataReader dr 
    = null;
                
    try
                {
                   
    //数据库操作部分省略

                    SetText(
    " 系统提示: 职员合同消息更新成功! ");
                   SetText(
    "执行时间:" + now.ToString("yyyy-MM-dd HH:mm:ss"+ " ");

                   
                }
                
    catch (Exception ex)
                {
                    dr.Close();
                    tran.Rollback();
                    SetText(
    " 系统错误: 职员合同消息更新错误:" + ex.Message + " ");
                    SetText(
    "执行时间:" + now.ToString("yyyy-MM-dd HH:mm:ss"+ " ");
                }
                
    finally
                {
                    dr.Close();
                    conn.Close();
                    conn.Dispose();             
                }
            }
            
    /// <summary>
            
    /// 采购及供货 到货提醒
            
    /// </summary>
            void UpCaiGou()
            {
                SqlConnection conn 
    = null;
                DateTime now 
    = DateTime.Now;
                SqlTransaction tran 
    = null;
                SqlDataReader dr 
    = null;
                
    try
                {
                    
    //数据库操作部分省略

                    SetText(
    "系统提示: 合同采购消息更新成功! ");
                    SetText(
    "执行时间:" + now.ToString("yyyy-MM-dd HH:mm:ss"+ " ");


                }
                
    catch (Exception ex)
                {
                    dr.Close();
                    tran.Rollback();
                    SetText(
    "系统错误: 合同采购消息更新错误:" + ex.Message + " ");
                    SetText(
    "执行时间:" + now.ToString("yyyy-MM-dd HH:mm:ss"+ " ");
                }
                
    finally
                {
                    dr.Close();
                    conn.Close();
                    conn.Dispose();
                }           
            }

            
    /// <summary>
            
    /// 供货收款情况提醒
            
    /// </summary>
            void GetMoney()
            {
                SqlConnection conn 
    = null;
                DateTime now 
    = DateTime.Now;
                
    try
                {
                   
    //数据库操作部分省略
                   SetText("系统提示: 供货付款消息更新成功! ");
                   SetText(
    "执行时间:" + now.ToString("yyyy-MM-dd HH:mm:ss"+ " ");
                }
                
    catch (Exception ex)
                {
                    SetText(
    "系统错误: 供货付款消息更新错误:" + ex.Message + " ");
                    SetText(
    "执行时间:" + now.ToString("yyyy-MM-dd HH:mm:ss"+ " ");
                }
                
    finally
                {
                    conn.Close();
                    conn.Dispose();
                }
            }

            
    void doEvent()
            {
                
    //int weather = int.Parse(weatherTime.Text);
                
    //int del = int.Parse(fileTime.Text);
                
    //            if(weather < 1 || weather > 24 || del < 1 || del > 24)
                
    //            {
                
    //                MessageBox.Show("时间输入有错!");
                
    //                button1.Enabled = true;
                
    //                return ;
                
    //            }
                while (true)
                {

                    
    //DateTime now = DateTime.Now;
                    int i = DateTime.Now.Hour;
                    
    if (i > 2 && i < 4)
                    {
                        myEvent 
    = new myDelegate(UpUserState);
                        myEvent 
    += new myDelegate(UpCaiGou);
                       
    // myEvent += new myDelegate(GetMoney);
                    }
                    
    //if (now.Hour == 3)
                    
    //{
                    
    //    myEventB = new myDelegate(deltemp);
                    
    //}
                    
    //if (myEventA != null) myEventA();
                    
    //if (myEventB != null) myEventB();
                    if (myEvent != null)
                    {
                        myEvent();
                        myEvent 
    = null;
                    }
                    Application.DoEvents();
                    
                    Thread.Sleep(
    6000000); //每100分钟检查一次时间
                }
            }

            
    private void frmAutoMsg_FormClosing(object sender, FormClosingEventArgs e)
            {
                
    if (canClosed == false)
                {
                    e.Cancel 
    = true;
                    
    this.Hide();
                    
    this.Visible = false;
                    
    //this.
                } 
            }

            
    private void menuItem2_Click(object sender, EventArgs e)
            {
                
    this.ShowInTaskbar = true;
                
    this.Show();
                
    this.Visible = true//恢复主窗体
            }

            
    private void menuItem1_Click(object sender, EventArgs e)
            {
                canClosed 
    = true;
                Application.Exit();
            }

            
    private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
            {
                
    this.ShowInTaskbar = true;
                
    this.Show();
                
    if (this.Visible == false)
                {
                    
    this.Visible = true;
                }

            }

            
    private void btnClear_Click(object sender, EventArgs e)
            {
                
    this.txtMsgStatus.Text = "";
            }

            
    private void btnUpMsg_Click(object sender, EventArgs e)
            {
                myEvent 
    = new myDelegate(UpUserState);
                myEvent 
    += new myDelegate(UpCaiGou);
                
    //myEvent += new myDelegate(GetMoney);

                
    if (myEvent != null)
                    myEvent();
            }          
        }
    }
  • 相关阅读:
    HTML5 学习04—— MathML数学标记
    HTML5 学习03——内联 SVG
    HTML5 学习02——新元素:canvas
    HTML5 学习01——浏览器问题、新元素
    HTML 回顾整理
    jQuery 学习05——AJAX:定义、load()方法、get()/post()方法
    jQuery 学习04——遍历:定义、向上、向下、同级、过滤
    jQuery 学习03——HTML:捕获、设置、添加元素、删除元素、CSS类、CSS()方法、尺寸
    UIDatePicker
    UIPikerView的属性
  • 原文地址:https://www.cnblogs.com/Fooo/p/798331.html
Copyright © 2011-2022 走看看