24b9dbd5a0f80d097d37167b1ce6624a9e55dd25.svn-base 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Runtime.Serialization.Formatters.Binary;
  6. using System.Data;
  7. using System.Runtime.Serialization;
  8. using System.IO.Compression;
  9. using System.Collections;
  10. using System.Threading;
  11. namespace Core.Mes.Common
  12. {
  13. public class Utility
  14. {
  15. #region dataset to byte[]
  16. /// <summary>
  17. /// 序列化数据集
  18. /// </summary>
  19. /// <param name="ds"></param>
  20. /// <returns></returns>
  21. public static byte[] SerializeDataSet(DataSet ds)
  22. {
  23. byte[] buffer = null;
  24. ds.RemotingFormat = SerializationFormat.Binary;
  25. MemoryStream ms = new MemoryStream();
  26. IFormatter bf = new BinaryFormatter();
  27. bf.Serialize(ms, ds);
  28. ds.Clear();
  29. ds.Dispose();
  30. ds = null;
  31. buffer = ms.ToArray();
  32. ms.Close();
  33. ms.Dispose();
  34. ms = null;
  35. return buffer;
  36. }
  37. /// <summary>
  38. /// 反序列化数据集
  39. /// </summary>
  40. /// <param name="buffer"></param>
  41. /// <returns></returns>
  42. public static DataSet ReSerializable(byte[] buffer)
  43. {
  44. MemoryStream ms = new MemoryStream(buffer);
  45. Array.Resize(ref buffer, 0);
  46. IFormatter bf = new BinaryFormatter();
  47. object obj = bf.Deserialize(ms);
  48. DataSet ds = (DataSet)obj;
  49. ms.Close();
  50. ms.Dispose();
  51. return ds;
  52. }
  53. /// <summary>
  54. /// 序列化对象
  55. /// </summary>
  56. /// <param name="ds"></param>
  57. /// <returns></returns>
  58. public static byte[] SerializeObject(Object obj)
  59. {
  60. byte[] buffer = null;
  61. MemoryStream ms = new MemoryStream();
  62. IFormatter bf = new BinaryFormatter();
  63. bf.Serialize(ms, obj);
  64. buffer = ms.ToArray();
  65. ms.Close();
  66. ms.Dispose();
  67. ms = null;
  68. return buffer;
  69. }
  70. /// <summary>
  71. /// 反序列化对象
  72. /// </summary>
  73. /// <param name="buffer"></param>
  74. /// <returns></returns>
  75. public static Object ReSerializeObject(byte[] buffer)
  76. {
  77. MemoryStream ms = new MemoryStream(buffer);
  78. IFormatter bf = new BinaryFormatter();
  79. object obj = bf.Deserialize(ms);
  80. ms.Close();
  81. ms.Dispose();
  82. return obj;
  83. }
  84. /// <summary>
  85. /// 序列化对象
  86. /// </summary>
  87. /// <param name="ds"></param>
  88. /// <returns></returns>
  89. public static byte[] SerializeAndCompressObject(Object obj)
  90. {
  91. byte[] buffer = null;
  92. MemoryStream ms = new MemoryStream();
  93. IFormatter bf = new BinaryFormatter();
  94. bf.Serialize(ms, obj);
  95. buffer = ms.ToArray();
  96. ms.Close();
  97. ms.Dispose();
  98. ms = null;
  99. return CompressBytes(buffer);
  100. }
  101. /// <summary>
  102. /// 反序列化对象
  103. /// </summary>
  104. /// <param name="buffer"></param>
  105. /// <returns></returns>
  106. public static Object ReSerializeCompressedObject(byte[] buffer)
  107. {
  108. MemoryStream ms = new MemoryStream(Decompress(buffer));
  109. IFormatter bf = new BinaryFormatter();
  110. object obj = bf.Deserialize(ms);
  111. ms.Close();
  112. ms.Dispose();
  113. return obj;
  114. }
  115. #endregion
  116. #region 压缩、解压缩
  117. //压缩字节
  118. public static byte[] CompressBytes(byte[] bytes)
  119. { /* 1.创建压缩的数据流
  120. 2.设定compressStream为存放被压缩的文件流,并设定为压缩模式
  121. 3.将需要压缩的字节写到被压缩的文件流
  122. */
  123. using (MemoryStream compressStream = new MemoryStream())
  124. {
  125. using (GZipStream zipStream = new GZipStream(compressStream, CompressionMode.Compress))
  126. zipStream.Write(bytes, 0, bytes.Length);
  127. Array.Resize(ref bytes, 0);
  128. return compressStream.ToArray();
  129. }
  130. }
  131. //解压缩字节
  132. public static byte[] Decompress(byte[] bytes)
  133. {
  134. /* 1.创建被压缩的数据流
  135. 2.创建zipStream对象,并传入解压的内存流
  136. 3.创建目标流
  137. 4.zipStream拷贝到目标流
  138. 5.返回目标流输出字节
  139. * */
  140. byte[] buffer = new byte[4096];//定义数据缓冲
  141. using (MemoryStream compressStream = new MemoryStream(bytes))
  142. {
  143. using (GZipStream zipStream = new GZipStream(compressStream, CompressionMode.Decompress))
  144. {
  145. using (MemoryStream resultStream = new MemoryStream())
  146. {
  147. int offset = 0;
  148. while ((offset = zipStream.Read(buffer, 0, buffer.Length)) != 0)
  149. {
  150. resultStream.Write(buffer, 0, offset);
  151. }
  152. return resultStream.ToArray();
  153. }
  154. }
  155. }
  156. }
  157. //////////////////////////////////////////////////////////////////////////
  158. ///读取Stream
  159. //////////////////////////////////////////////////////////////////////////
  160. public static int ReadAllBytesFromStream(Stream stream, byte[] buffer)
  161. {
  162. int offset = 0;
  163. int totalCount = 0;
  164. while (true)
  165. {
  166. int bytesRead = stream.Read(buffer, offset, 100);
  167. if (bytesRead == 0)
  168. {
  169. break;
  170. }
  171. offset += bytesRead;
  172. totalCount += bytesRead;
  173. }
  174. return totalCount;
  175. }
  176. #endregion
  177. }
  178. public class TaskManager
  179. {
  180. private Thread taskMonitor = new Thread(new ThreadStart(Kill_InvalidTask));
  181. public static Hashtable Tasks = new Hashtable();
  182. public bool CreateTask(TaskType taskType, object param, out string outmsg)
  183. {
  184. outmsg = "";
  185. TaskInfo ti = null;
  186. do
  187. {
  188. ti = new TaskInfo();
  189. } while (Tasks.Contains(ti.UniqueID));
  190. if (!ti.CreateTask(taskType, param, out outmsg))
  191. {
  192. return false;
  193. }
  194. Tasks.Add(ti.UniqueID, ti);
  195. return true;
  196. }
  197. public void SetTouch(String TaskID)
  198. {
  199. if (Tasks.Contains(TaskID))
  200. {
  201. TaskInfo ti = (TaskInfo)Tasks[TaskID];
  202. ti.SetTouch();
  203. }
  204. }
  205. public static void Kill_InvalidTask()
  206. {
  207. do
  208. {
  209. Thread.Sleep(30000);
  210. try
  211. {
  212. string outmsg = "";
  213. DateTime time_now = DateTime.Now;
  214. foreach (string key in Tasks.Keys)
  215. {
  216. try
  217. {
  218. TaskInfo ti = (TaskInfo)(Tasks[key]);
  219. if (ti.PlanEndTime < time_now)
  220. {
  221. if (ti.EndTask(out outmsg))
  222. {
  223. Tasks.Remove(key);
  224. }
  225. }
  226. }
  227. catch { }
  228. //继续判断其他任务
  229. }
  230. }
  231. catch { }
  232. } while (true);
  233. }
  234. }
  235. public class TaskInfo
  236. {
  237. TimeSpan _keepTimes = new TimeSpan(0, 15, 0);
  238. DateTime _startTime;
  239. public DateTime StartTime
  240. {
  241. get { return _startTime; }
  242. }
  243. DateTime _touchTime;
  244. public DateTime LastTouchTime
  245. {
  246. get { return _touchTime; }
  247. }
  248. DateTime _planEndTime;
  249. public DateTime PlanEndTime
  250. {
  251. get { return _planEndTime; }
  252. }
  253. string _id;
  254. public String UniqueID
  255. {
  256. get { return _id; }
  257. }
  258. TaskType _taskType;
  259. object _parameter;
  260. public TaskInfo(string _ID)
  261. {
  262. SetID(_ID);
  263. _startTime = DateTime.Now;
  264. _touchTime = _startTime;
  265. _keepTimes = new TimeSpan(0, 15, 0);
  266. _planEndTime = _touchTime.Add(_keepTimes);
  267. }
  268. public TaskInfo()
  269. {
  270. _startTime = DateTime.Now;
  271. _touchTime = _startTime;
  272. _keepTimes = new TimeSpan(0, 15, 0);
  273. SetID(_startTime.Ticks.ToString());
  274. _planEndTime = _touchTime.Add(_keepTimes);
  275. }
  276. public void SetID(string _ID)
  277. {
  278. if (string.IsNullOrEmpty(_ID)) return;
  279. _id = _ID.ToString();
  280. }
  281. public void SetTouch()
  282. {
  283. _touchTime = DateTime.Now;
  284. _planEndTime = _touchTime.Add(_keepTimes);
  285. }
  286. public void SetKeepTime(TimeSpan ts)
  287. {
  288. if (ts.Ticks <= 0) return;
  289. _keepTimes = ts;
  290. _planEndTime = _touchTime.Add(_keepTimes);
  291. }
  292. public bool CreateTask(TaskType taskType, object param, out string outmsg)
  293. {
  294. bool success = false;
  295. outmsg = "";
  296. switch (taskType)
  297. {
  298. case TaskType.SendFile:
  299. if (param.GetType() != typeof(string))
  300. {
  301. outmsg = "设置[文件发送]任务失败!,参数格式不正确!";
  302. return success;
  303. }
  304. string p = ((string)(param)).Trim();
  305. if (string.IsNullOrEmpty(p) || !File.Exists(p))
  306. {
  307. outmsg = "设置[文件发送]任务失败!,文件不存在!";
  308. return success;
  309. }
  310. _taskType = taskType;
  311. _parameter = param;
  312. success = true;
  313. break;
  314. default:
  315. break;
  316. }
  317. return success;
  318. }
  319. public bool EndTask(out string outmsg)
  320. {
  321. bool success = false;
  322. outmsg = "";
  323. switch (_taskType)
  324. {
  325. case TaskType.SendFile:
  326. string p = ((string)(_parameter)).Trim();
  327. if (File.Exists(p))
  328. {
  329. try
  330. {
  331. File.Delete(p);
  332. }
  333. catch (Exception ex)
  334. {
  335. outmsg = "删除[文件发送]任务失败!\n" + ex.Message;
  336. }
  337. }
  338. success = true;
  339. break;
  340. default:
  341. break;
  342. }
  343. return success;
  344. }
  345. }
  346. public enum TaskType { SendFile = 1 };
  347. }