简体   繁体   English

如何删除单独的工作线程 function 并创建一个线程

[英]How can I remove seperate worker thread function and create a thread

This is the traditional thread creation code:这是传统的线程创建代码:

        public static void Ping()
        {
            new Thread(workThreadPingRequest) { IsBackground = true }.Start();
        }

        private static void workThreadPingRequest()
        {
           line1(); //Create connection
           line2(); //Ask ping
           line3(); //Process the reply and close connection
        }

I've got many pairs like them.我有很多像他们这样的鞋。 So how can I remove seperate worker thread function to make the code easier -to me- like below:那么如何删除单独的工作线程 function 以使代码更容易 - 对我来说 - 如下所示:

        public static void Ping()
        {
            new Thread(new Func<void> fn = () => 
                { line1(); line2(); line3();}) 
                { IsBackground = true }
                .Start();
        }

Or is it possible?或者有可能吗?

Have you tried using .NET 4's Tasks ?您是否尝试过使用 .NET 4 的 任务

Task.Factory.StartNew(() => {
    line1();
    line2();
    line3();
});

@Cameron's answer seems like an excellent idea but if are on .net 3.5 you could use the threadpool instead: @Cameron 的回答似乎是个好主意,但如果在 .net 3.5 上,您可以改用线程池:

System.Threading.ThreadPool.QueueUserWorkItem(_ =>
    {
        line1();
        line2();
        line3();
    });

Also if you really want create a new thread instead you can do this:此外,如果你真的想创建一个新线程,你可以这样做:

new Thread(() => 
{
    line1();
    line2();
    line3();
}) { IsBackground = true }.Start();

Sounds like you are just interested in syntax enhancement?听起来您只是对语法增强感兴趣? BackgroundWorker is pretty convienent. BackgroundWorker 非常方便。

string arg = "blah...";
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += (s,e) =>
{
// can extract args from e
// code here
};
worker.RunAsync(args);

this code is off the top of my head so it might not be perfect, but the idea is there.这段代码不在我的脑海中,所以它可能并不完美,但这个想法就在那里。

暂无
暂无

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

相关问题 我如何在工作线程上创建和使用WebBrowser控件? - How might I create and use a WebBrowser control on a worker thread? 如何在不挂起控制台应用程序的情况下从工作线程调用Windows PostMessage函数? - How can I call the windows PostMessage function from a worker thread without hanging the console application? 什么是工作线程及其与我创建的线程的区别? - What is a worker thread and its difference from a thread which I create? 如何使用后台工作程序将参数(字符串)发送到UI线程? - How can I send arguments(string) to the UI thread with the Background Worker? 如何防止或恢复工作线程上的堆栈溢出? - How can I prevent or recover from a stack overflow on a worker thread? 如何将此C#工作线程代码与主线程中的共享数据变量分离? - How can I decouple this C# worker thread code from the main thread for shared data variables? 如何将后台工作线程设置为单线程单元? - How can I make a background worker thread set to Single Thread Apartment? 如何获取工作线程以在主UI线程上执行回调? - How can I get a worker thread to execute a callback on the main UI thread? 如何在单独的线程上运行后台服务? - How to run backgroundservices on seperate thread? 如何从单独的线程(而不是在创建该线程的线程)中调用事件处理程序? - How Can I Get an Event Handler to be Called From a Seperate Thread than the One it Was Created On?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM