Try doing something like this
private async void BtnTrade1_Buy_Click(object sender, RoutedEventArgs e)
{
if(isRunning)
{
cts.Cancel();
isRunning = false;
return;
}
isRunning = true;
cts = new CancellationTokenSource();
try
{
await RunningNrunnin(cts.Token);
}
catch (OperationCanceledException ex)
{
}
}
public async Task RunningNrunnin(CancellationToken token)
{
while (true)
{
await Task.Run(() => GetTradeTest(token));
await Task.Delay(2000, token);
}
}