-2

I have a program (.NET Core C#) that delete data from DB, for each account - deletes all the data related to the account. I want to add an option to stop the program but finish the deletion for the person the program is on and then stop and exit. I want to do something like this, when I press any key then the program will finish printing 10 entries and then stop and exit. this is for example :

public static void Main(string[] args)
{
    // ???
    
}


static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
    Console.WriteLine("Shutting down...");
    // Cleanup here
}

private static async Task MyTask(CancellationToken cancellationToken)
{
    Print(cancellationToken);
}

public static void Print(CancellationToken cancellationToken)
{
    while (cancellationToken.IsCancellationRequested!)
    {
        for (int i = 1; i < 10; i++)
        {
            Console.WriteLine(i);
        }
    }
}

I would like to any idea you have,
Thanks!

Xodya
  • 1
  • 1
  • Maybe I do not understand the complete question: When I have to break/cancel situation, I will have to check an condition on the begin (or end) of the loop, and this has to be set from outside (yeah, maybe if this is async or thread you have to solve that communication task) but basically: It comes down to a "if (breakcondition) ...." – nabuchodonossor Jun 23 '21 at 09:56
  • i mean to get imformation from outside , e.g ctrl+c or something else, and when i click it, let the program finish to delete the account data her is on, and then stop and exit. – Xodya Jun 23 '21 at 10:06

1 Answers1

3

Just don't pass cancellationToken to dbContext. Check cancellationToken manually when ever you can stop your program.

private static async Task DeleteAllAccounts(CancellationToken cancellationToken)
{
    var accounts = await GetAccountsAsync(cancellationToken);
    foreach (var account in accounts)
    {
        await DeleteAccountAsync(account, CancellationToken.None);
        if (cancellationToken.IsCancellationRequested)
            break;
    }
}
MrMoeinM
  • 2,080
  • 1
  • 11
  • 16
  • Hey,thanks for the answer! how i can get information from outside ? i meant when i click ctrl+c or something else – Xodya Jun 23 '21 at 10:03
  • @Xodya check out this link. https://stackoverflow.com/questions/1119841/net-console-application-exit-event – MrMoeinM Jun 23 '21 at 10:10
  • hey i think it's not help me... can you write for me small example please? with what you wrote up and with canceltoken from outside ... – Xodya Jun 23 '21 at 10:24
  • @Xodya if you looking for detecting ctrl+c check this link too https://www.meziantou.net/detecting-console-closing-in-dotnet.htm – MrMoeinM Jun 23 '21 at 10:27
  • 2
    You might consider using `ThrowIfCancellationRequested` instead, since this will put the task into a cancelled state instead of RanToCompletion. If you check 'IsCancellationRequested' the caller has no real way to know if all accounts where deleted or not. – JonasH Jun 23 '21 at 10:57