Set the Wait task to run until true

I am trying to use the function below to not return true / false if the Boolean Function boolean function does not return true or the timeout expires. In the current state, if the boolean function arg returns false, it immediately returns false instead of a loop and retries for more than a millisecond.

 public delegate bool BooleanFunction ();

    public static async Task<bool> Wait(uint Milliseconds, BooleanFunction Function)
    {
        var StartTime = Environment.TickCount;

        do
        {
            if (Function())
            {
                return true;
            }

            Thread.Yield();
        }
        while (Environment.TickCount < StartTime + Milliseconds);

        return false;
    }
+4
source share
2 answers

You need to use await Task.YieldinsteadThread.Yield

    if (Function())
    {
        return true;
    }

    await Task.Yield();

If you also want to handle the transfer of asynchronous delegates to Wait, save the existing version and add the following overload:

public static async Task<bool> Wait(uint Milliseconds, Func<Task<bool>> Function)
{
    var StartTime = Environment.TickCount;

    do
    {
        if (await Function())
        {
            return true;
        }

        Thread.Yield();
    }
    while (Environment.TickCount < StartTime + Milliseconds);

    return false;
}

:

   var result = await Wait(10000, async () => await Test());     
+2

Microsoft , cancelationTokenSource cancelationToken.

Microsoft

StackOverflow :

CancellationToken CancellationTokenSource- ?

, -, System.Threading.CancellationToken. Wait CancellationToken, .

, , . , , . , -.

, , -, BooleanFunction true, , , .. , , . , , , CancellationIsRequested.

:

, . , , , , , , System.Timer. , -

: buttonStartWait buttonCancelWait. StartWait Wait, CancelWait .

, buttonCancelWait , true.

( Ticks TimeSpan, )

private CancellationTokenSource tokenSource = null;

private async void OnButtonStartWait_clicked(object sender, ...)
{
    this.buttonStartWait.Enabled = false;
    TimeSpan waitTime = GetWaitTime();        
    this.tokenSource = new tokenSource(waitTime);
    // this makes sure that after waitTime cancellation is requested
    await this.Wait(this.TokenSource.Token);
    this.buttonstartWait.Enabled = true;
}

private async Task<bool> Wait(CancellationToken token)
{
    while (!token.IsCancellationRequested)
    {   // do the thing your function is supposed to do.
        DoSomethingShort();

        // if this function takes some time, pass the token to it
        // and let it regularly check if cancellation is requested
        DoSomethingLonger(token);

        // or consider starting a Task which regularly checks
        // if cancellation is requested
        await Task.Run( () => DoSomethingLonger(token), token);
    }
    return token.IsCancellationRequested;
}

// a Wait function that really doesn't do anything but wait
// until cancellation is requested.
private async Task<bool> Wait(CancellationToken token)
{
    while (!token.IsCancellationRequested)
    {   // wait a short while
        await Task.Wait(TimeSpan.FromSeconds(0.1), token;
    }
    return token.IsCancellationRequested;
}

private async void OnButtonCancelWait_clicked(object sender, ...)
{
    if (this.TokenSource != null)
    {
        this.TokenSource.Cancel();
    }
}
+1

Source: https://habr.com/ru/post/1607586/


All Articles