如何将线程封装在方法中?

发布于 2024-09-25 23:35:43 字数 1308 浏览 4 评论 0原文

目前我正在编写一个小型网络服务器,并为服务器收到的每个请求创建一个新线程。 基本上是这样的:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独守阴晴ぅ圆缺 2024-10-02 23:35:43

我尝试用线程编写一个新类 ProcessRequestThread:

public class ProcessRequestThread
{
  private Thread ProcessThread;
  private HttpListenerContext Context;

  public ProcessRequestThread()
  {
    ProcessThread = new Thread( ProcessRequest );
    ProcessThread.Start();
  }

  private void ProcessRequest(object contextObject)
  {
    Context = (HttpListenerContext)contextObject;

    // handle request
    if (someCondition())
    {
      EncapsulateMe(400, "Missing something");
    }
    else
    {
      EncapsulateMe(200, "Everything OK");
    }
  }

  private void EncapsulateMe(int code, string description)
  {
    Context.Response.StatusCode = code;
    Context.Response.StatusDescription = description;
  }
}

但我对这个解决方案并不太满意......这对我来说似乎有点过分了。有人有一个更小/更好的想法吗?

I tried to compose a new class ProcessRequestThread with a Thread:

public class ProcessRequestThread
{
  private Thread ProcessThread;
  private HttpListenerContext Context;

  public ProcessRequestThread()
  {
    ProcessThread = new Thread( ProcessRequest );
    ProcessThread.Start();
  }

  private void ProcessRequest(object contextObject)
  {
    Context = (HttpListenerContext)contextObject;

    // handle request
    if (someCondition())
    {
      EncapsulateMe(400, "Missing something");
    }
    else
    {
      EncapsulateMe(200, "Everything OK");
    }
  }

  private void EncapsulateMe(int code, string description)
  {
    Context.Response.StatusCode = code;
    Context.Response.StatusDescription = description;
  }
}

But I'm not really satisfied with this solution... it seems somehow to much to me. Anyone got a smaller/better idea?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文