PrepAway - Latest Free Exam Questions & Answers

Which code segment should you insert at line 14?

An application contains code that measures reaction times. The code runs the timer on a thread separate from
the user interface. The application includes the following code. (Line numbers are included for reference only.)

You need to ensure that the application cancels the timer when the user presses the Enter key.
Which code segment should you insert at line 14?

PrepAway - Latest Free Exam Questions & Answers

A.
tokenSource.Token.Register( () => tokenSource.Cancel() );

B.
tokenSource.Cancel();

C.
tokenSource.IsCancellationRequested = true;

D.
tokenSource.Dispose();

One Comment on “Which code segment should you insert at line 14?

  1. dfffdgfd says:

    using System;
    using System.Threading;
    using System.Threading.Tasks;

    namespace HelloWorld
    {
    class Hello
    {

    static int RunTimer(CancellationToken cancellationToken)
    {
    var time = 0;
    while (!cancellationToken.IsCancellationRequested)
    time++;
    return time;
    }
    static void Main(string[] args)
    {
    var tokenSource = new CancellationTokenSource();
    var task = Task.Factory.StartNew(() => RunTimer(tokenSource.Token));
    Console.WriteLine(“Press [Enter] to stop”);
    Console.ReadLine();
    tokenSource.Cancel();
    Console.WriteLine(“Time stop at {0}”, task.GetAwaiter().GetResult());
    Console.ReadLine();
    }
    }
    }




    7



    0

Leave a Reply