Is it possible to await on a callback from a Event Aggregator such as prism?
MessageManager.Subscribe("Reply", this.GetType().Name, Myfunc);
private void Myfunc(object obj)
{
}
public async void MyFunc2()
{
MessageManager.Publish("Request", xxx);
await [Message Reply]
}
There is the ugly method of Subscribe followed by a unsubscribe in a lambda but this is not very pretty.
Is there a better way to do that?
EDIT:
This is the solution I have so far:
private SemaphoreSlim dataReady = new SemaphoreSlim(0, 1);
MeasurementDataSet lastDataSet;
public Calibrator(IMessageManager msgMgr)
{
MessageManager = msgMgr;
MessageManager.Subscribe("NewMeasurementData", this.GetType().Name, OnMeasurementData);
}
private void OnMeasurementData(object obj)
{
lastDataSet = (MeasurementDataSet) obj;
dataReady.Release();
}
public async void Calibrate(IParameterCache prmCache, Action<string> updateText)
{
MessageManager.Publish("InitiateMeasurement", "CAL ");
await dataReady.WaitAsync();
// do stuff
MessageManager.Publish("InitiateMeasurement", "CAL ");
await dataReady.WaitAsync();
// do stuff
MessageManager.Publish("InitiateMeasurement", "CAL ");
await dataReady.WaitAsync();
// do stuff
}
You can use a TaskCompletionSource<T> to await an event
public partial class MainWindow : Window
{
IEventAggregator _eventAggregator;
public MainWindow()
{
InitializeComponent();
_eventAggregator = new EventAggregator();
_eventAggregator.GetEvent<PubSubEvent<Request>>().Subscribe( payload =>
{
Thread.Sleep( 1000 ); // only fake work
_eventAggregator.GetEvent<PubSubEvent<Response>>().Publish( new Response() );
}, ThreadOption.BackgroundThread );
}
private async void Button_Click( object sender, RoutedEventArgs e )
{
( (Button)sender ).IsEnabled = false;
try
{
var tcs = new TaskCompletionSource<Response>();
using ( var token = _eventAggregator.GetEvent<PubSubEvent<Response>>().Subscribe( payload =>
{
tcs.TrySetResult( payload );
}, ThreadOption.BackgroundThread ) )
{
_eventAggregator.GetEvent<PubSubEvent<Request>>().Publish( new Request() );
await tcs.Task;
}
}
finally
{
( (Button)sender ).IsEnabled = true;
}
}
}
public class Request
{
}
public class Response
{
}
Related
I am trying to read async from HttpContent but my code exists on this method. It used to work but I recently had to switch to a console app because I wanted to make a discord bot. Does anyone know why the code "stops" when I use this in a console app?
I've tried using an async Main as suggested but it does not work
I am using this code:
public class BazaarInfo
{
[JsonProperty("products")]
public List<BazaarProduct> Products { get; set; }
public static async Task<BazaarInfo> BuildAsync()
{
string url = "https://api.hypixel.net/skyblock/bazaar";
using (HttpResponseMessage response = await ApiHelper.GetApiClient("application/json").GetAsync(url))
{
if (response.IsSuccessStatusCode)
{
BazaarInfo output = await response.Content.ReadAsAsync<BazaarInfo>(); //Stops
return output;
}
else
{
return null;
}
}
}
}
I call it from here:
public class Bazaar
{
public Dictionary<string, BazaarProduct> Products { get; set; }
public static async Task<Bazaar> BuildAsync()
{
var output = new Bazaar();
var bazaarInfo = await BazaarInfo.BuildAsync();
output.Products = bazaarInfo.Products.ToDictionary(product => product.Name);
return output;
}
}
And this:
[Command("bazaar")]
public async Task BuildBzItem ([Remainder]string id)
{
var bazaar = await Bazaar.BuildAsync();
string sellSummary = "";
foreach (var summary in bazaar.Products[id].SellSummary)
sellSummary += summary.Amount + summary.Price;
var builder = new Discord.EmbedBuilder()
{
Description = sellSummary
};
await ReplyAsync("", false, builder.Build());
}
And then here with a discord chat event:
private async Task HandleCommandAsync(SocketMessage arg)
{
var message = arg as SocketUserMessage;
var context = new SocketCommandContext(Client, message);
if (message.Author.IsBot) return;
int argPos = 0;
if(message.HasStringPrefix("!", ref argPos))
{
var result = await Commands.ExecuteAsync(context, argPos, Services);
if (!result.IsSuccess) Console.Write(result.ErrorReason);
}
}
And this event is assigned here:
public static async Task Main(string[] args) => await new Program().RunBotAsync();
private DiscordSocketClient Client { get; set; }
private CommandService Commands { get; set; }
private IServiceProvider Services { get; set; }
public async Task RunBotAsync()
{
Client = new DiscordSocketClient();
Commands = new CommandService();
Services = new ServiceCollection().AddSingleton(Client).AddSingleton(Commands).BuildServiceProvider();
string token = "ODQ1MzE1OTY2OTcxODA1NzI3.YKfL1w.SPXi_0xXbbrMziZ9JWiqHFX4dto";
Client.Log += ClientLog;
await RegisterCommandsAsync();
await Client.LoginAsync(TokenType.Bot, token);
await Client.StartAsync();
await Task.Delay(-1);
}
private Task ClientLog(LogMessage arg)
{
Console.WriteLine(arg);
return Task.CompletedTask;
}
public async Task RegisterCommandsAsync()
{
Client.MessageReceived += HandleCommandAsync;
await Commands.AddModulesAsync(Assembly.GetEntryAssembly(), Services);
}
I've tried using an async Main as suggested but it does not work
If you can't use async Main, then block in the Main method:
public static void Main(string[] args) => new Program().RunBotAsync().GetAwaiter().GetResult();
I want to write some Unittests with NUnit for our wpf application.
The application downloads some data with System.Net.WebClient in the background using the observer pattern.
Here is an example:
Download.cs
public class Download : IObservable<string>
{
private string url { get; }
private List<IObserver<string>> observers = new List<IObserver<string>>();
private bool closed = false;
private string data = null;
public Download(string url)
{
this.url = url;
startDownload();
}
public IDisposable Subscribe(IObserver<string> observer)
{
if (!observers.Contains(observer))
{
if (!closed)
{
observers.Add(observer);
}
else
{
sendAndComplete(observer);
}
}
return new Unsubscriber(observer, observers);
}
private void startDownload()
{
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler((object sender, DownloadStringCompletedEventArgs e) => {
if (e.Error != null)
{
data = e.Result;
}
closed = true;
sendAndComplete();
});
client.DownloadStringAsync(new Uri(url));
}
private void sendAndComplete()
{
foreach (var observer in observers)
{
sendAndComplete(observer);
}
observers.Clear();
}
private void sendAndComplete(IObserver<string> observer)
{
if (data != null)
{
observer.OnNext(data);
}
else
{
observer.OnError(new Exception("Download failed!"));
}
observer.OnCompleted();
}
private class Unsubscriber : IDisposable
{
private IObserver<string> _observer { get; }
private List<IObserver<string>> _observers { get; }
public Unsubscriber(IObserver<string> _observer, List<IObserver<string>> _observers)
{
this._observer = _observer;
this._observers = _observers;
}
public void Dispose()
{
if (_observer != null && _observers.Contains(_observer))
{
_observers.Remove(_observer);
}
}
}
}
DownloadInspector.cs
public class DownloadInspector : IObserver<string>
{
private Action<string> onSuccessAction { get; }
private Action<Exception> onErrorAction { get; }
private Action onCompleteAction { get; }
public DownloadInspector(Action<string> onSuccessAction, Action<Exception> onErrorAction, Action onCompleteAction)
{
this.onSuccessAction = onSuccessAction;
this.onErrorAction = onErrorAction;
this.onCompleteAction = onCompleteAction;
}
public void OnCompleted()
{
onCompleteAction.Invoke();
}
public void OnError(Exception error)
{
onErrorAction.Invoke(error);
}
public void OnNext(string value)
{
onSuccessAction.Invoke(value);
}
}
example (usage)
Download download = new Download("http://stackoverflow.com");
DownloadInspector inspector = new DownloadInspector(
(string data) =>
{
Debug.WriteLine("HANDLE DATA");
},
(Exception error) =>
{
Debug.WriteLine("HANDLE ERROR");
},
() =>
{
Debug.WriteLine("HANDLE COMPLETE");
}
);
I'm still new in c# and not very familiar with asynchronous programming in that language. I know the await and async keywords and know that they work with NUnit, but the current construct don't use this keywords.
Can you help me creating a unit test for this case? Its okay to change/remove the observer pattern.
The constructor for the Download class starts the download, which means that I can't subscribe an observer until after the download has started. That's a race condition. It's possible (although unlikely) that observers will be notified before they can be subscribed.
public Download(string url)
{
this.url = url;
startDownload();
}
But I can go ahead and test because I'm subscribing an observer before that can happen. If you can I'd recommend not doing that. Allow the caller to construct the class in one step and then start the download with a method call.
I also had to change this method. I figured that testing for an error would be the easiest first step, but it needs to do data = e.Result if there is no error, not if there is an error.
private void StartDownload()
{
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler((object sender, DownloadStringCompletedEventArgs e) =>
{
if (e.Error == null) // <== because of this
{
data = e.Result;
}
closed = true;
sendAndComplete();
});
client.DownloadStringAsync(new Uri(url));
}
What I didn't see coming is that WebClient.DownloadStringAsync isn't actually async. It doesn't return a Task. It just takes a callback. What that means is that there's no sure way to know whether it's done except to wait for it to notify the observer that the download is complete.
My NUnit test runner wasn't running, so I used MsTest. It's the same thing.
The basic approach is that I'm creating some flags, and the inspector responds to notifications by setting the flags. That way I can see which notifications were raised.
The last problem is that because DownloadStringComplete is a callback, the test exits before the Assert. That means it will always pass. So in order to fix it I had to do something I've never seen before, which I found here:
[TestMethod]
public void download_raises_error_notification()
{
var success = false;
bool error = false;
bool complete = false;
var pause = new ManualResetEvent(false);
var download = new Download("http://NoSuchUrlAnywhere.com");
var inspector = new DownloadInspector(
onSuccessAction: s => success = true,
onCompleteAction: () =>
{
complete = true;
pause.Set();
},
onErrorAction: s => error = true
);
download.Subscribe(inspector);
// allow 500ms for the download to fail. This is a race condition.
pause.WaitOne(500);
Assert.IsTrue(error,"onErrorAction was not called.");
}
This is technically an integration test since it must actually attempt a download in order to run. That could be remedied by mocking WebClient.
I am trying to return a bool true if the user selects yes from AlertDialog and visa versa.
at the moment it always returns false. it seems like the bool "result" is never being set.
public bool AskForConfirmation(string messege, Context context)
{
bool result;
Android.Support.V7.App.AlertDialog.Builder dialog = new Android.Support.V7.App.AlertDialog.Builder(context);
dialog.SetPositiveButton("Yes", (sender, args) =>
{
result = true;
});
dialog.SetNegativeButton("No", (sender, args) =>
{
result = false;
}).SetMessage(messege).SetTitle("System Message");
dialog.Show();
return result;
}
And I call the method
this.RunOnUiThread(() =>
{
bool response = ioManager.AskForConfirmation("Message", this);
Console.WriteLine("Response is " + response);
});
You can create a Task-based dialog via a ManualResetEvent or a TaskCompletionSource so you can call it like this:
Usage via TaskCompletionSource Example:
try
{
var result = await DialogAsync.Show(this, "StackOverflow", "Does it rock?");
Log.Debug("SO", $"Dialog result: {result}");
}
catch (TaskCanceledException ex)
{
Log.Debug("SO", $"Dialog cancelled; backbutton, click outside dialog, system-initiated, .... ");
}
DialogAsync via TaskCompletionSource Example:
public class DialogAsync : Java.Lang.Object, IDialogInterfaceOnClickListener, IDialogInterfaceOnCancelListener
{
readonly TaskCompletionSource<bool?> taskCompletionSource = new TaskCompletionSource<bool?>();
public DialogAsync(IntPtr handle, Android.Runtime.JniHandleOwnership transfer) : base(handle, transfer) { }
public DialogAsync() { }
public void OnClick(IDialogInterface dialog, int which)
{
switch (which)
{
case -1:
SetResult(true);
break;
default:
SetResult(false);
break;
}
}
public void OnCancel(IDialogInterface dialog)
{
taskCompletionSource.SetCanceled();
}
void SetResult(bool? selection)
{
taskCompletionSource.SetResult(selection);
}
public async static Task<bool?> Show(Activity context, string title, string message)
{
using (var listener = new DialogAsync())
using (var dialog = new AlertDialog.Builder(context)
.SetPositiveButton("Yes", listener)
.SetNegativeButton("No", listener)
.SetOnCancelListener(listener)
.SetTitle(title)
.SetMessage(message))
{
dialog.Show();
return await listener.taskCompletionSource.Task;
}
}
}
Usage Via ManualResetEvent Example:
using (var cancellationTokenSource = new CancellationTokenSource())
{
var result = await DialogAsync.Show(this, "StackOverflow", "Does it rock?", cancellationTokenSource);
if (!cancellationTokenSource.Token.IsCancellationRequested)
{
Log.Debug("SO", $"Dialog result: {result}");
}
else
{
Log.Debug("SO", $"Dialog cancelled; backbutton, click outside dialog, system-initiated, .... ");
}
}
DialogAsync via ManualResetEvent Example:
public class DialogAsync : Java.Lang.Object, IDialogInterfaceOnClickListener, IDialogInterfaceOnCancelListener
{
readonly ManualResetEvent resetEvent = new ManualResetEvent(false);
CancellationTokenSource cancellationTokenSource;
bool? result;
public DialogAsync(IntPtr handle, Android.Runtime.JniHandleOwnership transfer) : base(handle, transfer) { }
public DialogAsync() { }
public void OnClick(IDialogInterface dialog, int which)
{
switch (which)
{
case -1:
SetResult(true);
break;
default:
SetResult(false);
break;
}
}
public void OnCancel(IDialogInterface dialog)
{
cancellationTokenSource.Cancel();
SetResult(null);
}
void SetResult(bool? selection)
{
result = selection;
resetEvent.Set();
}
public async static Task<bool?> Show(Activity context, string title, string message, CancellationTokenSource source)
{
using (var listener = new DialogAsync())
using (var dialog = new AlertDialog.Builder(context)
.SetPositiveButton("Yes", listener)
.SetNegativeButton("No", listener)
.SetOnCancelListener(listener)
.SetTitle(title)
.SetMessage(message))
{
listener.cancellationTokenSource = source;
context.RunOnUiThread(() => { dialog.Show(); });
await Task.Run(() => { listener.resetEvent.WaitOne(); }, source.Token);
return listener.result;
}
}
}
This is a followup question to the following question:
Volatile IEnlistmentNotification and TransactionScope.AsyncFlowEnabled = true
The approach accepted in the question above works as long as you don't await multiple statements. Let me show an example:
public class SendResourceManager : IEnlistmentNotification
{
private readonly Action onCommit;
public SendResourceManager(Action onCommit)
{
this.onCommit = onCommit;
}
public void Prepare(PreparingEnlistment preparingEnlistment)
{
preparingEnlistment.Prepared();
}
public void Commit(Enlistment enlistment)
{
Debug.WriteLine("Committing");
this.onCommit();
Debug.WriteLine("Committed");
enlistment.Done();
}
public void Rollback(Enlistment enlistment)
{
enlistment.Done();
}
public void InDoubt(Enlistment enlistment)
{
enlistment.Done();
}
}
public class AsyncTransactionalMessageSender : ISendMessagesAsync
{
private readonly List<Message> sentMessages = new List<Message>();
public IReadOnlyCollection<Message> SentMessages
{
get { return new ReadOnlyCollection<Message>(this.sentMessages); }
}
public async Task SendAsync(Message message)
{
if (Transaction.Current != null)
{
await Transaction.Current.EnlistVolatileAsync(
new SendResourceManager(async () => await this.SendInternal(message)),
EnlistmentOptions.None);
}
else
{
await this.SendInternal(message);
}
}
private async Task SendInternal(Message message)
{
Debug.WriteLine("Sending");
await Task.Delay(1000);
this.sentMessages.Add(message);
Debug.WriteLine("Sent");
}
}
[Test]
public async Task ScopeRollbackAsync_DoesntSend()
{
var sender = new AsyncTransactionalMessageSender();
using (var tx = new System.Transactions.TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
await sender.SendAsync(new Message("First"));
await sender.SendAsync(new Message("Second"));
await sender.SendAsync(new Message("Last"));
// We do not commit the scope
}
sender.SentMessages.Should().BeEmpty();
}
[Test]
public async Task ScopeCompleteAsync_Sends()
{
var sender = new AsyncTransactionalMessageSender();
using (var tx = new System.Transactions.TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
await sender.SendAsync(new Message("First"));
await sender.SendAsync(new Message("Second"));
await sender.SendAsync(new Message("Last"));
tx.Complete();
}
sender.SentMessages.Should().HaveCount(3)
.And.Contain(m => m.Value == "First")
.And.Contain(m => m.Value == "Second")
.And.Contain(m => m.Value == "Last");
}
As soon as you introduce a Task.Delay like shown in the example above the generated asynchronous statemachine will never come back and invoke the this.sentMessages.Add(message) and Debug.WriteLine("Sent")
The problem is I currently see now way to properly enlist asynchronous code inside the enlistment notification. Any ideas how to tackle this challenge?
I was wondering how to implement this Android AsyncTask using .NET's async libraries:
public class LoadRecordTask : AsyncTask
{
private Activity1 _context;
private int _recordId;
public LoadRecordTask( Activity1 outerActivity, int recordId )
{
_context = outerActivity;
_recordId = recordId;
}
protected override void OnPreExecute()
{
base.OnPreExecute();
_progressDialog = Android.App.ProgressDialog.Show( _context , "", "Loading record {0}", _recordId );
}
protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] #params)
{
_bSuccess = LoadRecord( _recordId );
if( !_bSuccess )
{
return false;
}
return true;
}
protected override void OnPostExecute(Java.Lang.Object result)
{
base.OnPostExecute(result);
if( !_bSuccess )
{
_progressDialog.SetMessage( "Error loading recording." );
}
_progressDialog.Dismiss();
}
}
The AsyncTask you are using could be translated to something like:
_progressDialog = ProgressDialog.Show( _context , "", "Loading record {0}", _recordId );
if (await LoadRecord(_recordId))
_progressDialog.Dismiss();
else
_progressDialog.SetMessage( "Error loading recording." );
Where LoadRecord could be returning Task<bool> and the internals running inside of Task. Otherwise you can just wrap the LoadRecord method you are currently using in a Task to make it run async.
private Task<bool> LoadRecord(int recordId)
{
return Task<bool>.Run(() =>
{
//Do stuff here to fetch records
return true;
});
}
The method you are calling await from needs to be marked as async. I.e.:
private async void MyAwesomeAsyncMethod() {}