c#-5.0

Is it OK to have virtual async method on base class?

99封情书 提交于 2019-12-03 04:12:16
I am working with some code, where I have 2 classes with very similar logic and code. I have protected async void LoadDataAsync() method on both classes. Currently I am refactoring it and thinking to move shared logic to base class. Is it OK to have virtual async method on base class and override it on derived classes? Are there any issues with it? My code looks like this: public class Base { protected virtual async void LoadDataAsync() {} } public class Derived : Base { protected override async void LoadDataAsync() { // awaiting something } } Similar (but not same) question was already asked.

C# 5 async/await thread mechanics feel wrong?

妖精的绣舞 提交于 2019-12-03 03:32:26
Why have the calling thread walk into the async method until the inner 'await'? Isn't it cleaner to just spawn a thread as soon as an async method is called. That way you know for sure that the async method returns immediately. You don't have to worry about not doing anything expensive at the early stages of the async method. I tend to like to know whether a method is going to execute code on 'my' thread or not. Whether it's blocking or not. This model seems to open a whole spectrum of in-between possibilities. The designers are much smarter than I am so I'm sure there is a good reason, I'd

How to attach CancellationTokenSource to DownloadStringTaskAsync method and cancel the async call?

巧了我就是萌 提交于 2019-12-03 02:22:35
I an creating a sample example to call link using WebClient using async and await method now I want to attach cancel async call functionality also. But I am not able to get CancellationTokenSource token and attach DownloadStringTaskAsync to this cancellation token. Following Is my code can anyone tell me how to accomplish this. private async void DoWork() { this.Cursor = Cursors.WaitCursor; Write("DoWork started."); cts = new CancellationTokenSource(); WebClient wc = new WebClient(); string result = await wc.DownloadStringTaskAsync(new Uri("http://gyorgybalassy.wordpress.com")); if (result

TcpListener: how to stop listening while awaiting AcceptTcpClientAsync()?

依然范特西╮ 提交于 2019-12-03 02:04:56
I don't know how to properly close a TcpListener while an async method await for incoming connections. I found this code on SO, here the code : public class Server { private TcpListener _Server; private bool _Active; public Server() { _Server = new TcpListener(IPAddress.Any, 5555); } public async void StartListening() { _Active = true; _Server.Start(); await AcceptConnections(); } public void StopListening() { _Active = false; _Server.Stop(); } private async Task AcceptConnections() { while (_Active) { var client = await _Server.AcceptTcpClientAsync(); DoStuffWithClient(client); } } private

Await on the last method line

萝らか妹 提交于 2019-12-03 01:50:13
Still learning about async-await. I bumped into examples similar to following: public async Task MethodAsync() { await Method01Async(); await Method02Async(); } What is the purpose of the last await? Method02Async is the last line of MethodAsync method. So there is no any method remainder - no any lines below - no anything to be called in the callback generated by the compiler... Am I missing anything? There actually is a "method remainder" - it completes the Task returned by MethodAsync . (The return value of) Method02Async is awaited so that MethodAsync is not completed until Method02Async

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

隐身守侯 提交于 2019-12-02 23:29:34
问题 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

Why does TaskCanceledException occur?

喜欢而已 提交于 2019-12-02 23:14:52
I have the following test code: void Button_Click(object sender, RoutedEventArgs e) { var source = new CancellationTokenSource(); var tsk1 = new Task(() => Thread1(source.Token), source.Token); var tsk2 = new Task(() => Thread2(source.Token), source.Token); tsk1.Start(); tsk2.Start(); source.Cancel(); try { Task.WaitAll(new[] {tsk1, tsk2}); } catch (Exception ex) { // here exception is caught } } void Thread1(CancellationToken token) { Thread.Sleep(2000); // If the following line is enabled, the result is the same. // token.ThrowIfCancellationRequested(); } void Thread2(CancellationToken token

C# 5.0 async/await feature and Rx - Reactive Extensions

这一生的挚爱 提交于 2019-12-02 20:04:42
I am wondering what do the new C# 5.0 asynchronous features mean for Rx - Reactive Extensions? It seems to be not a replacement but they seem to overlap - Task and IObservable . Check also: TPL Dataflow Overview about TDF and Rx: Astute readers may notice some similarities between TPL Dataflow and Reactive Extensions (Rx), currently available as a download from the DevLabs prototypes site. Rx is predominantly focused on coordination and composition of event streams with a LINQ-based API, providing a rich set of combinators for manipulating IObservables of data. In contrast, TPL Dataflow is

await/async vs. “classic” asynchronous (callbacks)

 ̄綄美尐妖づ 提交于 2019-12-02 17:42:11
So the new async CTP is very cool; it makes my life a lot easier not having to write named callback methods and makes the intent of the methods a lot clearer. Now that I've gotten to play with it a little, I'm wondering what differences there may be between the async/await and the "classic" asynchronous callback syntaxes. Here are a few questions I have in mind, but there are numerous others that I won't have thought of now and probably will later. Does one perhaps offer superior performance over the other? Is there an overhead to one that is greater than the other? Which would be better to

C# params apparent compiler bug (C# 5.0) [closed]

泪湿孤枕 提交于 2019-12-02 16:31:46
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 7 years ago . This is a followup on a thread I thought was resolved yesterday. Yesterday I was having problems with my code in the following case: using System; using