可以使用Windows自带计划任务执行Receive操作。
控制面板=>管理工具 计划任务 =>创建计划任务 step : http://www.2cto.com/kf/201402/277337.html using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Messaging;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace ConsoleApplication26
{
class Program
{
private static int PostCoount { get; set; }
private static MessageQueue _queue; private static readonly string _queuePath = ".\\private$\\MSMQDemo";
private static MessageQueue Queue
{
get
{
if (_queue == null)
{
_queue = new MessageQueue(_queuePath)
{
Formatter = new XmlMessageFormatter(new Type[] { typeof(User) })
}; Debug.WriteLine("Connect To " + _queuePath + " Done!");
} return _queue;
}
}
static void Main(string[] args)
{ //Thread[] threads = new Thread[200];
//for (int i = 0; i < 200; i++)
//{ // ParameterizedThreadStart threadStart = new ParameterizedThreadStart(Send);
// Thread thread = new Thread(threadStart) { IsBackground = true };
// threads[i] = thread; // User user = new User() { ID = 1, Phone = "15689", Name = "张三" };
// thread.Start(user);
//} User u = new User();
u.Name = "ls";
u.Phone = ""; Send(u); //ThreadStart ttStart = new ThreadStart(Receive);
//Thread t = new Thread(ttStart);
//t.IsBackground = true;
//t.Start(); /*挂起Supend()*/
//for (int i = 0; i < PostCoount; i++)
//{
//while (true)
//{
// Receive();
//} //}
/*恢复*/ Console.WriteLine("完成"); Console.ReadLine(); } public static int Add(User user)
{
string sql = @"
insert into [User] values ('" + user.Phone + "','" + user.Name + "') "; string conStr = "data source=192.168.1.10;initial catalog=xiaocainiao;user id=guakao;password=wrx.362114";
int i = ; using (SqlConnection connect = new SqlConnection(conStr))
{
connect.Open();
SqlCommand cmd = new SqlCommand(sql, connect);
cmd.ExecuteNonQuery();
i++;
} return i;
} /// <summary>
/// Send
/// </summary>
private static void Send(object user)
{ for (var i = ; i <= ; i++)
{
//var message = new Message();
System.Messaging.Message message = new System.Messaging.Message();
message.Body = (User)user;
message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(User) });
Queue.Send(message); }
} /// <summary>
/// Receive
/// </summary>
private static void Receive()
{
//MessageQueue myQueue = new MessageQueue(".\\private$\\MSMQDemo");
//myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(User) }); //从队列中接收消息
Message myMessage = Queue.Receive();
myMessage.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(User) });
User context = myMessage.Body as User; //获取消息的内容
Add(context); }
}
}