最简单的方法做一个火和忘记的方法在c#4.0 [英] Simplest way to do a fire and forget method in c# 4.0

查看:125
本文介绍了最简单的方法做一个火和忘记的方法在c#4.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很喜欢这个问题:



在C#中最简单的做火和忘记的方法?



我只想知道现在我们有并行扩展在C#4.0是有更好的更清洁的方式做火&

使用任务类是的,但是不要使用平行linq?

解决方案

PLINQ实际上是用于查询集合。



类似下面的任务将用于任务。

  Task.Factory.StartNew(()=> FireAway()); 

或甚至...

  Task.Factory.StartNew(FireAway); 

  new Task(FireAway).Start(); 

其中 FireAway

  public static void FireAway()
{
// Blah ...
}

因此,凭借类和方法名称terseness这会跳过线程池版本六个和十九个字符之间, :)

  ThreadPool.QueueUserWorkItem(o => FireAway 


I really like this question:

Simplest way to do a fire and forget method in C#?

I just want to know that now that we have Parallel extensions in C# 4.0 is there a better cleaner way to do Fire & Forget with Parallel linq?

解决方案

With the Task class yes, but PLINQ is really for querying over collections.

Something like the following will do it with Task.

Task.Factory.StartNew(() => FireAway());

Or even...

Task.Factory.StartNew(FireAway);

Or...

new Task(FireAway).Start();

Where FireAway is

public static void FireAway()
{
    // Blah...
}

So by virtue of class and method name terseness this beats the threadpool version by between six and nineteen characters depending on the one you choose :)

ThreadPool.QueueUserWorkItem(o => FireAway());

这篇关于最简单的方法做一个火和忘记的方法在c#4.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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