C# - 代表们做ASYN任务还是做我需要的System.Threading? [英] C# - As Delegates doing Asyn task still do i need System.Threading?

查看:160
本文介绍了C# - 代表们做ASYN任务还是做我需要的System.Threading?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以利用执行代表异步操作,我怀疑有薄薄的机会摆在我application.Is那里使用的System.Threading任何必要的情况下,我无法避免的System.Threading?



(只是我在学习阶段)



例如:

 类节目

{

公共委托INT总(INT A,INT b);

静态无效的主要()

{

共计TL =新总(SumUp);
IAsyncResult的isany = tl.BeginInvoke(20,20,NULL,NULL);

,而(isany.IsCompleted!)
{
Console.WriteLine(忙于其他工作);
}

INT ANS = tl.EndInvoke(isany);

Console.WriteLine(结果= {0},ANS);

}

静态INT SumUp(INT A,INT B)

{
A = A * A;

B = B * B;

返回A + B;

}
}


解决方案

我们不断地越来越好抽象,并发处理,所以更低水平的结构往往是没有必要的。



您已经找到了这样一个例子更高层次的抽象,和.NET 4.0会给我们处理并发代码,如任务和延续的一些新的和非常强大的方式。



随着时间的推移,你应该能够处理在更高的抽象层次的并发性。无论这些抽象住的System.Threading与否是,在我看来,不那么重要了。


As i can perform asynchronous operations using delegates,i suspect there is a thin chance to use System.Threading in my application.Is there any essential situation where i can not avoid System.Threading ?

( Just I am in learning Phase).

Example :

class Program

{

public delegate int Total (int a,int b);

static void Main()

{

  Total tl = new Total(SumUp);
  IAsyncResult isany=tl.BeginInvoke(20,20, null, null);

    while (!isany.IsCompleted)
    {
        Console.WriteLine("Busy with other work");
    }

    int ans = tl.EndInvoke(isany);

    Console.WriteLine("Result ={0}", ans);

}

static int SumUp(int a,int b)

{
    a = a * a;

    b = b * b;

    return a + b;

}
}

解决方案

We are constantly getting better and better abstractions that deal with concurrency, so the more low-level constructs are often not necessary.

You already found one example of such a higher-level abstraction, and .NET 4.0 will give us some new and very powerful ways of dealing with concurrent code, such as Tasks and continuations.

As time goes by, you should be able to deal with concurrency at higher and higher levels of abstractions. Whether those abstractions live in System.Threading or not is, in my opinion, less important.

这篇关于C# - 代表们做ASYN任务还是做我需要的System.Threading?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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