SQL CLR等待未执行 [英] SQL CLR awaitable not getting executed

查看:206
本文介绍了SQL CLR等待未执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,当等待代码从等待返回时,等待代码将完成执行代码的其余部分.我试图使它在sql clr中工作,但由于等待的进程和其下的代码均未执行,因此该命令无法正常工作. 在debug方法中,控件只是在执行await行之后返回.

It is my understanding that the awaitable completes the remaining part of the executing code when they return back from the wait. I am trying to get this to work in an sql clr, and this is not working as both awaited process and codes beneath it are not getting executed. In debug method, the control just returns after executing the await line.

我该如何进行这项工作,因为要求在不阻塞主线程的情况下执行clr,以便其他工作可以在db中顺利进行. 我真的需要它来工作,并且已经使用了2天了.

how can i make this work, as the requirement is for clr to be executed without blocking the main thread so that other work can continue well in the db. I really need this to work, and have been on it for 2 days now.

NB 如果方法是同步的,这将很好地工作. 并且存储的proc汇编在sql server中注册为不安全. 我正在使用mssql 2012和visual studio 2015.

NB This works well if method is sync. And the stored proc assemble is registered as unsafe in sql server. I am using mssql 2012 and visual studio 2015.

public partial class StoredProcedures
{
   [Microsoft.SqlServer.Server.SqlProcedure]
   public static async void sp_push_stock_update(SqlString transactionPrimaryKey, SqlString transactionType)
   {

       using (SqlConnection conn = new SqlConnection("context connection=true"))
       {

           StockUpdateClient.stock_fetcher _stockFetcher = new StockUpdateClient.stock_fetcher(conn);

           List<String> skuList = await new 
           System.Threading.Tasks.Task<List<string>>(()=> 
           _stockFetcher.getItems(transactionPrimaryKey, transactionType));

           //Code does not get here
           performLogging(skuList):


        }

    }
}

推荐答案

要求在不阻塞主线程的情况下执行clr

the requirement is for clr to be executed without blocking the main thread

您是否尝试过不使用线程并经历过它确实阻塞了线程?这可能会使您过于复杂.仍然有一些可用的选项:

Have you tried without the threading and experienced that it did indeed block the thread? It is possible that you are over-complicating this. Still, some options available are:

  1. 按照此通过创建处理长时间运行的调用的新Thread来执行相当于老式"方式的多线程,并等待其在调用thread.sleep(x);的循环中完成:

    Do multi-threading in what amounts to the "old-fashioned" way by creating a new Thread that handles the long-running call, and wait for it to complete in a loop that calls thread.sleep(x);:

    Thread _RunYouLongTime = new Thread(delegate);
    _RunYouLongTime.Start();
    while (_RunYouLongTime.IsAlive)
    {
      System.Threading.Thread.Sleep(100);
    }
    

  2. 如果长时间运行的调用是HttpRequest/Web Service,则增加

  3. If the long-running call is a HttpRequest / Web Service, increase the ServicePointManager.DefaultConnectionLimit Property. This should probably be done regardless of how, or even if, you do threading!

    这篇关于SQL CLR等待未执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆