Is it possible to know that my method is being called using 'await' keyword or not?
For example; method MyAsync() would like to know whether its being awaited or not.
public void CallForBackground()
{
MyAsync();
}
public async void CallAsync()
{
bool result = await MyAsync();
}
public Task<bool> MyAsync()
{
return Task.Run(() =>
{
//do something endlessly if its being called without 'await', but how do I know the caller is 'await'ing or not?
//do something only once if its being called with 'await', but how do I know the caller is 'await'ing or not?
return true;
});
}
EDITED: Added a scenario where I would like to know whether MyAsync() is being called with or without 'await';
1) Let's say MyAsync() is connecting to a remote server using TCP.
2) MyAsync() is asynchronous and awaitable.
3) Let's say UserA wanted to connect to remote server asynchronously and once completed he/she would like to know whether it was successful or not before continuing his process. Something similar to CallAsync() method implementation.
4) And UserB wants to fire MyAsync() method, without waiting for it to complete. In this case, UserB don't care whether it was connected or not until he sends a message to remote server. Something similar to CallForBackground() method implementation.
So for UserA (awaiting) MyAsync() method will attempt once and return the status. But for UserB (not awaiting), method will keep trying. For this logic, MyAsync() needs to know whether its being awaiting or not. How do we achieve this?