使用Timer新线程定时查询数据库

定时从服务器的指定表取数据。后台线程,不占用主线程资源。
注意:
返回的数据,如果要写入UI控件,请关闭线程的验证,或者使用Invoke
标签: <无>

代码片段(1)[全屏查看所有代码]

1. [文件] DatabaseChange.cs ~ 5KB     下载(0)     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.Data;
using System.Data.SqlClient;
 
using jd.code.DAO;
 
namespace jd.Class
{
     /// <summary>
     /// 检测数据库是否有更新
     /// </summary>
     public class DatabaseChange
     {
         #region 0---公共变量
         private System.Timers.Timer timer = new System.Timers.Timer(500);
         private SqlConnection sqlConnection;
         #endregion
 
         #region 1---构造函数
         public DatabaseChange(SqlConnection sqlConnection)
         {
             this .State = false ;
             this .sqlConnection = sqlConnection;
             timer.AutoReset = true ;
             timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
 
         }
         #endregion
 
         #region 2---数据读写器与变量
         //=================================================================
         /// <summary>
         /// 程序是否在运行
         /// </summary>
         public bool State
         {
             get { return _State; }
             set { _State = value; }
         }
         private bool _State;
 
         //=================================================================
         /// <summary>
         /// 返回的数据[0]
         /// </summary>
         public bool DBValue
         {
             get { return _DBValue; }
             set { _DBValue = value; }
         }
         private bool _DBValue;
 
         //=================================================================
         /// <summary>
         /// 返回的数据[1]
         /// </summary>
         public string DBTableName
         {
             get { return _DBTableName; }
             set { _DBTableName = value; }
         }
         private string _DBTableName;
         #endregion
 
         #region 3---委托与事件
         public delegate void SetValues( bool value, string tableName); //设置当前数据的委托
 
         /// <summary>
         /// 数据更改处理委托
         /// </summary>
         public delegate void dbChangeEventHandler( object sender, dbChangeArgs e);
 
         /// <summary>
         /// 数据更改时处理
         /// </summary>
         public event dbChangeEventHandler dbChange;
         #endregion
 
         #region 4---方法
         //=================================================================
         /// <summary>
         /// 时钟间隔引发的事件进行处理
         /// </summary>
         private void timer_Elapsed( object sender, ElapsedEventArgs e)
         {
             try
             {
                 OpenConnection(); //打开数据库连接
                 string sql = @"select top 1 * from DBC" ;
                 SqlDataReader tmpSDR = SqlHelper.ExecuteReader(SqlHelper.GetConnection(), CommandType.Text, sql);
                 if (tmpSDR.Read())
                 {
                     SetThisValues(( bool )tmpSDR[0], tmpSDR[1].ToString());
                 }
                 tmpSDR.Close();
             }
             catch (Exception)
             {
                 throw ;
             }
         }
 
         //=================================================================
         /// <summary>
         /// 打开数据库连接
         /// </summary>
         private void OpenConnection()
         {
             try
             {
                 if (sqlConnection != null )
                 {
                     if (sqlConnection.State == ConnectionState.Closed)
                     {
                         sqlConnection.Open();
                     }
                     else if (sqlConnection.State == ConnectionState.Broken)
                     {
                         sqlConnection.Close();
                         sqlConnection.Open();
                     }
                 }
             }
             catch (Exception)
             {
                 throw ;
             }
         }
 
         //=================================================================
         private void SetThisValues( bool value, string tableName)
         {
             if ( this .DBValue == value && this .DBTableName == tableName)
             {
 
             }
             else
             {
                 this .DBValue = value;
                 this .DBTableName = tableName;
                 dbChange( null , new dbChangeArgs(value, tableName));
             }
         }
 
         #endregion
 
         #region 5------主方法------
         /// <summary>
         /// 启动或暂停
         /// </summary>
         public void Run()
         {
             if (timer.Enabled)
             {
                 timer.Stop();
                 this .State = false ;
             }
             else
             {
                 timer.Start();
                 this .State = true ;
             }
         }
 
         #endregion
 
     }
 
     /// <summary>
     /// 定义事件数据
     /// </summary>
     public class dbChangeArgs : EventArgs
     {
         public dbChangeArgs( bool value, string tableName)
         {
             this .Value = value;
             this .TableName = tableName;
         }
         public bool Value { get ; private set ; }
         public string TableName { get ; set ; }
     }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值