c#-5.0

A code example illustrating the difference between the paradigms of async/await and Reactive (Rx) extension?

Deadly 提交于 2019-12-02 16:22:59
Both the System. Reactive extension for .NET and new C# 5.0 (.NET 4.5) async/await pursue (or based on) future and promises constructs paradigm (approach). Can you give the (*) simplest C# code example illustrating the difference between them? (*) Is it possible without I/O, internet or database connections? Update: Well, let me reformulate if this question seemed to be answered before. Why would one add and start using Reactive (Rx) extensions for .NET while using native .NET Iobservable / IObserver + await/async ? What are the possible illustrations of what one will be missing from Rx that

Wrapping synchronous code into asynchronous call

天大地大妈咪最大 提交于 2019-12-02 14:03:55
I have a method in ASP.NET application, that consumes quite a lot of time to complete. A call to this method might occur up to 3 times during one user request, depending on the cache state and parameters that user provides. Each call takes about 1-2 seconds to complete. The method itself is synchronous call to the service and there is no possibility to override the implementation. So the synchronous call to the service looks something like the following: public OutputModel Calculate(InputModel input) { // do some stuff return Service.LongRunningCall(input); } And the usage of the method is

Getting an Exception while calling some async methods from outside the class

点点圈 提交于 2019-12-02 13:23:44
lets say i have class called x and y like this class x { public x() { p(); } private async p() { await q(); } private async p() { //some logic is there } } in test.aspx.cs file i am trying to create an instance of this class x object =new x(); when i run this i get an exception at runtime saying : An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>.

Recurring Background Task [closed]

て烟熏妆下的殇ゞ 提交于 2019-12-02 10:54:41
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . I am just starting to try to use "Tasks" instead of Threads and am trying to implement an object with a background "cleanup" task that runs every 5 minutes as long as the object is in use but which should not block garbage collection. Something crudely along the lines of (which obviously doesn't

Reproduce Capturing iteration variable issue

醉酒当歌 提交于 2019-12-02 06:09:53
问题 I'm rereading a part from c# 5.0 in Nutshell about the capturing iteration variables (Page 138) and I have tried to reproduce the code bellow on c# 4.0 and c# 5.0 but with no hope to catch the difference until now using System; class Test { static void Main() { Action[] actions = new Action[3]; int i = 0; foreach (char c in "abc") actions[i++] = () => Console.Write(c); for (int j = 0; j < 3; j++) { actions[j](); } foreach (Action a in actions) a(); Console.ReadLine(); } } Note I have Visual

ReactiveAsyncCommand missing in ReactiveUI 5.0.2

落花浮王杯 提交于 2019-12-02 04:56:13
问题 I just start learn ReactiveUI from https://github.com/reactiveui/ReactiveUI.Samples/blob/master/ReactiveUI_4Only.Samples.sln. I download lastest version via nuget but I cant find class ReactiveAsyncCommand in ReactiveUI.Xaml. 回答1: ReactiveAsyncCommand is now in ReactiveUI.Legacy . 来源: https://stackoverflow.com/questions/17792826/reactiveasynccommand-missing-in-reactiveui-5-0-2

ReactiveAsyncCommand missing in ReactiveUI 5.0.2

▼魔方 西西 提交于 2019-12-02 03:12:27
I just start learn ReactiveUI from https://github.com/reactiveui/ReactiveUI.Samples/blob/master/ReactiveUI_4Only.Samples.sln . I download lastest version via nuget but I cant find class ReactiveAsyncCommand in ReactiveUI.Xaml. ReactiveAsyncCommand is now in ReactiveUI.Legacy . 来源: https://stackoverflow.com/questions/17792826/reactiveasynccommand-missing-in-reactiveui-5-0-2

C# async/await strange behavior in console app

天涯浪子 提交于 2019-12-01 16:23:19
I built some async/await demo console app and get strange result. Code: class Program { public static void BeginLongIO(Action act) { Console.WriteLine("In BeginLongIO start... {0} {1}", (DateTime.Now.Ticks - ticks) / TimeSpan.TicksPerMillisecond, Thread.CurrentThread.ManagedThreadId); Thread.Sleep(1000); act(); Console.WriteLine("In BeginLongIO end... \t{0} {1}", (DateTime.Now.Ticks - ticks) / TimeSpan.TicksPerMillisecond, Thread.CurrentThread.ManagedThreadId); } public static Int32 EndLongIO() { Console.WriteLine("In EndLongIO start... \t{0} {1}", (DateTime.Now.Ticks - ticks) / TimeSpan

Code coverage for async methods

限于喜欢 提交于 2019-12-01 15:34:42
When I analyse code coverage in Visual Studio 2012, any of the await lines in async methods are showing as not covered even though they are obviously executing since my tests are passing. The code coverage report says that the uncovered method is MoveNext , which is not present in my code (perhaps it's compiler-generated). Is there a way to fix code coverage reporting for async methods? Note : I just ran coverage using NCover, and the coverage numbers make a lot more sense using that tool. As a workaround for now, I'll be switching to that. This can happen most commonly if the operation you're

Should method that get Task and passes it away await it?

狂风中的少年 提交于 2019-12-01 14:48:00
问题 I have two following methods public async Task<bool> DoSomething(CancellationToken.token) { //do something async } //overload with None token public /*async*/ Task<bool> DoSomething() { return /*await*/ DoSomething(CancellationToken.None); } Should second method be marked with async/await keywords or not? 回答1: It doesn't need to - if you use await/async in the second method, you'll be adding extra overhead which accomplishes nothing, in this case. The async work inside of DoSomething