异步运行两个任务,等到他们最终的最快方法 [英] Fastest way to run two tasks asynchronously and wait until they end

查看:93
本文介绍了异步运行两个任务,等到他们最终的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我需要改变这个..

 无效美孚()
{
DoSomething的( 1,0);
DoSomething的(2,3);
}

要这样的事情...

 无效美孚()
{
//这个功能之一是同步..我NEAD运行它们异步莫名其妙
新主题( DoSomething的(1,0));
新线程(DoSomething的(2,3));

//现在我NEAD等待,直到两个异步功能将结束
WaitUntilBothFunctionsWillEnd();
}



有没有办法在Silverlight做到这一点?


解决方案

 无效美孚()
{
变种线程1 =新的Thread(()= > DoSomething的(1,0));
变种线程2 =新主题(()=> DoSomething的(2,3));

thread1.Start();
thread2.Start();

thread1.Join();
thread2.Join();
}



该方法的Thread.join()将阻止执行,所以在加入这两个线程将确保富()将返回这两个线程都结束之后。


Ok i need to change this..

    void foo()
    {
        DoSomething(1, 0);
        DoSomething(2, 3);
    }

to something like this...

    void foo()
    {
        //this functions is sync.. i nead to run them async somehow
        new Thread(DoSomething(1, 0));
        new Thread(DoSomething(2, 3));

        //Now i nead to wait untill both async functions will end
        WaitUntilBothFunctionsWillEnd();
    }

is there a way to do this in silverlight?

解决方案

void foo()
{
    var thread1 = new Thread(() => DoSomething(1, 0));
    var thread2 = new Thread(() => DoSomething(2, 3));

    thread1.Start();
    thread2.Start();

    thread1.Join();
    thread2.Join();
}

The method Thread.Join() will block execution until the thread terminates, so joining both threads will ensure that foo() will return only after both threads have terminated.

这篇关于异步运行两个任务,等到他们最终的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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