System.Threading.Tasks

我只是一个虾纸丫 提交于 2019-12-11 04:48:08

问题


i am trying to use that reference. i know it is in c# 4. i don't know what i currently have.

but basically, when i write the line:

Task.Factory.StartNew(someMethod);

it tells me that it is a mistake. is there any way to solve it so my ide will identify that library (Task Parallel Library)?


回答1:


You just need a using directive of

using System.Threading.Tasks;

You shouldn't need to add any extra references - Task is in mscorlib.

Are you sure you're targeting .NET 4? Check in your project properties. (The types available don't depend on the version of the language you're using, but the version of the framework you're using.)




回答2:


You can see what framework version you're targeting by right-clicking your project and selecting Properties. On the Application tab, your framework version will be in the "Target framework" dropdown. If there is no Application tab, check for a Silverlight tab.

If you are targeting .NET 4, then Task is already referenced; see Jon's answer.

If you are targeting .NET 3.5, then you can get Task by downloading Microsoft Reactive Extensions and adding those libraries as references.

If you are targeting .NET 2 or Silverlight, then you're out of luck; Task is not available on those systems.




回答3:


You probably have a reference to an earlier version of mscorlib. Make sure you have the 4.0 version. If you have an earlier one referenced (like 2.x), delete the reference and add the one for 4.0.




回答4:


Are you sure your code looks something like this

var task2 = Task.Factory.StartNew(() => pr.DoSomething());

Please share your code if the issue still persists.



来源:https://stackoverflow.com/questions/5050004/system-threading-tasks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!