C#中线程的应用,循环查找某个表是否有记录
这个例子只作了一个线程,是用控制台的,也可以用到服务里...
这里只是提供个思路,如果要实际用到项目中,请了解线程的更多知识.
using System;
using System.Threading;
using System.Configuration;
namespace line
{
///
/// Class1 的摘要说明。
///
public class MutexTest
{
private Thread trd;
private static int iShowTime = 500;
public static void Main()
{
//数据库初始化
myCh.Common.SystemFramework.SystemFramework.ConnectionString = ConfigurationSettings.AppSettings["ConnSqlServer"];
myCh.Common.SystemFramework.SystemFramework.ConnectionType = ConfigurationSettings.AppSettings["DatabaseType"];
try
{
iShowTime = Convert.ToInt32(ConfigurationSettings.AppSettings["ShowTime"]);
}
catch
{}
Thread trd = new Thread(new ThreadStart(ThreadTask));
//trd.IsBackground = true;
trd.Start();
}
private static void ThreadTask()
{
int stp = 0;
while(true)
{
try
{
stp++;
EAE.EnterAndExitLogic.MsgTinterfaceData.MsgTinterfaceData myMsgCount = new EAE.EnterAndExitLogic.MsgTinterfaceData.MsgTinterfaceData();
int iCount = myMsgCount.MsgList().Rows.Count;
//如果一条也没有
if(iCount >0)
{
for(int i=1 ;i <= iCount;i++)
{
EAE.EnterAndExitLogic.MsgTinterfaceData.MsgTinterfaceData myMsg = new EAE.EnterAndExitLogic.MsgTinterfaceData.MsgTinterfaceData();
myMsg.main();
if(myMsg.Error.ToString().Trim() != "")
{
Console.WriteLine("{0}",myMsg.Error.ToString().Trim());
}
}
}
Console.WriteLine("/n{0}, 执行成功!",stp);
Thread.Sleep(iShowTime);
if (stp > 100000)
{
stp = 0;
}
}
catch(Exception ex)
{
Console.WriteLine("{0}",ex.ToString().Trim());
}
}
}
}
}