当前位置: 代码迷 >> ASP.NET >> .net 计时器解决方法
  详细解决方案

.net 计时器解决方法

热度:5566   发布时间:2013-02-25 00:00:00.0
.net 计时器
.net 的后台代码中可以用计时器吗?eg:Timer

------解决方案--------------------------------------------------------
你写xml文件的那段代码,最好是lock
类似:
C# code
private void WriteXML(string context){    lock    {      //你的代码逻辑    }}
------解决方案--------------------------------------------------------
C# code
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Collections;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Configuration;
using System.Threading;
using MySql.Data.MySqlClient;
namespace qxjshuju
{
  public partial class qxjshuju : Form
  {
    delegate void SetTextCallback(string text);
    //private Thread demoThread = null;
    //private BackgroundWorker backgroundWorker1;

    //用到的变量
    ArrayList Zm_list;
    ArrayList Zm_Name;
    ArrayList starttime;
    ArrayList endtime;
    ArrayList windstation;
    ArrayList windname;
    ArrayList windstime;
    ArrayList windetime;
    public string strtext;
    public bool sum;
    public int con;
    public bool st;
    System.Timers.Timer t;
    private MySqlConnection conn;
    private SqlConnection innerconn;
    private DataTable data;
    private MySqlDataAdapter da;
    private MySqlCommandBuilder cb;
    public qxjshuju()
    {
      InitializeComponent();
      sum = true;
      st = true;
      con = 0;
      this.lblxszt.Text = "服务运行中";
      if (st)
      {
        btnstart.Enabled = false;
      }
      t = new System.Timers.Timer(300000);//实例化Timer类,每5分种一次
      t.Elapsed += new System.Timers.ElapsedEventHandler(ReadData);//到达时间的时候执行事件;
      t.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
      t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;
      ReadData(null, null);
    }

    #region 线程数据提示语
    private void ThreadProcUnsafe()
    {
      this.txtbox.Text = "This text was set unsafely.";
    }

    private void ThreadProcSafe()
    {
      this.SetText("This text was set safely.");
    }
    #endregion

    #region 利用委托方法把内容显示在textbox上
    private void SetText(string text)
    {
      if (this.txtbox.InvokeRequired)
      {
        SetTextCallback d = new SetTextCallback(SetText);
        this.Invoke(d, new object[] { text });
      }
      else
      {
        this.txtbox.Text = text;
      }
    }
    #endregion

    #region 函数入口
    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
  相关解决方案