2017-01-26 32 views
0

您好,我知道我们可以用C#这样的区别返回任务

Task t1 = new Task(new Action(A)) 

运行任务,但一个是静态函数
我们可以有非静态函数,当我们有函数返回任务 可我们已经运行像

new Task(new Action(A)); 

,如果我们不能什么是DEF之间

static void A() 
{ 
     Console.Write("task"); 
} 
    Task t1 = new Task(new Action(A)); 

public async Task<BitmapImage> Set_Image(string location_in_server, string location_to_save, string name_file) 
     { 
      Ntaban.Api.API_HttpClient apic = new Ntaban.Api.API_HttpClient(); 
     string location = location_to_save + name_file; 
     string location_directory = location_to_save; 
     if (Directory.Exists(location_directory) == false) 
     { 
      Directory.CreateDirectory(location_directory); 
     } 
     if (File.Exists(location) == false) 
     { 
      await apic.download_file_async(location_in_server + name_file, location, null); 
     } 
     BitmapImage s1 = new BitmapImage(); 
     s1.BeginInit(); 
     s1.UriSource = new System.Uri(location_to_save + name_file); 
     s1.EndInit(); 
     return s1; 
    } 
    imgProfile.Source = savefile.Set_Image(Ntaban.Api.API_server.Host + "/content/profile/", St_Major.Directories.Directory_Main, lst.First().picAdr).Result; 

回答

0

我知道我们可以用C#这样的

这并不实际运行任务运行任务。它创建了一个准备运行但尚未实际安排的任务。

哦,你不应该有史以来做到这一点。 有史以来没有任何理由使用Task构造函数。

什么是高清

的任务建构之间 产生不运行的任务。据推测,未显示的代码会安排线程池上的任务(例如,t1.Start(TaskScheduler.Default)),在这种情况下,它将在线程池线程上执行A

async方法返回的任务代表该方法的执行。请注意,async意味着一个异步任务 - 这是而不是在线程池线程上执行。我有一篇关于how async and await work的更详细的博客文章。

+0

我知道这个,但是当我们有函数返回任务(例如)它作为任务运行。 换句话说,我喜欢在任务或线程或CPU的其他核心运行我的功能我们该怎么办? –

+0

@amirjahany:如果你想在一个线程池上运行代码,那么使用'Task.Run'。 –

+0

感谢您的回答。你可以告诉我关于threadpool我不明白它 –