deferred-execution

yield returns within lock statement

一世执手 提交于 2019-12-17 19:37:19
问题 if i have a yield return in a lock statement does the lock get taken out on each yield (5 times in the example below) or only once for all the items in the list? Thanks private List<string> _data = new List<string>(){"1","2","3","4","5"}; private object _locker =new object(); public IEnumerable<string> GetData() { lock (_locker) { foreach (string s in _data) { yield return s; } } } 回答1: Edit: This answer was wrong, but I can't delete it as it was marked as correct. Please see @Lockszmith's

Deferred execution in C#

我怕爱的太早我们不能终老 提交于 2019-12-13 11:55:02
问题 How could I implement my own deferred execution mechanism in C#? So for instance I have: string x = DoFoo(); Is it possible to perform some magic so that DoFoo does not execute until I "use" x? 回答1: You can use lambdas/delegates: Func<string> doit = () => DoFoo(); // - or - Func<string> doit = DoFoo; Later you can invoke doit just like a method: string x = doit(); I think the closest you can get is something like this: Lazy<string> x = DoFoo; string y = x; // "use" x With a definition of Lazy

GWT - Implement programmatic tab selection of a TabLayoutPanel and then scroll to a particular element contained in the tab?

三世轮回 提交于 2019-12-12 21:19:31
问题 I have a TabLayout panel with 2 tabs. I would like to programmatically select the 2nd tab and then scroll to a particular element within the tab. This is how my code looks like: public void scrollToTextArea(final String textArea) { TabPanel.selectTab(1); //tab selection textArea.getElement().scrollIntoView(); //scroll to text area field } I tried using a deferred command to run the scroll portion, but was still unable to get the right display. Is there a specific way to implement this

How can I evaluate a deferred Linq statement when debugging?

浪子不回头ぞ 提交于 2019-12-12 20:57:17
问题 I'm debugging in VS2010, and I want to inspect a string value but all I can get the debugger to show me (through watches, hovering, locals, etc.) is: "System.Linq.Enumerable+<TakeIterator>d__3a`1[System.Char]" I don't care if there are side effects from premature evaluation or whatever, I just want to see what the expression would evaluate to if I evaluate it right now at the current breakpoint. How is this done? Also can I change my code in such a way that it evaluates earlier? Not that I

Server-side execution of Entity Framework Query combined with Stored Procedure

旧城冷巷雨未停 提交于 2019-12-12 10:21:54
问题 Is it possible to call a StoredProcedure from an ObjectQuery? Basically I want to dynamically build a query and execute it server-side. You can imagine each query to be part of a search where you can combine different criteria with "and" or "or". It is working fine with ObjectQueries created like this. var query1 = from a in objectContext.Articles where a.Name = 'SOMETHING' select new ResultType { ArticleId = a.ArticleId, Name = a.Name }; var query2 = from a in objectContext.Articles where a

Dealing with Arrays of Deferred Objects

拟墨画扇 提交于 2019-12-12 07:35:55
问题 Since using $.Deferred I've run into this scenario a couple times: I have a list of values each of which yields a Deferred Object in some way and I want to execute a callback once all of the Deferred Objects are resolved. A more concrete example would be something like this: var urls = [ 'foo.com', 'bar.com', 'baz.com', 'qux.com' ], defers = [], defer; for( var i = 0, j = urls.length; i < j; i++ ){ defer = $.ajax({ url: 'http://' + urls[ i ] }); defers.push(defer); } $.when.apply(window,

What comes first .always() or .then() callbacks in jQuery? [duplicate]

爱⌒轻易说出口 提交于 2019-12-11 13:36:09
问题 This question already has answers here : jQuery: What is the difference between deferred.always() and deferred.then()? (4 answers) Closed 4 years ago . If you have a function which has both .then and .always callbacks, which one will get executed first? 回答1: Taken from the deferred.resolve() documentation: When the Deferred is resolved, any doneCallbacks added by deferred.then() or deferred.done() are called. Callbacks are executed in the order they were added. Example below: var $logger = $(

Is the order of execution of Linq the reason for this catch?

放肆的年华 提交于 2019-12-11 05:56:15
问题 I have this function to repeat a sequence: public static List<T> Repeat<T>(this IEnumerable<T> lst, int count) { if (count < 0) throw new ArgumentOutOfRangeException("count"); var ret = Enumerable.Empty<T>(); for (var i = 0; i < count; i++) ret = ret.Concat(lst); return ret.ToList(); } Now if I do: var d = Enumerable.Range(1, 100); var f = d.Select(t => new Person()).Repeat(10); int i = f.Distinct().Count(); I expect i to be 100, but its giving me 1000! My question strictly is why is this

Are deferred functions called when SIGINT is received in Go?

五迷三道 提交于 2019-12-10 15:56:50
问题 For the snippet below, the deferred call is not made when ^C is received. Is it possible that the cleanup introduces a race condition? If yes, what could be a better pattern of cleanup on receiving an interrupt? func fn() { // some code defer cleanup() go func() { c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt) // Block until a signal is received. _ = <-c cleanup() } for { // Infinite loop. Returns iff an error is encountered in the // body } } 回答1: Note that if you "install" your

How to maintain LINQ deferred execution?

荒凉一梦 提交于 2019-12-10 03:11:28
问题 Suppose I have an IQueryable<T> expression that I'd like to encapsulate the definition of, store it and reuse it or embed it in a larger query later. For example: IQueryable<Foo> myQuery = from foo in blah.Foos where foo.Bar == bar select foo; Now I believe that I can just keep that myQuery object around and use it like I described. But some things I'm not sure about: How best to parameterize it? Initially I've defined this in a method and then returned the IQueryable<T> as the result of the