如何从线程池中的C#程序运行Main()方法?(How to run Main() method from C# program in the thread pool?)

编程入门 行业动态 更新时间:2024-10-28 08:21:43
如何从线程池中的C#程序运行Main()方法?(How to run Main() method from C# program in the thread pool?)

我想知道,如何在线程池中运行C#程序的Main()方法,就像我在F#中一样:

let main() =
    Console.WriteLine("Hello World!")

do Task.Factory.StartNew(main).Wait()
 

C#编程语言中的替代代码是什么? 因为在简单的控制台应用程序中,只有使用方法:

static void Main() // or with int as the return type and args[] // in method arguments but it's not important for this question

我不能在某个地方添加像F#中的smth:

do Task.Factory.StartNew(main).Wait()
 

或者我只是不知道C#程序执行的所有可能性?

I wonder, how can I run the method Main() of C# program in the thread pool like I do it in F#:

let main() =
    Console.WriteLine("Hello World!")

do Task.Factory.StartNew(main).Wait()
 

What is the alternative code in C# programming language? Because in the simple console application, there is only the way to use:

static void Main() // or with int as the return type and args[] // in method arguments but it's not important for this question

And I can't somewhere also add smth like in F#:

do Task.Factory.StartNew(main).Wait()
 

Or I just don't know the all possibilites of C# program execution?

最满意答案

我可以建议你不要那样做。 我很确定你应该编写其他函数,然后可以根据需要添加到线程池中。

在大多数编程语言中,调用main函数是不好的方式; 如果您想要这样做,请编写其他功能来完成您的工作。 基本上:

static void Function(){ //Do stuff } static void Main(){ TaskFactory.StartNew(Function).wait(); }

Could I suggest that you refrain from that. I'm fairly certain that you should write other functions which you can then add to the thread pool as required.

In most programming languages, it's bad style to call the main function; write other functions to do your work if that's how you want to do it. Basically:

static void Function(){ //Do stuff } static void Main(){ TaskFactory.StartNew(Function).wait(); }

更多推荐

本文发布于:2023-07-20 11:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1198661.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   池中   程序   方法   program

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!