简体   繁体   English

如何从线程池中的C#程序运行Main()方法?

[英]How to run Main() method from C# program in the thread pool?

I wonder, how can I run the method Main() of C# program in the thread pool like I do it in F#: 我想知道,如何在线程池中运行C#程序的Main()方法,就像我在F#中一样:

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

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

What is the alternative code in C# programming language? C#编程语言中的替代代码是什么? 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#: 我不能在某处添加像F#中的smth:

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

Or I just don't know the all possibilites of C# program execution? 或者我只是不知道C#程序执行的所有可能性?

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; 在大多数编程语言中,调用main函数是不好的方式; 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();
}

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

相关问题 从C#主线程上的辅助线程运行方法? - Run Method from a secondary thread on the main thread in C#? 运行一个使用主线程中的方法的单独线程是否仍然在主线程上运行它? (C#) - Will running a separate thread that uses a method from the main thread still run it on the main thread? (C#) C#如何从主程序运行链接列表的delete方法? - C# How do I run a Linked List's delete method from the main program? C#:如何通过从另一个线程以某种方式发出信号来强制从主线程“调用”一个方法 - C#: How to force "calling" a method from the main thread by signaling in some way from another thread 如何从Nancyfx开头的工作线程中在主线程中运行? C# - How to run in main thread from worker thread that start by Nancyfx? C# 如何从C#系统托盘应用程序中将C程序作为线程运行? - How do I run a C program as a thread from a C# System Tray Application? C#,当主线程空闲时如何在主线程中运行函数 - C#, how to run a function in the main thread when the main thread becomes idle 如何在c#中使用线程池和互斥锁? - How to use Thread Pool and Mutex in c#? 如何判断一个线程是否是C#中的主线程 - How to tell if a thread is the main thread in C# 带锁的 C# 线程池 - C# Thread Pool with Locking
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM