如何将线程封装在方法中?
目前我正在编写一个小型网络服务器,并为服务器收到的每个请求创建一个新线程。 基本上是这样的:
public class MyController
{
public void ProcessRequest(object contextObject)
{
HttpListenerContext context = (HttpListenerContext)contextObject;
// handle request
if (someCondition())
{
context.Response.StatusCode = 400;
context.Response.StatusDescription = "Missing something";
}
else
{
context.Response.StatusCode = 200;
context.Response.StatusDescription = "Everything OK";
}
}
public void AcceptRequest()
{
while (true)
{
HttpListenerContext context = HttpListener.GetContext();
Thread thread = new Thread(this.ProcessRequest);
thread.Start(context);
}
}
}
我试图让我的例子保持简单。显然在我的应用程序中它有点复杂。 现在我尝试将这里发生的情况封装在 if-else 指令中。我想到了这样的方法:
public void EncapsulateMe(int code, string description)
{
context.Response.StatusCode = code;
context.Response.StatusDescription = description;
}
问题是我也需要传输上下文对象,但我不确定如何做到线程安全以及最好的方法是什么。我考虑创建一个从 Thread 派生的新类并实现 ProcessRequest 方法和新的 EncapsulateMe 方法。这对于我想要完成的事情来说会太复杂吗?
编辑:我刚刚发现不可能在 C# 中编写一个派生自 Thread
的类,因为此类是密封的...有什么方法可以在 C# 中创建自己的线程吗? 我只是从 Java 中知道这一点,所以我有点困惑,这在 C# 中是不可能的......
Currently I'm writing a little webserver and I create a new Thread for every request the server gets.
Basically it's like this:
public class MyController
{
public void ProcessRequest(object contextObject)
{
HttpListenerContext context = (HttpListenerContext)contextObject;
// handle request
if (someCondition())
{
context.Response.StatusCode = 400;
context.Response.StatusDescription = "Missing something";
}
else
{
context.Response.StatusCode = 200;
context.Response.StatusDescription = "Everything OK";
}
}
public void AcceptRequest()
{
while (true)
{
HttpListenerContext context = HttpListener.GetContext();
Thread thread = new Thread(this.ProcessRequest);
thread.Start(context);
}
}
}
I tried to keep my example simple. Obviously in my application it's a bit more complicated.
Now I try to encapsulate what here happens in the if-else-directive. I thought about a method like:
public void EncapsulateMe(int code, string description)
{
context.Response.StatusCode = code;
context.Response.StatusDescription = description;
}
The problem is that I need to transfer the context object too, but I'm not sure how to do it thread-safe and what would be the best way. I thought about creating a new class that derives from Thread and implements the ProcessRequest-method and the new EncapsulateMe-method. Would that be to complicated for what I'm trying to accomplish?
Edit: I just found out that it's not possible to write a class in c# that derives from Thread
because this class is sealed... is there any way to create your own Threads in c#?
I just knew this from Java, so I'm a bit confused that it's not possible in c#...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我尝试用线程编写一个新类 ProcessRequestThread:
但我对这个解决方案并不太满意......这对我来说似乎有点过分了。有人有一个更小/更好的想法吗?
I tried to compose a new class ProcessRequestThread with a Thread:
But I'm not really satisfied with this solution... it seems somehow to much to me. Anyone got a smaller/better idea?