简体   繁体   English

在单独的线程中延迟执行ASP.NET C#

[英]Delay execution in a separate thread ASP.NET C#

In a ASP.NET C# web application I want to perform an asynchronous action after a form has been submitted. 在ASP.NET C#Web应用程序中,我想在提交表单后执行异步操作。 It's fire and forget. 这是火和忘记。 I don't need to wait for a response of any kind. 我不需要等待任何形式的回应。

I can do that as follows: 我可以这样做:

System.Threading.ThreadPool.QueueUserWorkItem(delegate { TheThingToDo(); });

Now, within my TheThingToDo() method I would like to introduce a delay of, say, 10 seconds before everything kicks off. 现在,在我的TheThingToDo()方法中,我想在一切开始之前引入延迟,比如10秒。 The simplest thing seem to be to use thread.sleep since I'm in a separate thread already and don't care about execution being blocked. 最简单的事情似乎是使用thread.sleep,因为我已经在一个单独的线程中,并不关心执行被阻止。

Is there any reason not to do this? 有什么理由不这样做吗? Or is there a more elegant solution? 还是有更优雅的解决方案? I've looked at timers but using them seems to introduce more code for not much benefit in this particular case? 我看过计时器但是使用它们似乎在这种特殊情况下引入了更多的代码并没有太大的好处?

Sleeping in the thread is fine. 睡在线程很好。

Since there is nothing else going on and you don't care about the results or synchronisation, best approach is to KISS . 由于没有其他任何事情发生,你不关心结果或同步,最好的方法是KISS

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

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