Tag: 延迟执行

何时使用LINQ的.ToList()或.ToArray()

运行此代码后: var input = new List( … ); var result = input.Select( t => new U(t) ); U first1 = null; foreach ( U u1 in result ) if ( first1 == null ) first1 = u1; U first2 = null; foreach ( U u2 in result ) if ( first2 == null ) first2 […]

是否有可能确定IEnumerable 是否已将执行挂起?

我有一个接受Enumerable的函数。 我需要确保对枚举数进行评估,但是我不想创建它的副本(例如,通过ToList()或ToArray())如果它已在List或其他“冻结”集合中准备就绪。 冻结我指的是已经建立了一组项目的集合,例如List,Array,FsharpSet,Collection等,而不是像Select()和where()这样的linq。 是否有可能创建一个函数“ForceEvaluation”,可以确定枚举是否已将执行挂起,然后评估可枚举? public void Process(IEnumerable foos) { IEnumerable evalutedFoos = ForceEvaluation(foos) EnterLockedMode(); // all the deferred processing needs to have been done before this line. foreach (Foo foo in foos) { Bar(foo); } } public IEnumerable ForceEvaluation(IEnumerable foos) { if(??????) { return foos} else {return foos.ToList()} } } 经过一些研究后,我意识到这在任何实际意义上都是不可能的,并且需要对每个迭代器进行复杂的代码检查。 因此,我将使用Mark的答案的变体并创建已知安全类型的白名单,并且只需调用ToList()不在白名单上的任何内容。 感谢大家的帮助。 编辑*经过更多的反思,我意识到这相当于停止问题。 所以非常不可能。

NServiceBus延迟消息处理

我有一个NServiceBus应用程序,由于某些外部事件没有发生,可能无法处理给定的消息。 因为这个其他事件不是NSB事件,所以我无法正确实施传奇。 但是,不是仅仅重新排队消息(这会导致循环直到发生外部事件),而是将消息包装在另一个消息(DelayMessage)中并对其进行排队。 DelayMessage由不同的服务获取并放置在数据库中,直到重试间隔到期。 此时,延迟服务在原始队列上重新排队消息,以便进行另一次尝试。 但是,如果外部事件仍然没有发生,这种情况可能不止一次发生,而且即使从未发生这种情况,我也希望限制消息的往返次数。 这意味着DelayMessage具有MaxRetries属性,但是当延迟服务将原始消息排队以进行重试时,该属性会丢失。 我还缺少哪些其他选择? 我很高兴接受这个问题有一个完全不同的解决方案。

c#可靠的延迟/预定执行最佳实践

我正在进行的项目要求在某个时间执行一些处决。 我不确定处理这种情况的最佳方法是什么。 该方法必须能够在服务器重启/维护后继续存在。 方法调用必须以编程方式进行。 我正在考虑走这条道路: 我可以在数据库(甚至是消息队列)中有一个名为TaskTable的表,它可以具有TaskID(PK),TaskName(varchar),TaskStatus(枚举成功,失败,已调度)和TimeOfExecution。 但我需要一个Windows服务,定期轮询数据库以查找任何未执行的任务。 我面临的问题是:我将什么用作保存到数据库中的TaskName? class级名称? 类和方法名称ToString? 我怎样才能将字符串转换回来并以编程方式调用方法调用(我不希望有一个巨大的switch语句)? 一项持久性的任务如下所示。 所以我需要能够获得任务“SendIncompleteNotification”的名称,并将类名保存到数据库中并以回溯方式调用 public static Task SendIncompleteNotification { get { return new Task ( a => Console.WriteLine(“Sample Task”) , “This is a sample task which does nothing.” ); } } 现在的问题是我在编译方法/属性名称时遇到问题。 var type = ApplicationTask.SendIncompleteNotification.GetType(); //type.Name shows “Task`1” rather than SendIncompleteNotification 有没有更好的方法来处理这种情况? 谢谢! 更新:对不起,我的头在旋转。 我现在意识到我做错了是有另一个方法/属性来返回我的任务。 […]

Linq,XmlNodes,foreach和例外

请考虑以下代码: using System; using System.Collections.Generic; using System.Linq; using System.Xml; using System.Xml.Linq; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(@” “); XmlNode root = xmlDoc.DocumentElement; List disabledNodes = new List(); try { foreach (XmlNode node in root.ChildNodes.Cast() .Where(child => child.Attributes[“disabled”] != null && Convert.ToBoolean(child.Attributes[“disabled”].Value))) { Console.WriteLine(“Removing:”); Console.WriteLine(XDocument.Parse(node.OuterXml).ToString()); […]

C#中的延迟执行

我怎样才能在C#中实现自己的延迟执行机制? 所以我举例说: string x = DoFoo(); 是否有可能执行一些魔法,以便在我“使用”x之前DoFoo不会执行?

在“using”语句中使用yield时,Dispose何时发生?

我有一个关于延迟执行和处理数据的问题。 请考虑以下示例: private IEnumerable ParseFile(string fileName) { using(StreamReader sr = new StreamReader(fileName)) { string line; while((line = sr.ReadLine()) != null) { yield return line; } } } private void LineReader(string fileName) { int counter = 0; foreach(string line in ParseFile(fileName)) { if(counter == 2) { break; // will this cause a dispose on the StreamReader? […]

如何转换此迭代器阻止function更改?

给出以下代码片段: public class Foo { public IEnumerable Sequence { get; set; } public IEnumerable Bar() { foreach (string s in Sequence) yield return s; } } 以下代码片段在语义上是等价的,还是不同? 如果它不同,它们如何以不同的方式运作? public class Foo2 { public IEnumerable Sequence { get; set; } public IEnumerable Bar2() { return Sequence; } } 这个问题的灵感来自于这个问题 ,它提出了一个类似情况的不同问题。

yield语句中的yield返回

如果我在一个锁定语句中有一个收益率回报,锁定会在每个收益率上取消(在下面的例子中为5次),或者只对列表中的所有项目取消一次? 谢谢 private List _data = new List(){“1″,”2″,”3″,”4″,”5”}; private object _locker =new object(); public IEnumerable GetData() { lock (_locker) { foreach (string s in _data) { yield return s; } } }