I'm making an integration test and want a method to wait until "on method" is called. In C#.
Something like this:
public async Task CheckMethod(){
var hubConnection = GetHubConnection();
connection.On("OnMethod",async () =>{
Log("First");
}
await connection.InvokeAsync("Subscribe", id);
// Something to wait then proceed
Log("Second");
}
Try this:
public async Task CheckMethod()
{
var tcs = new TaskCompletionSource<object>();
var hubConnection = GetHubConnection();
connection.On("OnMethod",() =>
{
Log("First");
tcs.TrySetResult(null);
}
await connection.InvokeAsync("Subscribe", id);
Log("Second");
await tcs.Task;
Log("Third");
}
Related
I have this metod:
public async Task StartAsync(Task process)
{
if (process is null)
{
throw new ArgumentNullException(nameof(process));
}
var loading = ...;
await Task.WhenAll(process, loading).ContinueWith(t => EndProgress());
}
and is called via a command like so:
private async Task TestAsync()
{
await StartAsync(new Task(async () =>
{
//just for testing purpose
await Task.Delay(15000);
}));
}
ExecuteDelegate = async param => await TestAsync();
where ExecuteDelegate is an Action<T> delegate used by command.
Why does the await Task.WhenAll line not waiting those 15 seconds from Task.Dalay?
You need to await the call to StartAsync:
private async Task TestAsync()
{
await StartAsync(new Task(async () =>
{
await Task.Delay(15000);
}));
}
NOTE: You can also simplify your code by not creating the redundant Task:
private async Task TestAsync()
{
await StartAsync(Task.Delay(15000));
}
Or even simpler:
private Task TestAsync()
{
return StartAsync(Task.Delay(15000));
}
You shouldn't use the constructor to create a Task but the static Task.Run method:
private async Task TestAsync()
{
await StartAsync(Task.Run(async () =>
{
//just for testing purpose
await Task.Delay(15000);
}));
}
The task returned by Task.Run can be awaited as expected.
I am trying to create a helper class for sending some information periodically to backend server.
Attaching the code below.
public class HeartBeatService
{
private CancellationToken _cancellationToken;
private CancellationTokenSource _cancellationTokenSource;
public void StartHeartBeatService(TimeSpan timeSpan)
{
_cancellationTokenSource = new CancellationTokenSource();
_cancellationToken = _cancellationTokenSource.Token;
Task.Run(async () =>
{
while (!_cancellationToken.IsCancellationRequested)
{
SendHeartBeatToAzure();
try
{
await Task.Delay(timeSpan, _cancellationToken);
}
catch
{
break;
}
}
});
}
public void SuspendHeartBeatService()
{
_cancellationTokenSource?.Cancel();
}
private async void SendHeartBeatToAzure()
{
var platformService = ServiceLocator.Get<IPlatformService>();
var location = await platformService?.GetPositionAsync();
if (!double.IsNaN(location.Item1) && !double.IsNaN(location.Item2))
{
Debug.WriteLine($"Triggering Heartbeat with location{location.Item1},{location.Item2}");
//TODO Invoke heartbeat api call.
}
}
}
The code for sending the information to server is working fine.
But there is some issue with CancellationToken which is not working/it is not cancelling.
not sure what's wrong with the implementation.
Change the signature of the SendHeartBeatToAzure to return a Task, so that it can be awaited:
private async Task SendHeartBeatToAzure()
Then await the task returned by the method inside the loop. To achieve a stable and consisted heartbeat, it is a good idea to create the Task.Delay task before calling the method:
Task.Run(async () =>
{
while (true)
{
var delayTask = Task.Delay(timeSpan, _cancellationToken);
await SendHeartBeatToAzure();
await delayTask;
}
});
As a side note, you should probably store the task returned by Task.Run as a readonly property of the HeartBeatService class, so that the status of the task can be monitored.
We are running into a situation where have a requirement to start and execute few launch and forget threads during a call. Though, our call fails to execute if the async methods have any awaited call.
Here is an example. Are we missing something?
public class SomeClass
{
public async Task Test()
{
//Calling synchronously this things works
await Save(1).ConfigureAwait(false);
await Save(2).ConfigureAwait(false);
await Save(3).ConfigureAwait(false);
//Starting three threads at the same time fails while trying to run var queryResult = await SomeClient.QueryAsync<T>(q).ConfigureAwait(false);
_ = Task.Run(async () => await Save(1));
_ = Task.Run(async () => await Save(2));
_ = Task.Run(async () => await Save(3));
}
public async Task<bool> Save(int ct)
{
var x = await Update(ct).ConfigureAwait(false);
return x;
}
public async Task<bool> Update(int ct)
{
await _someObject.CallingSomeAsyncMethod<dynamic>("Some Query").ConfigureAwait(false);
await _someObject.CallingSomeAsyncMethod<dynamic>("Some Query").ConfigureAwait(false);
await _someObject.CallingSomeAsyncMethod<dynamic>("Some Query").ConfigureAwait(false);
return true;
}
}
public class SomeObject
{
public async Task<T> CallingSomeAsyncMethod(string q)
{
await Task.Delay(1000).ConfigureAwait(false);
//OR Any async method which is awaited here just stops the execution
return queryResult;
}
}
If you want to run multiple tasks at the same time you should call the methods without the await and hold the task. Then you can do await Task.WhenAll(task1, task2, task3, ...);
I have such a function:
public async Task<bool> DoSomething()
{
var tcs = new TaskCompletionSource<bool>();
// Here is the problem. I need to keep this line because I wait on something asynchronously, but the function must return bool and I can't just return tcs.Task
while(something)
await Task.Delay(100);
someobject.somevent += () => {
// do some sht
tcs.SetResult(true);
}
// it doesn't work
return tcs.Task;
}
It's just a fake code but I have real situation where I need this. I want to keep DoSomething asynchronous but I also want to keep Task.Delay/Sleep in it. How do I do this in not-async function returning just Task?
UPDATE:
THIS WORKS:
class Program
{
static TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
static Task<bool> Test()
{
// tcs = new TaskCompletionSource<bool>();
Task.Factory.StartNew(() =>
{
Console.WriteLine("Waiting...");
Thread.Sleep(5000);
Console.WriteLine("Setting result");
if(tcs.TrySetResult(true))
Console.WriteLine("Result has been set");
});
return tcs.Task;
}
static async Task Test2()
{
Console.WriteLine("Starting awaiting");
var result = await Test();
Console.WriteLine(result.ToString());
}
static void Main(string[] args)
{
Test2();
Console.ReadKey(false);
}
}
and this doesn't
static async Task<bool> Test()
{
// tcs = new TaskCompletionSource<bool>();
Task.Factory.StartNew(() =>
{
Console.WriteLine("Waiting...");
Thread.Sleep(5000);
Console.WriteLine("Setting result");
if(tcs.TrySetResult(true))
Console.WriteLine("Result has been set");
});
return await tcs.Task;
}
what's worse, I have tested it in my windows forms app and awaiting tcs.Task caused weird crash coming from System.Threading....dll
If I understand correctly (it's tricky because your question isn't that easy to follow) you can restate things as follows:
public async Task<bool> DoSomething()
{
var tcs = new TaskCompletionSource<bool>();
someobject.somevent += () => {
// do some sht
tcs.SetResult(true);
}
return await tcs.Task;
}
The whole thing will come out a lot more elegantly if you separate out the logic of turning the event firing into a Task into its own method.
public static Task<bool> WhenSomeEvent(this SomeObject someobject)
{
var tcs = new TaskCompletionSource<bool>();
Action handler = null;
handler = () =>
{
tcs.SetResult(true);
someobject.SomeEvent -= handler;
};
someobject.SomeEvent += handler;
return tcs.Task;
}
This allows you to write the business logic separately, without mixing in all of the logic of translating the event into a Task:
public async Task<bool> DoSomething()
{
while(something)
await Task.Delay(100);
return await someobject.WhenSomeEvent();
}
I have a method with some code that does an await operation:
public async Task DoSomething()
{
var x = await ...;
}
I need that code to run on the Dispatcher thread. Now, Dispatcher.BeginInvoke() is awaitable, but I can't mark the lambda as async in order to run the await from inside it, like this:
public async Task DoSomething()
{
App.Current.Dispatcher.BeginInvoke(async () =>
{
var x = await ...;
}
);
}
On the inner async, I get the error:
Cannot convert lambda expression to type 'System.Delegate' because it is not a delegate type.
How can I work with async from within Dispatcher.BeginInvoke()?
The other answer may have introduced an obscure bug. This code:
public async Task DoSomething()
{
App.Current.Dispatcher.Invoke(async () =>
{
var x = await ...;
});
}
uses the Dispatcher.Invoke(Action callback) override form of Dispatcher.Invoke, which accepts an async void lambda in this particular case. This may lead to quite unexpected behavior, as it usually happens with async void methods.
You are probably looking for something like this:
public async Task<int> DoSomethingWithUIAsync()
{
await Task.Delay(100);
this.Title = "Hello!";
return 42;
}
public async Task DoSomething()
{
var x = await Application.Current.Dispatcher.Invoke<Task<int>>(
DoSomethingWithUIAsync);
Debug.Print(x.ToString()); // prints 42
}
In this case, Dispatch.Invoke<Task<int>> accepts a Func<Task<int>> argument and returns the corresponding Task<int> which is awaitable. If you don't need to return anything from DoSomethingWithUIAsync, simply use Task instead of Task<int>.
Alternatively, use one of Dispatcher.InvokeAsync methods.
I think you can use below code and then depends of place use it with async and await or without to fire and forget:
public static Task FromUiThreadAsync(Action action)
{
TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
Dispatcher disp = GetUiDispatcher();
disp.Invoke(DispatcherPriority.Background, new Action(() =>
{
try
{
action();
tcs.SetResult(true);
}
catch (Exception ex)
{
tcs.SetException(ex);
}
}));
return tcs.Task;
}
Use Dispatcher.Invoke()
public async Task DoSomething()
{
App.Current.Dispatcher.Invoke(async () =>
{
var x = await ...;
});
}
(Edit: This answer is wrong, but I'll fix it soon)
Declare this
public async Task DoSomethingInUIThreadAsync(Func<Task> p)
{
await Application.Current.Dispatcher.Invoke(p);
}
Use like this
string someVar = "XXX";
DoSomethingInUIThreadAsync(()=>{
await Task.Run(()=> {
Thread.Sleep(10000);
Button1.Text = someVar;
});
});
DoSomethingInUIThreadAsync receives a delegate that returns a Task, Application.Current.Dispatcher.Invoke accepts a Func callback that can be awaited.