草庐IT

task-switching

全部标签

c# - 在 switch case 中,如果我们将 "default"写为任何单词或单个字母,它不会抛出错误

在switch中,如果我们写任何单词或单个字母而不是default,它不会抛出错误。例如switch(10){case1:break;hello:break;}它运行时没有抛出错误。谁能解释一下这是如何工作的? 最佳答案 它正在编译,因为hello:是一个标签,因此可以作为goto的目的地。当我编译这个时,我收到了关于未引用标签的警告(因为我没有转到)这是您可以放入LINQPad的示例-您会注意到它同时打印“1”和“hello”:switch(1){case1:"1".Dump();gotohello;break;hello:"he

c# - C# 中的 Switch 语句

这个问题在这里已经有了答案:Multiplecasesinswitchstatement(24个答案)Switchcase:canIusearangeinsteadofaonenumber[duplicate](16个答案)关闭1年前。有谁知道是否可以在switch语句中包含范围(如果可以,如何)?例如:switch(x){case1://dosomethingbreak;case2..8://dosomethingelsebreak;default:break;}编译器似乎不喜欢这种语法——它也不喜欢:case

c# - 在键盘事件中使用 CancellationToken 调用 Task.Delay 时出现 TaskCanceledException

我正在尝试延迟从WinRT中的键盘事件调用的方法(示例中的SubmitQuery())的处理,直到一段时间内(在本例中为500毫秒)没有进一步的事件。我只希望在我认为用户已完成输入时运行SubmitQuery()。使用下面的代码,当Task.Delay(500,cancellationToken.Token)时,我不断收到System.Threading.Tasks.TaskCanceledException;叫做。请问我做错了什么?CancellationTokenSourcecancellationToken=newCancellationTokenSource();private

c# : Why is a cast needed from an Enum to an INT when used in a switch statement? ,枚举是整数

谁能告诉我为什么我需要从我的枚举中转换为Intswitch(Convert.ToInt32(uxView.SelectedValue)){case(int)ViewBy.Client:如果我删除强制转换(int),它会失败并提示我必须使用强制转换。这是我的枚举,枚举是整数....有人知道吗?publicenumViewBy{Client,Customer} 最佳答案 在C#中,enum不仅仅是数字。相反,它们是与类型相关联的数字或在上下文中具有名称的数字。要避免在case语句中进行强制转换,您可以在switch中进行强制转换:swi

c# - 编译器对 switch 语句的看法是什么?

再次受到-5问题的启发!IsemptycaseofswitchinC#combinedwiththenextnon-emptyone?我阅读了[thiscomment]的@Quartermeister并感到惊讶!那么为什么这个可以编译switch(1){case2:}但这不是。inti;switch(i=1){case2://Controlcannotfallthroughfromonecaselabel('case2:')toanother}这都不是switch(2){case2://Controlcannotfallthroughfromonecaselabel('case2:')

c# - 异步等待 Task.Run 与 HttpClient.GetAsync

我是c#5异步功能的新手。我试图了解这两种实现之间的区别:实现1:privatevoidStart(){foreach(varurlinurls){ParseHtml(url);}}privateasyncvoidParseHtml(stringurl){varquery=BuildQuery(url);//BuildQueryissomehelpermethodvarhtml=awaitDownloadHtml(query);//...MyTypeparsedItem=ParseHtml(html);SaveTypeToDB(parsedItem);}privateasyncTask

c# - 从以 Task 为返回类型的非异步方法返回什么?

假设我有一个不是异步但返回一个Task的方法(因为定义来自一个也用于异步实现的接口(interface))publicTaskDoWorkAsync(Guidid){//dotheworkreturn...;}返回的最佳对象是什么?我目前的选择:returnTask.Yield();returnTask.FromResult(null);//anyoftheotherbutcachedinastaticfieldandreused. 最佳答案 在Microsoft.net4.6中,Task类具有用于此目的的静态属性。任务.Compl

c# - .NET 4 相当于 Task.WhenAll()

在.NET4中,是否有与.NET4.5的System.Threading.Tasks.Task.WhenAll()等效的功能??目标是将多个异步任务打包成一个任务,当所有组成任务都完成时,该任务就完成了。 最佳答案 在.NETFramework4.0中,WhenAll和WhenAny可以与已安装的AsyncCTP一起使用如果是VisualStudio2010或AsyncTargetingPack对于VisualStudio2012。然后在TaskEx类型上提供WhenAll和WhenAny方法。

c# - "long-running tasks"是什么意思?

Bydefault,theCLRrunstasksonpooledthreads,whichisidealforshort-runningcompute-boundwork.Forlonger-runningandblockingoperations,youcanpreventuseofapooledthreadasfollows:Tasktask=Task.Factory.StartNew(()=>...,TaskCreationOptions.LongRunning);我正在阅读有关thread和task的主题。你能给我解释一下什么是“长时间运行”和“短期运行”任务吗?

c# - 我如何在类型安全的枚举模式上使用 switch 语句

我以不同的方式找到了一个关于实现枚举的漂亮示例。我认为这就是所谓的类型安全枚举模式。我开始使用它,但我意识到我不能在switch语句中使用它。我的实现如下所示:publicsealedclassMyState{privatereadonlystringm_Name;privatereadonlyintm_Value;publicstaticreadonlyMyStatePASSED=newMyState(1,"OK");publicstaticreadonlyMyStateFAILED=newMyState(2,"ERROR");privateMyState(intvalue,stri