我如何等待同一事件发生?

时间:2016-04-08 16:33:54

标签: c# events

我正在尝试录制音频,而我并不想简单地使用source 'https://rubygems.org' gem 'rails', '4.2.2' # Use SCSS for stylesheets gem 'sass-rails', '~> 5.0' # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '>= 1.3.0' # Use jquery as the JavaScript library gem 'jquery-rails' # Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks gem 'turbolinks' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 2.0' # bundle exec rake doc:rails generates the API under doc/api. gem 'sdoc', '~> 0.4.0', group: :doc # rake gem 'rake', '11.1.2' # csv reader gem 'smarter_csv' # bower rails gem 'bower-rails' # angular templates gem 'angular-rails-templates' # angular material gem 'rails-angular-material' # ionicons gem 'ionicons-rails' # Get user location info gem 'geocoder' group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug' # Use sqlite3 as the database for Active Record gem 'sqlite3' end # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' group :development do # Access an IRB console on exception pages or by using <%= console %> in views gem 'web-console', '~> 2.0' end group :test do gem 'minitest-reporters', '1.0.5' gem 'mini_backtrace', '0.1.3' gem 'guard-minitest', '2.3.1' end group :production do gem 'pg' gem 'rails_12factor' end ruby "2.2.1" 功能,因为我想自己控制录音。这就是我所拥有的......

System.Threading.Thread.Sleep(x)

我想这样做,以便当我再次点击 private void buttonStart_Click(object sender, EventArgs f) { using (WasapiCapture capture = new CSCore.SoundIn.WasapiLoopbackCapture()) { //initialize the selected device for recording capture.Initialize(); //... stuff... //create a wavewriter to write the data to using (WaveWriter w = new WaveWriter(@directory, capture.WaveFormat)) { capture.DataAvailable += (s, e) => { //save the recorded audio w.Write(e.Data, e.Offset, e.ByteCount); }; //start recording capture.Start(); //wait until button is pressed again... //stop recording capture.Stop(); } } } 时,buttonStart行将会运行。我对C#很陌生,所以我只想问。

2 个答案:

答案 0 :(得分:0)

 WasapiCapture capture;

 private void buttonStart_Click(object sender, EventArgs f)
    {
       if(capture == null)
                capture = new new CSCore.SoundIn.WasapiLoopbackCapture();
                capture.Initialize();
                capture.DataAvailable += (s, e) =>
                {
                    //save the recorded audio
                    using (WaveWriter w = new WaveWriter(@directory, capture.WaveFormat))
                    {
                        w.Write(e.Data, e.Offset, e.ByteCount);
                    }
                };

                //start recording
                capture.Start();
            }
        else {   
                //stop recording
                capture.Stop();
                capture = null;
            }
        }
    }

答案 1 :(得分:-1)

要让某些代码 等到...... ,您可以使用线程。一旦实现等待某个事件发生的最佳方式正在使用AutoResetEvent

public class Program
{
    // AutoResetEvent constructor argument is if the event should be start
    // signaled or not.
    //
    // A ResetEvent is like a door. If it's signaled, the door is open,
    // and if it's not signaled, the door is closed.
    private static readonly AutoResetEvent ResetEvent = new AutoResetEvent(false);

    public static void Main()
    {
        Task.Run
        (
            () =>
            {
                Thread.Sleep(3000);

                // Calling Set() is signaling the event. That is, 
                // you open the door. And other threads waiting until
                // the door is opened get resumed.
                ResetEvent.Set();
            }
        );

        // Since the ResetEvent isn't signaled, the door is closed
        // and it'll wait until the door gets opened (i.e. the event
        // gets signaled).
        ResetEvent.WaitOne();
        Console.WriteLine("This will be written once the ResetEvent has been signaled in the other thread");
    }
}

现在你可以将这种方法应用到你的身上,你就可以得到你想要的东西。