这个C#代码有更短的版本吗? [英] Is there a shorter version of this C# code?

查看:74
本文介绍了这个C#代码有更短的版本吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法缩短这段代码?每当我在线程中调用函数时,我必须这样做,为一个函数创建两个函数,也许你认为我是愚蠢的,但我真的需要一个快捷方式,我使用这个方法使我的代码看起来很愚蠢。它看起来像这样:



Is there a way to make this code shorter? Whenever I call function in thread I have to do this, making two function for one, maybe you think I'm dumb, but I really need a shortcut to this, I make my code looks stupid using this method. It looks like this:

public void StartServer(bool InThread = false)
        {
            if (InThread)
            {
                Thread t = new Thread(CallStartServer);
                t.Start();
            }
            else CallStartServer();
        }

        private void CallStartServer()
        {
            TcpListener listener = new TcpListener(IPAddress.Any, Port);
            while (true)
            {
                if (listener.Pending())
                {
                    TcpClient client = listener.AcceptTcpClient();
                    if (OnClientConnected != null) OnClientConnected(client);
                }
            }
        }





如果你分享一个较短的版本,我们将非常感激。



it will really be appreciated if you share a shorter version.

推荐答案

我认为这就是PIEBALDconsult所指的:

I think this is what PIEBALDconsult was referring to:
public void StartServer(bool InThread = false)
{
  if (InThread)
  {
    Thread t = new Thread(it => StartServer((bool)it));
    t.Start(false);
  }
  else
  {
    TcpListener listener = new TcpListener(IPAddress.Any, Port);
    while (true)
    {
      if (listener.Pending())
      {
        TcpClient client = listener.AcceptTcpClient();
        if (OnClientConnected != null)
          OnClientConnected(client);
      }
    }
  }
}



线程中的lambda 构造函数是必需的,因为该构造函数的 ParameterizedThreadStart 委托需要参数为 object



编辑:MTH

正如PIEBALDconsult在下面的评论中指出的那样。线程创建可以简化一点:


The lambda in the Thread constructor is required because the ParameterizedThreadStart delegate to that constructor needs the argument to be type object.

MTH
As PIEBALDconsult noted in the comments below. The thread creation can be simplified a bit:

if (InThread)
{
  Thread t = new Thread(() => StartServer(false));
  t.Start();
}
else



或者,如果你只是想启动线程,然后忘掉它:


Or, if you just want to start the thread and then forget about it:

if (InThread)
{
  new Thread(() => StartServer(false)).Start();
}
else



但您将无法使用之后对线程做 任何事情

(这就像拥有本地变量 t 在启动线程后立即超出范围!)


but you won't be able to do anything to the thread after that.
(That's pretty much like having the local variable t go out of scope right after starting the thread!)


为什么要缩短已经基于错误想法的东西?在通话中创建新线程的整个想法非常糟糕。有不同的,更好的变体,例如使用任务(委托实例)提供永久工作的线程;任务进入阻塞队列,所以当没有其他任务时,线程进入等待状态,这不会浪费任何CPU时间。



见,例如,我的文章用于线程通信和线程间调用的简单阻塞队列 [ ^ ](并注意替代方案, http://www.codeproject。 com / script / Articles / ListAlternatives.aspx?aid = 149540 [ ^ ]。



依旧...



-SA
Why making shorter something which is already based on wrong idea? The whole idea to create a new thread in a call is quite bad. There are different, much better variants, such as having a permanently working thread you feed with tasks (delegate instances); the tasks go in a blocking queue, so when there is no another task, a thread goes in a wait state, which does not waste any CPU time.

See, for example, my article Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^] (and pay attention for the alternative, http://www.codeproject.com/script/Articles/ListAlternatives.aspx?aid=149540[^]).

And so on…

—SA


这篇关于这个C#代码有更短的版本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆