草庐IT

thread_func

全部标签

c# - 在 Select 和 Where 调用中重用 Linq to Entities 的 Expression<Func<T, TResult>

假设我有一个实体对象定义为publicpartialclassArticle{publicId{get;set;}publicText{get;set;}publicUserId{get;set;}}根据文章的某些属性,我需要确定给定用户是否可以删除该文章。所以我添加了一个静态方法来进行检查。像这样的东西:publicpartialclassArticle{publicstaticExpression>CanBeDeletedBy(intuserId){//Addlogictobereusedherereturna=>a.UserId==userId;}}现在我可以做using(MyE

c# - 预定义类型 'System.Threading.Tasks.Task' 在全局别名的多个程序集中定义

我刚刚建立了一个新的asp.netmvc4项目,结合使用最新的.net框架(4.5)和VisualStudio2012。在我添加了一些类、辅助项目等之后。我突然注意到我编译项目时收到5次相同的警告。警告指出:ASPNETCOMPILER:warningCS1685:Thepredefinedtype'System.Threading.Tasks.Task'isdefinedinmultipleassembliesintheglobalalias;usingdefinitionfrom'c:\ProgramFiles(x86)\ReferenceAssemblies\Microsoft\

c# - 在没有这些参数的情况下将具有默认值的方法分配给 Func<>?

我希望能够做到以下几点:FunctryMethodFunc=TryMethod;TryMethod的签名如下:boolTryMethod(intvalue,intvalue2=0,doublevalue3=100.0)我不反对将方法分解为柯里化(Currying)格式,但如果有一种方法可以做到这一点,那么这样做会更有效。 最佳答案 可选参数是一种语言特性,编译器负责将对带有可选参数的方法的调用转换为带有值的完整调用。看看下面这段简单的代码,publicvoidGeneralMethod(){TestMethod(6);}public

c# - 为什么在通用序列创建器上使用 Func<> 比使用 new() 约束快得多

考虑以下代码...在我对Windows7x64PC(Inteli73GHz)上的RELEASE(不是调试!)x86构建的测试中,我获得了以下结果:CreateSequence()withnew()took00:00:00.9158071CreateSequence()withcreator()took00:00:00.1383482CreateSequence()withnew()took00:00:00.9198317CreateSequence()withcreator()took00:00:00.1372920CreateSequence()withnew()took00:00:

c# - 将 Func<T, String> 转换为 Func<T, bool>

我想我的思绪正在爆炸,试图弄清楚Funcs...如果这没有意义,我道歉,现在对我来说很有意义,但已经度过了漫长的一天......1)假设给你一个函数,它接受T并输出一个字符串:Func您能否将其转换为接受T并根据某种逻辑返回bool值的函数(在这种情况下,如果返回的字符串为空(String.IsNullOrWhiteSpace)?Func2)如果给你一个,你能做同样的事情吗Expression>并需要将其转换为Func根据返回的字符串是否为空(String.IsNullOrWhiteSpace)返回true/false?谢谢 最佳答案

c# - 为任何方法创建 Func 或 Action(在 C# 中使用反射)

我的应用程序根据设置动态加载dll来自数据库(文件、类和方法名称)。为了促进、加快和减少反射的使用,我想要一个缓存....遵循使用的想法:MethodInfo.Invoke没有任何表现形式(ReflectionPerformance-CreateDelegate(PropertiesC#))我想翻译对方法的任何调用。我想到了一些可以像这样工作的东西:publicstaticTCreate(Typetype,stringmethodName)//orpublicstaticTCreate(MethodInfoinfo)//touselikethis:varaction=Create>(t

c# - 无法将类型 'bool' 隐式转换为 'system.threading.tasks.task bool'

我在服务实现代码中遇到此错误:“无法将类型‘bool’隐式转换为‘system.threading.tasks.taskbool’”。你能更正我的代码吗?publicTasklogin(stringusn,stringpwd){DataClasses1DataContextauth=newDataClasses1DataContext();varmessage=frompinauth.Userswherep.usrName==usn&&p.usrPass==pwdselectp;if(message.Count()>0){returntrue;}else{returnfalse;}}

c# - 如何在 C#(托管代码)中获取 *THREAD* 的 CPU 使用率和/或 RAM 使用率?

我知道如何获取进程的CPU使用率和内存使用率,但我想知道如何在每个线程级别上获取它。如果最好的解决方案是进行一些P调用,那也很好。我需要的例子:ThreadmyThread=Thread.CurrentThread;//sometimelaterinsomeotherfunction...Console.WriteLine(GetThreadSpecificCpuUsage(myThread)); 最佳答案 如前所述,无法回答内存使用情况,因为这是整个进程的一个属性,但CPU使用情况:Processp=Process.GetCurr

C#:Func<> 而不是方法?

这个问题在这里已经有了答案:FuncDelegatevsFunction(6个答案)关闭9年前。这是给所有知情人士的好奇问题:使用Func而不是方法有任何危害/缺点吗?简单示例:privatestaticFunc>Foo=(i1,i2,dbc)=>(i1!=0)?dbc.Bar(i2):newList{/*somedefaultvalues...*/};对比privatestaticListFoo(inti1,inti2,DBContextdbc){returni1!=0?dbc.Bar(i2):newList{/*somedefaultvalues...*/};}

c# - 使 System.Threading.Timer 无效会停止吗?

如果我有一个活跃的System.Threading.Timer我将它设置为null,它停止了吗?我意识到调用.Dispose()更合适,但我想要问题的答案。publicclassFoo{privateSystem.Threading.Timer_timer;publicFoo(){//initializetimer}publicvoidKillTimer(){_timer=null;}}更新:经过反复讨论是否将对System.Threading.Timer的单个引用设置为null确实会导致停止它表明没有挥之不去的引用,例如事件列表,因为线程计时器接受单一回调并且不公开事件。如果GC收集