Beispiel #1
0
        public void Play(Dictionary <long, List <KeyEvent> > keyPlaybackBuffer, int interludeDelay)
        {
            if (!Playing)
            {
                KeyPlayingTask = Task.Run(
                    async() =>
                {
                    var currentTimestamp  = 0L;
                    var keyInputSequences = KeyInputSequencer.BuildSequence(keyPlaybackBuffer);
                    var enumerator        = keyInputSequences.GetEnumerator();

                    SW = new Stopwatch();
                    SW.Start();

                    while (enumerator.MoveNext())
                    {
                        try
                        {
                            await Task.Delay(interludeDelay);

                            currentTimestamp = SW.ElapsedMicroseconds();
                            var err          = NativeMethods.SendInput(
                                (uint)enumerator.Current.InputSequence.Length,
                                enumerator.Current.InputSequence,
                                Marshal.SizeOf(typeof(INPUT)));

                            if (err > 1)
                            {
                                await KeyEventMessageChannelWriter
                                .WriteAsync(string.Format(SendInputError, err));
                            }
                            else
                            {
                                await KeyEventMessageChannelWriter
                                .WriteAsync(
                                    string.Format(KeySequenceInputLog, enumerator.Current.KeySequence, currentTimestamp));
                            }
                        }
                        catch (Exception ex)
                        {
                            await KeyEventMessageChannelWriter
                            .WriteAsync(
                                string.Format(UknownError, ex.Message, ex.StackTrace));
                        }
                    }

                    Playing = false;
                });
            }
        }
Beispiel #2
0
        public void PlayWithTimeSynchronization(Dictionary <long, List <KeyEvent> > keyPlaybackBuffer)
        {
            if (!Playing)
            {
                KeyPlayingTask = Task.Run(
                    async() =>
                {
                    var currentTimestamp  = 0L;
                    var keyInputSequences = KeyInputSequencer.BuildSequence(keyPlaybackBuffer);
                    var enumerator        = keyInputSequences.GetEnumerator();

                    SW = new Stopwatch();
                    SW.Start();

                    while (enumerator.MoveNext())
                    {
                        try
                        {
                            // Sleep - Long Wait - Low CPU
                            currentTimestamp        = SW.ElapsedMicroseconds();
                            var millisecondsToSleep = (enumerator.Current.FrameTimestamp - currentTimestamp) / 1_000.0 - ThresholdAwaitTaskDelayInMilliseconds;
                            if (millisecondsToSleep > SleepAccuracyAdjustmentInMicroseconds)
                            {
                                await Task.Delay((int)millisecondsToSleep);
                            }

                            // NO-OP - Short Wait - High CPU
                            while (SW.ElapsedMicroseconds() < enumerator.Current.FrameTimestamp - SleepAccuracyAdjustmentInMicroseconds)
                            {
                            }

                            currentTimestamp = SW.ElapsedMicroseconds();
                            var err          = NativeMethods.SendInput(
                                (uint)enumerator.Current.InputSequence.Length,
                                enumerator.Current.InputSequence,
                                Marshal.SizeOf(typeof(INPUT)));

                            if (err > 1)
                            {
                                await KeyEventMessageChannelWriter
                                .WriteAsync(string.Format(SendInputError, err));
                            }
                            else
                            {
                                await KeyEventMessageChannelWriter
                                .WriteAsync(
                                    string.Format(KeySequenceInputLog, enumerator.Current.KeySequence, currentTimestamp));
                            }
                        }
                        catch (Exception ex)
                        {
                            await KeyEventMessageChannelWriter
                            .WriteAsync(
                                string.Format(UknownError, ex.Message, ex.StackTrace));
                        }
                    }

                    Playing = false;
                });
            }
        }