I am using async methods and a SemaphoreSlim
object to wait for and detect a click. Eventually, on a different method being called, I want to stop the async method that is being run and continue as if the method completed.
Incase it affects the answer, I am using Windows Forms.
Here is a skeleton of my problem (not my actual code)
private SemaphoreSlim _semaphoreClick = new SemaphoreSlim(0, 1);
static void Main()
{
RunProgram();
SomeFinishedFuncion();
}
async void RunProgram()
{
object thing = await WaitForSemaphoreSlim();
}
async Task<object> WaitForSemaphoreSlim()
{
object thing;
// do stuff
await _semaphoreClick.WaitAsync();
// do stuff
return thing;
}
void OnStopProgram()
{
// Do something here that will stop the RunProgram()
// which will make Main() continue to SomeFinishedFunction()
}