我正在使用多线程数据库查询一次检索多个数据表。我使用了一个被锁定的列表...一旦返回所有回调,我想返回列表。 我想知道如何等待所有回调完成。
private Object TableLock = new Object();
private List<DataTable> tables_from_callback = new List<DataTable>();
private void ExecReaderHandleCallback(IAsyncResult result)
{
SqlCommand command = (SqlCommand)result.AsyncState;
SqlDataReader reader = command.EndExecuteReader(result);
DataTable dt = new DataTable();
dt.Load(reader);
lock (TableLock)
{
tables_from_callback.Add(dt);
}
}
public List<DataTable> BegExecReader(String query, int query)
{
conn = new SqlConnection(connectionString);
try
{
conn.Open();
WaitHandle[] waitHandles = new WaitHandle[query];
IAsyncResult[] results = new IAsyncResult[query];
SqlCommand[] cmds = new SqlCommand[query];
for (int i = 0; i < query; i++ )
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = query;
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = queryTIMEOUT;
AsyncCallback callback = new AsyncCallback(ExecReaderHandleCallback);
cmd.BeginExecuteReader(callback, cmd);
cmds[i] = cmd;
}
/* ????
here I need to wait for the callbacks to finish
HOW?
??????? */
return tables_from_callback;
}
finally
{
conn.Close();
}
}
我已经做了类似的事情而没有使用回调
IAsyncResult result = cmd.BeginExecuteReader();
results[i] = result;
waitHandles[i] = result.AsyncWaitHandle;
...
WaitHandle.WaitAll(waitHandles);
但是现在我必须使用回调,所以我没有等待句柄。 任何帮助将不胜感激。
答案 0 :(得分:0)
这样做的一种简洁方法是使用两个上下文都可以访问的变量。根据上下文,我的意思是调用者传递回调和回调本身。
static void Main(string[] args)
{
int x = 1;
Task.Run(() =>
{
Thread.Sleep(5000);
Console.WriteLine("Yay!");
x = 0;
});
while (x != 0) { Thread.Yield(); }
Console.WriteLine("Done");
Console.ReadKey(true);
}
我正在使用Task.Run来模拟你的回调情况;但是,该原则仍适用于回调的情况......区别在于您将使用类级变量或(yikes)静态变量。只要信号量没有立即发出信号,就像信号量一样,你也可以像信号量一样......就像这样。
static void Main(string[] args)
{
Semaphore s = new Semaphore(1, 2);
Task.Run(() =>
{
s.WaitOne();
Thread.Sleep(5000);
Console.WriteLine("Yay!");
s.Release();
});
Thread.Sleep(100);
s.WaitOne();
Console.WriteLine("Done");
Console.ReadKey(true);
}