1.命名空间
namespace DatabaseSynchronization.HRApplication
{
public class HRCampusRecruitment
{
//入口
while (true)
{
if (DateTime.Now.Hour == 14)
{
if (FeishuRecruitmentSchool()) // 如果执行成功
{
Thread.Sleep(1000 * 60 * 60 * 24); // 等待到明天下午3点再次运行
}
else
{
// 如果执行失败,则等待一段时间后重新尝试
Thread.Sleep(1000 * 60 * 5); // 等待5分钟后重新尝试
}
}
else
{
Thread.Sleep(1000 * 60 * 10);//检查一次时间
}
}
}
public static bool FeishuRecruitmentSchool()
{
try
{
//写业务逻辑
}
catch (Exception ex)
{
Log.Info("》》" + ex.Message);
var mailAdministrator = "".Split(',');
var mailSubject = "Windows服务运行出错!" + "(" + DateTime.Now.ToString("yyyy-MM-dd") + ")";
var mailBody = "运行‘" + "<br><br>" + ex.Message;
EmailSender.SendMail(mailAdministrator, null, mailSubject, mailBody, null);
return false;
throw;
}
return true;
}
}
2.线程池
namespace DatabaseSynchronization
{
//线程定义
partial class SyncService : ServiceBase
{
private readonly Thread _th5 = new Thread(PlcSynchronizer.Run);
}
//构造函数
public SyncService()
{
InitializeComponent();
}
//线程开始
protected override void OnStart(string[] args)
{
_th5.Start();
}
//线程结束
protected override void OnStop()
{
_th5.Abord();
}
}
3.数据库直连
public string GetAbnormalStatus(string socode)
{
if (string.IsNullOrWhiteSpace(socode))
{
return "";
}
string connectString = "Data Source=IP地址;Initial Catalog=数据库名;Persist Security Info=True;User ID=账号;Password=密码";
using (SqlConnection sqlCnt = new SqlConnection(connectString))
{
sqlCnt.Open();
using (SqlCommand command = new SqlCommand())
{
command.Connection = sqlCnt;
command.CommandType = System.Data.CommandType.Text;
command.CommandText = @"select AbnormalStatus from 表 WHERE SoCode = @socode";
command.Parameters.AddWithValue("@socode", socode);
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
var state = reader["AbnormalStatus"].ToString();
if (state == "1")
{
return "正常";
}
else if (state == "2")
{
return "暂停";
}
else if (state == "3")
{
return "取消";
}
else
{
return "正常";
}
}
}
}
}
return "";
}
1655

被折叠的 条评论
为什么被折叠?



