1、类QueueMessage
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CHEER.Common
{
public class QueueMessage
{
public string DeliveryTag { get; set; }
public string ConsumerTag { get; set; }
public Constant Type { get; set; }
public string Message { get; set; }
}
}
2、枚举Constant
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CHEER.Common
{
public enum Constant
{
ProcessStopRun
}
public class TimeConstant
{
public static readonly int ProcessDevice = int.Parse(System.Configuration.ConfigurationManager.AppSettings["ProcessDeviceMinutes"]) * 60;
}
}
3、类AsyncThreadHelper
using CHEER.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
namespace CHEER.PresentationLayer
{
public class AsyncThreadHelper
{
private static Queue<QueueMessage> _Queue;
private static Thread _processThread;
static AsyncThreadHelper()
{
_Queue = new Queue<QueueMessage>();
_processThread = new Thread(Process);
_processThread.IsBackground = true;
_processThread.Start();
}
// 处理队列中的任务
private static void Process()
{
while (true)
{
AsyncThread();
}
}
private static void AsyncThread()
{
if (_Queue.Count > 0)
{
QueueMessage queueMessage = _Queue.Dequeue();
CHEER.Common.Log2.Instance.Debug(typeof(AsyncThreadHelper), "开始处理队列");
switch (queueMessage.Type)
{
case Constant.ProcessStopRun:
new ProcessStopRun(queueMessage.Message).DoJob();
break;
default:
//未知任务, 睡眠0.01s一会
System.Threading.Thread.Sleep(10);
break;
}
}
}
static bool isFirst = true;
// 公开一个Add方法,用于向队列中添加内容然后供外部调用
public static void Add(QueueMessage queueMessage)
{
_Queue.Enqueue(queueMessage);
if (isFirst)
{
isFirst = false;
}
}
}
}
4、接口IProcess
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CHEER.PresentationLayer
{
interface IProcess
{
void DoJob();
}
}
5、操作类ProcessStopRun
using CHEER.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
namespace CHEER.PresentationLayer
{
public class ProcessStopRun:IProcess
{
private DeviceData deviceData;
public ProcessStopRun(string message)
{
this.deviceData = JsonConvert.DeserializeObject<DeviceData>(message) ;
}
public void DoJob()
{
var requestControl = new Dictionary<string, object>
{
["deviceId"] = deviceData.deviceId,
["json"] = deviceData.jsonString
};
eApi.ThirdCloudRequestController third = new eApi.ThirdCloudRequestController();
Dictionary<string,object> result= third.controldev(requestControl);
Log2.Instance.Info(GetType(),"异步调用停止命令返回结果:"+JsonConvert.SerializeObject(result));
}
public string UploadData(string url, string data)
{
var response = "";
using (var client = new WebClient())
{
client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
client.Encoding = Encoding.UTF8;
response = client.UploadString(url, data);
}
return response;
}
public class DeviceData
{
public string deviceId { get; set; }
public string jsonString { get; set; }
}
}
}
6.开始使用
var requestControl = new Dictionary<string, object>
{
["deviceId"] = deviceId,
["jsonString"] = jsonString
};
QueueMessage queueMessage = new QueueMessage();
queueMessage.Type = Constant.ProcessStopRun;
queueMessage.Message = JsonConvert.SerializeObject(requestControl);
版权声明:本文为qq_35913274原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。