简体   繁体   English

ASP.NET Web窗体中的后台进程

[英]Background processes in ASP.NET Web Forms

Let's say I want to make an application that can receive connections from Socket (not page requests) clients on one socket, and simultaneously serve web forms on another socket, telling the user, for example, how many clients have connected using the other (Socket) interface. 假设我要创建一个应用程序,该应用程序可以在一个套接字上接收来自Socket客户端(而非页面请求)的连接,并同时在另一个套接字上提供Web表单,例如,告诉用户有多少个客户端使用另一个套接字连接)界面。 This is only loosely similar to what I'm trying to do, so don't worry if it seems nonsensical! 这只是大致上与我要执行的操作类似,所以如果它看起来很荒谬,请不要担心!

So, is it possible to have the server in an ASP.NET Web Forms application run another thread dealing with other things, while the web server serves the pages? 因此,当Web服务器为页面提供服务时,是否可以让ASP.NET Web窗体应用程序中的服务器运行另一个处理其他内容的线程? Is there a recommended way of doing this, or does it need to be a bit of a hack? 有推荐的方法可以做到这一点,还是需要一点技巧?

Edit: Don't worry, I switched to MVC 4, and am now using the App_Start method of starting the thread. 编辑:不用担心,我切换到MVC 4,现在正在使用启动线程的App_Start方法。 As it doesn't matter if the thread is aborted, as long as it restarts, this is a suitable fix. 由于线程是否中止并不重要,因此只要重新启动线程即可,这是一个合适的解决方案。

Thanks for all the help anyway though, hnefatl 无论如何,感谢您的所有帮助,hnefatl

You could try using the async feature in .NET 4.5, mentioned here . 您可以尝试在.NET 4.5中使用异步功能(在此处提到)。 Basically, have a method with the "async" keyword defined, such as: 基本上,有一个定义了“ async”关键字的方法,例如:

public async Task DoThis();

Register the async task: 注册异步任务:

RegisterAsyncTask(new PageAsyncTask(DoThis));

Mark your page header as Async="true" 将页面标题标记为Async =“ true”

<%@ Page Title="Async" Language="C#" CodeBehind="Page.aspx.cs" 
       Inherits="Whatever" Async="true" %>

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

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