public async Task doItAsync()
    {
        try {
            await Task.Delay(3000);                     // substitute for the real operation

            await pts.WaitWhilePausedAsync();

            (Activity as MainActivity).taskFinished();
        } catch (Exception e) {
            Console.WriteLine(e);
        }
    }
Beispiel #2
0
    private async void LongRunning(PauseTokenSource pause)
    {
        _updateGUI = new updateUI(SetUI);
        for (int i = 0; i < 20; i++)
        {
            await pause.WaitWhilePausedAsync();

            Thread.Sleep(500);
            this.Invoke(_updateGUI, i.ToString() + " => " + txtInput.Text);

            //txtOutput.AppendText(Environment.NewLine + i.ToString());


            if (_cancelToken.IsCancellationRequested)
            {
                this.Invoke(_updateGUI, "Task cancellation requested at " + DateTime.Now.ToString());
                break;
            }
        }
        _updateGUI = null;
    }
Beispiel #3
0
 public Task WaitWhilePausedAsync()
 {
     return(IsPaused ?
            m_source.WaitWhilePausedAsync() :
            PauseTokenSource.completedTask);
 }
Beispiel #4
0
 public Task WaitWhilePausedAsync() => IsPaused?_Source.WaitWhilePausedAsync() : __CompletedTask;
 public Task WaitWhilePausedAsync() => _source.WaitWhilePausedAsync();
Beispiel #6
0
        public async Task SendAudioAsync(IGuild guild, IMessageChannel channel, string file)
        {
            Playlist.Enqueue(file);

            IAudioClient client;

            if (ConnectedChannels.TryGetValue(guild.Id, out client))
            {
                IsBotPlaying = true;
                while (Playlist.Count > 0 && !cancellationToken.IsCancellationRequested)
                {
                    CurrentAudio = IsLooping ? Playlist.Peek() : Playlist.Dequeue();
                    string path = $"{Directory.GetCurrentDirectory()}{Path.DirectorySeparatorChar}Sounds{Path.DirectorySeparatorChar}{CurrentAudio}";
                    if (!File.Exists(path))
                    {
                        IsBotPlaying = false;
                        await channel.SendMessageAsync("File does not exist.");

                        return;
                    }

                    //await Log(LogSeverity.Debug, $"Starting playback of {path} in {guild.Name}");
                    using (var ffmpeg = CreateProcess(path))
                        using (AudioOutStream stream = client.CreatePCMStream(AudioApplication.Music))
                        {
                            try
                            {
                                //await ffmpeg.StandardOutput.BaseStream.CopyToAsync(stream, 81920, cancellationToken.Token);

                                int    blockSize  = 3840; // The size of bytes to read per frame; 1920 for mono
                                byte[] buffer     = new byte[blockSize];
                                byte[] gainBuffer = new byte[blockSize];
                                int    byteCount;

                                while (!cancellationToken.IsCancellationRequested)
                                {
                                    if (pauseToken.IsPaused)
                                    {
                                        await pauseToken.WaitWhilePausedAsync();
                                    }
                                    if (skipAudio)
                                    {
                                        skipAudio = false;
                                        break;
                                    }

                                    byteCount = ffmpeg.StandardOutput.BaseStream.Read(buffer, 0, blockSize);

                                    if (byteCount == 0) // FFmpeg did not output anything
                                    {
                                        break;
                                    }

                                    for (int i = 0; i < blockSize / 2; ++i)
                                    {
                                        // convert to 16-bit
                                        short sample = (short)((buffer[i * 2 + 1] << 8) | buffer[i * 2]);

                                        // scale
                                        if (!IsMute)
                                        {
                                            sample = (short)(sample * Volume + 0.5);
                                        }
                                        else
                                        {
                                            sample = (short)(sample * 0 + 0.5);
                                        }

                                        // back to byte[]
                                        buffer[i * 2 + 1] = (byte)(sample >> 8);
                                        buffer[i * 2]     = (byte)(sample & 0xff);
                                    }

                                    await stream.WriteAsync(buffer, 0, byteCount);
                                }
                            }
                            finally
                            {
                                await stream.FlushAsync();
                            }
                        }
                }
                IsBotPlaying      = false;
                cancellationToken = new CancellationTokenSource();
            }
        }