111,126
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.Serialization;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using System.Runtime.Serialization.Json;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private SafeQueue<JobDetail> TestQueue = new SafeQueue<JobDetail>();
public Form1()
{
InitializeComponent();
}
private void btnEnqueue_Click(object sender, EventArgs e)
{
for (int i = 0; i < int.Parse(txtEnQueue.Text.Trim()); i++)
{
SendJobInfo();
}
MessageBox.Show("Done!");
}
private void btnDequeue_Click(object sender, EventArgs e)
{
for (int i = 0; i < int.Parse(txtDequeue.Text.Trim()); i++)
{
var j = TestQueue.Dequeue();
}
MessageBox.Show("Done!");
}
public void SendJobInfo()
{
string sInfo = "{\"JobID\":\"" + System.Guid.NewGuid().ToString() + "\",";
sInfo += "\"JobNum\":2,";
sInfo += "\"MsgType\":\"Job_info\",";
sInfo += "\"UserName\":\"takako_mu\",";
sInfo += "\"DocName\":\"Test.txt\",";
sInfo += "\"SubmitTime\":\"" + DateTime.Now.ToString() + "\"}";
var jobDetail = Utility.DeSerializerFromJson<JobDetail>(sInfo);
TestQueue.TryEnqueue(jobDetail, 10);
}
}
public class SafeQueue<T>
{
// A queue that is protected by Monitor.
private Queue<T> m_inputQueue = new Queue<T>();
// Lock the queue and add an element.
public void Enqueue(T qValue)
{
// Request the lock, and block until it is obtained.
Monitor.Enter(m_inputQueue);
try
{
// When the lock is obtained, add an element.
m_inputQueue.Enqueue(qValue);
}
finally
{
// Ensure that the lock is released.
Monitor.Exit(m_inputQueue);
}
}
// Try to add an element to the queue: Add the element to the queue
// only if the lock is immediately available.
public bool TryEnqueue(T qValue)
{
// Request the lock.
if (Monitor.TryEnter(m_inputQueue))
{
try
{
m_inputQueue.Enqueue(qValue);
}
finally
{
// Ensure that the lock is released.
Monitor.Exit(m_inputQueue);
}
return true;
}
else
{
return false;
}
}
// Try to add an element to the queue: Add the element to the queue
// only if the lock becomes available during the specified time
// interval.
public bool TryEnqueue(T qValue, int waitTime)
{
// Request the lock.
if (Monitor.TryEnter(m_inputQueue, waitTime))
{
try
{
m_inputQueue.Enqueue(qValue);
}
finally
{
// Ensure that the lock is released.
Monitor.Exit(m_inputQueue);
}
return true;
}
else
{
return false;
}
}
// Lock the queue and dequeue an element.
public T Dequeue()
{
T retval;
// Request the lock, and block until it is obtained.
Monitor.Enter(m_inputQueue);
try
{
// When the lock is obtained, dequeue an element.
if (m_inputQueue.Count > 0)
{
retval = m_inputQueue.Dequeue();
m_inputQueue.TrimExcess();
}
else
retval = default(T);
}
finally
{
// Ensure that the lock is released.
Monitor.Exit(m_inputQueue);
}
return retval;
}
public void Clear()
{
m_inputQueue.Clear();
}
}
[DataContract]
public class JobDetail
{
/// <summary>
/// Job ID (GUID) QueueID
/// </summary>
[DataMember(Name = "JobID")]
public string JobID { get; set; }
/// <summary>
/// Message Type (Not Used)
/// </summary>
[DataMember(Name = "MsgType")]
public string MsgType { get; set; }
/// <summary>
/// Document Name
/// </summary>
[DataMember(Name = "DocName")]
public string DocName { get; set; }
/// <summary>
/// User name
/// </summary>
[DataMember(Name = "UserName", IsRequired = true)]
public string UserName { get; set; }
[DataMember(Name = "SubmitTime")]
public string SubmitTime { get; set; }
}
public class Utility
{
public static string GetSerializerXml<T>(T t)
{
var xmlSerializer = new XmlSerializer(t.GetType());
var memStream = new MemoryStream();
var xmlTextWriter = new XmlTextWriter(memStream, Encoding.UTF8);
xmlSerializer.Serialize(xmlTextWriter, t);
memStream = xmlTextWriter.BaseStream as MemoryStream;
if (memStream != null)
{
var utf8Encoding = new UTF8Encoding();
string strXml = utf8Encoding.GetString(memStream.ToArray());
return strXml;
}
xmlTextWriter.Close();
return string.Empty;
}
public static string GetSerializerJson<T>(T t)
{
var jsonSerializer = new DataContractJsonSerializer(typeof(T));
using (MemoryStream memStream = new MemoryStream())
{
jsonSerializer.WriteObject(memStream, t);
if (memStream != null)
{
var utf8Encoding = new UTF8Encoding();
string strJson = utf8Encoding.GetString(memStream.ToArray());
return strJson;
}
}
return string.Empty;
}
public static T DeSerializerFromXml<T>(string xml)
{
var xmlSerializer = new XmlSerializer(typeof(T));
var utf8Encoding = new UTF8Encoding();
MemoryStream memStream = new MemoryStream(utf8Encoding.GetBytes(xml));
var t = (T)xmlSerializer.Deserialize(memStream);
memStream.Close();
return t;
}
public static T DeSerializerFromJson<T>(string json)
{
var jsonSerializer = new DataContractJsonSerializer(typeof(T));
var utf8Encoding = new UTF8Encoding();
MemoryStream memStream = new MemoryStream(utf8Encoding.GetBytes(json));
var t = (T)jsonSerializer.ReadObject(memStream);
memStream.Close();
return t;
}
}
}
学习了