我有一个小型应用程序需要测试多个连接的 SQL 连接字符串(每个连接一次完成一个)。为此,我临时设置了 ConnectionTimeout = 5
,以避免在连接无效时等待很长时间,比如 ConnectionTimeout = 0
(永远等待)。
为了避免在我们尝试 Open()
时 UI 挂起一个错误的连接(即使 ConnectionTimeout = 5
等待 SqlException
最长可达 20 秒),我想使用任务并行库 (TPL) 在单独的线程上运行测试。所以我分拆了我的新线程:
Task<bool> asyncTestConn = Task.Factory.StartNew<bool>
(() => TestConnection(conn, bShowErrMsg));
return asyncTestConn.Result;
问题是这仍然锁定了 UI(很明显),因为它在返回给调用者之前等待结果。如何让代码在从异步 Task
获取最终结果的同时将控制权返回给 UI(释放 GUI)?
此外,在 Task
中,我可以合法地执行 MessageBox.Show("Some message")
吗?这不适用于 BackgroundWorkers
并且这个池化线程默认是后台线程;但这似乎不是问题。感谢您的宝贵时间。
最佳答案
对于 TPL,ContinueWith
正是您想要的。扩展 Henk 的回答:
var asyncTestConn = Task.Factory.StartNew(() => TestConnection(conn, bShowErrMsg));
// Henk's "MyFinishCode" takes a parameter representing the completed
// or faulted connection-testing task.
// Anything that depended on your "return asyncTestConn.Result;" statement
// needs to move into the callback method.
asyncTestConn.ContinueWith(task =>
{
switch (task.Status)
{
// Handle any exceptions to prevent UnobservedTaskException.
case TaskStatus.Faulted: /* Error-handling logic */ break;
case TaskStatus.RanToCompletion: /* Use task.Result here */ break;
}
},
// Using this TaskScheduler schedules the callback to run on the UI thread.
TaskScheduler.FromCurrentSynchronizationContext());
关于c# - TPL 如何执行 'Call-Back',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9617440/