简体   繁体   English

asp.net/c#中的主题

[英]thread in asp.net/c#

I want to create a thread on user_login Event or Form_load Event. 我想在user_login Event或Form_load Event上创建一个线程。 In this thread i want to call a class function to execute some sql statement, and the user do not have to wait for the result or background process to be finished, he is just directed to his desired page let say my profile or what ever. 在这个线程中我想调用一个类函数来执行一些sql语句,并且用户不必等待结果或后台进程完成,他只是被导向他想要的页面让我说出我的个人资料或者什么。 I am using ASP.NET, C#. 我正在使用ASP.NET,C#。

For what it is worth, this is a Bad Idea. 对于它的价值,这是一个坏主意。 There are a variety of ways that that IIS could terminate that background thread (IIS restart, app pool restart, etc., all of which are normal expected behavior of IIS), and the result would be that your DB transaction gets silently rolled back. IIS可以通过多种方式终止后台线程(IIS重新启动,应用程序池重新启动等等,所有这些都是IIS的正常预期行为),结果是您的数据库事务会以静默方式回滚。

If these queries need to be reliably executed, you should either execute them in the request or send them to a windows service or other long-lived process. 如果需要可靠地执行这些查询,则应在请求中执行它们或将它们发送到Windows服务或其他长期进程。 This doesn't mean that the user can't get feedback on the progress- the IIS request itself could be executed via an AJAX call. 这并不意味着用户无法获得有关进度的反馈 - IIS请求本身可以通过AJAX调用执行。

Here are some examples of asynchronous calls in C#. 以下是C#中异步调用的一些示例。

http://support.microsoft.com/kb/315582 http://support.microsoft.com/kb/315582

The choice of pattern depends on your needs. 模式的选择取决于您的需求。 Note that in sample 5 you can provide null as the callback if you don't want any code executed when the action is done. 请注意,在示例5中,如果您不希望在操作完成时执行任何代码,则可以提供null作为回调。

You could do this by calling your method in a thread using Thread.Start. 您可以通过使用Thread.Start在线程中调用您的方法来完成此操作。 IsBackground is false by default, which should prevent your application from stopping. 默认情况下,IsBackground为false,这会阻止您的应用程序停止。

http://msdn.microsoft.com/en-us/library/7a2f3ay4.aspx http://msdn.microsoft.com/en-us/library/7a2f3ay4.aspx

But, since most of your time will probably be spent in your database call. 但是,因为你的大部分时间都可能花在数据库调用上。 Why not just execute it asynchronously without a callback? 为什么不在没有回调的情况下异步执行它? On a SqlCommand that would be BeginExecuteNonQuery. 在一个SqlCommand上,它将是BeginExecuteNonQuery。

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

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