I need to pass an event from one form to another. Event - the end of the voice recording (it is of the void type). In Program.cs I create a class.

static class SRec { public static void Val {get; set;} } 

In the form where my recording ends, I write

 SRec.Val = wavein.StopRecording(); 

Gives the error: " Cannot implicity convert type" void "to" void " "

I need this in order to form two in if it was written, as soon as the recording is over, then start the timer. Something like that:

 if(SRec.Val = wavein.StopRecording()) { timer1.Enabled = true; } 

There are no events here, but it is advised to do it through events. How to do it? How to transfer the end of the record to another form? Thank!

  • wavein is what? is it an object from naudio? if so, then it has a termination event. - Stack

2 answers 2

as soon as the recording is over, then start the timer.

If wavein is an object from NAudio, then there is a record completion event. In the event handler for this event, you can start a timer.

 var w = new NAudio.Wave.WaveIn(); w.RecordingStopped += (s, e) => { // тут код для запуска таймера ... }; w.StartRecording(); 

If you need to open the second form, show something / do something in it and, after completion, notify the first form, then you must define the event for which the first form is signed in the second form.
Below is an example. There is a button on Form1. If you click it, it opens Form2, in which something is done in a separate thread. Upon completion, an event is sent.

 class Program { [STAThread] static void Main(string[] args) { Application.Run(new Form1()); } class Form1 : Form { public Form1() { new Button() { Parent = this, Text = "Start" } .Click += (s, e) => { var f2 = new Form2(); f2.Stopped += delegate { f2.Close(); }; f2.Start(); }; } } class Form2 : Form { public event EventHandler Stopped = delegate { }; public void Start() { Task.Factory.StartNew(() => { Task.Delay(1000).Wait(); // долго что-то делается }) .ContinueWith(t => { Stopped(this, EventArgs.Empty); // посылаем событие } , TaskScheduler.FromCurrentSynchronizationContext()); this.Show(); } } } 
  • Interesting. I will try now - Alexander

Probably, forms about each other do not need to know anything - this time. In the spirit of MVP, it will be something like this: 1 form implements the interface with the StopRecord event and, accordingly, the Presenter (or you somewhere in Program.cs) have a subscription for this event. 2 form has some method (also through the interface, probably) of type StartTimer . Well, in Presenter-e (read in Program.cs for you), the event handler for the StopRecord event of the first form will pull StartTimer second form. Something like this.