简体   繁体   English

如何在执行sql命令时保持线程空闲

[英]How to keep the thread free while executing an sql command

So I have loooooooong stored procedure. 所以我有ooooooooong存储过程。 It takes about 5-8 mins to execute. 执行大约需要5-8分钟。 Say I create a an sqlCommand object and execute it via my my asp.net application, The thread will wait until the command execution is complete or command timeout duration. 假设我创建了一个sqlCommand对象,并通过我的asp.net应用程序执行该对象,该线程将等待命令执行完成或命令超时持续时间。

Event If I another thread (thread_2) to accomplish this task, thread_2 will wait. 事件如果我另一个线程(thread_2)完成此任务,线程_2将等待。 I want to avoid waiting because command is going to take about 4-7 mins to execute. 我想避免等待,因为命令将需要大约4-7分钟来执行。

Is there any way to do that ? 有什么办法吗?

EDIT : 编辑:

correct me If I am wrong, but I believe using the asynchronous version will also use thread_2. 纠正我,如果我错了,但是我相信使用异步版本也将使用thread_2。 Asynchronous methods are just a wrapper to reduce threading code. 异步方法只是减少线程代码的包装。 [my belief] [我的信念]

I wanted some event driven mechanism, so that no thread is in use until the command execution is complete. 我想要一些事件驱动的机制,以便在命令执行完成之前不使用线程。

But now It feels that I am asking for too much... Am I ? 但是现在感觉到我要的太多了...是吗?

To make SQL calls asynchronous there are Async versions of all commands - ie SqlCommand.ExecuteNonQueryAsync 为了使SQL调用异步,所有命令都有异步版本-即SqlCommand.ExecuteNonQueryAsync

The second part is to make page rendering asynchronous to not use thread while execution of the SQL command is in progress. 第二部分是使页面呈现异步,以便在执行SQL命令时不使用线程。 Both MVC and WebForms allow you to do that (MVC is easier). MVC和WebForms均允许您执行此操作(MVC更容易)。 Note that if execution is as long as you said you may want to implement some sort of poling mechanism on the page instead of waiting for request competition to render results... 请注意,如果执行时间与您说的一样长,则可能要在页面上实现某种轮询机制,而不是等待请求竞争来呈现结果...

With some code you can wrap it in async -friendly code ( Using async / await ) to use in async MVC actions . 使用一些代码,您可以将其包装在async友好的代码中( 使用async / await ),以用于异步MVC操作中

If you have to stick with WebForms/plain ASP.Net you will need to do more work to implement asynchronous pages, but it is still possible. 如果您必须坚持使用WebForms / plain ASP.Net,则需要做更多的工作来实现异步页面,但是仍然可以实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM