我有一个客户,我们为他开发了大约10个Asp.net网站。每个站点都有自己的订单创建方式。我希望有一个通用的方法来处理每个站点中的订单。目前我正在循环所有站点并检查是否有任何订单等待处理(在控制台应用程序中)。这似乎不是一种有效的方法。
就像,一个网站有大约1000个订单等待处理,我的工作将等到它完成后,其他网站的订单将等待。
我想到了以下事情
有没有建议以更好的方式实施它?
我的网站是使用Asp.Net网络表单,c#.net和SQL Server 2008
开发的答案 0 :(得分:3)
"Async" in a very eaaaaaaaaaaaaasy way, by using my way: Async.cs
Usage>
/* ___________________ Summary ___________________ */
Async.GetDataAsync<List<double>>(GetSomeDoubles, DoubleArrived);// DoubleArrived takes List<double> as param
Async.DoAsync(doSomethingIns, doNextthingIns); // both are voids
/* ___________________ Details ___________________ */
/* ========= GetAsync ========= */
Async.GetDataAsync<List<int>>(
() =>
{
//Code as you like
System.Threading.Thread.Sleep(2000);
return Enumerable.Range(0, 10).ToList();
},
(List<int> data) =>
{
MessageBox.Show(data.Count.ToString());
});
/* ========= GetAsync ========= */
Async.GetDataAsync<object>(
() =>
{
//Code as you like
System.Threading.Thread.Sleep(2000);
return 5;
},
delegate (object data)
{
MessageBox.Show(data.ToString());
});
/* ======== Do some thing Async ======== */
Async.DoAsync(
() =>
{
//Code as you like
System.Threading.Thread.Sleep(3000);
},
() =>
{
MessageBox.Show("You did it async :)");
});
/* ======== Do some thing Async ======== */
Async.DoAsync(
() =>
{
//Code as you like
System.Threading.Thread.Sleep(3000);
},
delegate()
{
MessageBox.Show("You did it async again :)");
});
Async类:Async.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.ComponentModel;
public class Async
{
public class DataByThreadWorker<T>
{
private BackgroundWorker bw = null;
public delegate T GetDataDelegate();
public GetDataDelegate getDataIns;
public delegate void CallCompleted(T data);
public CallCompleted dataCompletedIns;
public delegate void DoSomething();
public DoSomething doSomethingIns;
public DoSomething doNexthingIns;
public void DoAsync()
{
if (doSomethingIns != null)
{
bw = new BackgroundWorker();
bw.DoWork += bw_DoWorkVoid;
bw.RunWorkerCompleted += bw_RunWorkerVoidCompleted;
bw.RunWorkerAsync();
}
else
{
throw new Exception("Ooops, doSomethingIns should not be null !!!");
}
}
public void GetDataAsync()
{
if (getDataIns != null)
{
bw = new BackgroundWorker();
bw.DoWork += bw_DoWork;
bw.RunWorkerCompleted += bw_RunWorkerCompleted;
bw.RunWorkerAsync();
}
else
{
throw new Exception("Ooops, getDataIns should not be null !!!");
}
}
/*===========================================================================*/
void bw_DoWork(object sender, DoWorkEventArgs e)
{
if (getDataIns != null) e.Result = getDataIns();
}
void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (dataCompletedIns != null) dataCompletedIns((T)e.Result);
}
/*===========================================================================*/
void bw_DoWorkVoid(object sender, DoWorkEventArgs e)
{
if (doSomethingIns != null) doSomethingIns();
e.Result = true;
}
void bw_RunWorkerVoidCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (doNexthingIns != null) doNexthingIns();
}
}
/*======================================================================================================================================================*/
public static void GetDataAsync<T>(DataByThreadWorker<T>.GetDataDelegate GetDataFunc, DataByThreadWorker<T>.CallCompleted DataCompletedDelegate)
{
DataByThreadWorker<T> dt = new DataByThreadWorker<T>();
dt.getDataIns = GetDataFunc;
dt.dataCompletedIns = DataCompletedDelegate;
dt.GetDataAsync();
}
public static void DoAsync(DataByThreadWorker<object>.DoSomething doSomethingIns, DataByThreadWorker<object>.DoSomething doNextthingIns)
{
DataByThreadWorker<object> dt = new DataByThreadWorker<object>();
dt.doSomethingIns = doSomethingIns;
dt.doNexthingIns = doNextthingIns;
dt.DoAsync();
}
}