Window Service - 等待存储过程获取结果

use*_*185 3 c# windows-services sql-server-2005 sql-server-2008 c#-4.0

我正在从 Window Service 运行存储过程C#。存储过程是一个相当繁重的查询,并且需要相当长的时间。

我想等到存储过程完成并返回值。

有什么方法可以确定存储过程已完成吗?

Sco*_*ion 5

使用Threading.Tasks。以下代码将使您的线程处于等待状态,直到任务完成。

public void CallStoredProcMethod()
{
    var task1 = System.Threading.Tasks.Task.Factory.StartNew(() => RunStoredPro());

    // thread will wait there till the operation finish
    task1.Wait();
}

public void RunStoredPro()
{
    using (var connection = new SqlConnection(sqlConnString))
    {
        // your database call
    }
}
Run Code Online (Sandbox Code Playgroud)