背景

  需要Client跑服务在终端间隔执行任务,我的目标是运行在树莓派上

Client代码

如果未连接成功时隔3秒重新连接服务器

        public static void Reconnect()
{
var isLink = false;
do
{
Thread.Sleep(3000);//3秒
isLink = Signalr().GetAwaiter().GetResult();
} while (!isLink);
}

  通过命令开启 Quartz 调度器

        public static async Task<bool> Signalr()
{
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start(); // 开始监视代码运行时间
var urlstr = "https://localhost:44363//hubs";
var url = new Uri(urlstr);
var connection = new HubConnectionBuilder()
.WithUrl(url)
.Build();
hubConnection = connection;
connection.On<int, string>("SendCmd", (cmd, data) =>
{
switch (cmd)
{
case 10001:
if (scheduler == null)
{
InJobAsync().Wait();
}
else
{
if (scheduler.IsStarted != true)
{
InJobAsync().Wait();
}
else
{ connection.SendAsync("ClientMsg", "正在运行");
}
} break;
case 10010:
var send = 10;
Int32.TryParse(data, out send);
if (scheduler != null)
scheduler.Clear();
InJobAsync(send).GetAwaiter().GetResult();
break;
default:
connection.SendAsync("ClientMsg", "未知命令");
break;
}
});
connection.Closed += e =>
{
if (e != null)
{
Console.WriteLine("Connection closed..." + e.Message);
}
else
{
Console.WriteLine("开始重连..." + e.Message); }
Reconnect();
return Task.CompletedTask;
};
try
{
Console.WriteLine($"开始连接服务器[{urlstr}]");
await connection.StartAsync();
stopwatch.Stop(); // 停止监视
TimeSpan timespan = stopwatch.Elapsed; // 获取当前实例测量得出的总时间
double seconds = timespan.TotalSeconds; // 总秒数
Console.WriteLine($"耗时:{seconds} 服务器[{urlstr}]连接成功!");
return true;
}
catch (Exception err)
{
stopwatch.Stop(); // 停止监视
TimeSpan timespan = stopwatch.Elapsed; // 获取当前实例测量得出的总时间
double seconds = timespan.TotalSeconds; // 总秒数
var meg = $"执行时间:{seconds} 连接失败:{err.Message}";
Console.WriteLine(meg);
return false;
}
}

  

public static StdSchedulerFactory factory = null;
public static IScheduler scheduler = null;
public static async Task InJobAsync(int iInterval = 10)
{ if (factory == null)
{
factory = new StdSchedulerFactory();
}
if (scheduler == null)
{
scheduler = await factory.GetScheduler();
} // 启动任务调度器
await scheduler.Start(); IJobDetail job = JobBuilder.Create<FlowerJob>()
.WithIdentity("job1", "group1")
.WithDescription("定时数据传送")
.Build();
ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create()
.WithIdentity("trigger1") // 给任务一个名字
//.StartAt(myStartTime) // 设置任务开始时间
.ForJob("job1", "group1") //给任务指定一个分组
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(iInterval) //循环的时间
.RepeatForever())
.Build(); // 开启任务调度器
await scheduler.ScheduleJob(job, trigger); }

  

Jobs任务

public class FlowerJob : IJob
{
public Task Execute(IJobExecutionContext context)
{
//Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}执行任务:"+context.JobDetail.Description); //逻辑代码 return Task.CompletedTask;
}
}

  

最新文章

  1. Web前端需要熟悉大学里【高大上】的计算机专业课吗?
  2. [SAP ABAP开发技术总结]选择屏幕——各种屏幕元素演示
  3. angularjs的四大特征
  4. Linux内核源码详解——命令篇之iostat[zz]
  5. 实现简单ThreadPool
  6. 百度搜索API v3版本与soap
  7. python(7)&ndash; 类的反射
  8. 时间格式nls_date_format的设置
  9. Android OpenGL ES 3.0 纹理应用
  10. BIND9的架构与机制笔记1
  11. 深入分析Java Web开发
  12. 02-Go语言数据类型与变量
  13. [BZOJ1212][HNOI2004]L语言
  14. 5. VIM 系列 - 文件管理
  15. DSAPI之摄像头追踪指定颜色物体
  16. JSOI2020备考知识点复习
  17. python3自学第二天,模块,三元运算
  18. linux cp 命令详解
  19. Centos expect spawn、linux expect 用法
  20. React 动画 Animation

热门文章

  1. 多版本切换python
  2. this.$route和this.$router区别
  3. Django 简单评论实现
  4. spring-cloud-kubernetes服务发现之在k8s环境下开发spring cloud应用
  5. 登录-退出,在T分钟实现BC次用户登录退出,单次登录-退出%90用户时间t,需要的并发用户(线程)
  6. 最易用的 Android HTTP library
  7. day15_7.17正则表达式与re模块
  8. 微信小程序 - 组件 | 自定义组件 | 组件事件传递页面
  9. [Taro] 解决 使用 Taro UI 小程序下 Iconfont 图标 不显示问题
  10. MACbook安装WIN7中文版后乱码的解决办法