开发者

how to execute a method specific to a function in a different class from the main form class? (c#)

May be my lack of awareness of some key programming concept on polymorphism or interface inheritance etc. this should be easy. I'm a novice.

I have a separate class in C# program called tts. Now I defined a function in it called speak. here is it:

class tts

{ 

     public void speak() 
     {

        Process process1 = new Process();
        process1.StartInfo.FileName = "C:\\Program Files\\eSpeak\\command_line\\espeak.exe";
        process1.StartInfo.Arguments = "hello";

        process1.StartInfo.UseShellExecute = false;
        process1.StartInfo.CreateNoWindow = true;

  开发者_JAVA百科      process1.Start();
        //process1.Kill();
    }
}

When I call this function from main form under an event (listBox1_SelectedIndexChange), it runs fine. What this program does is read "hello" every time I scroll through my list box. But I want to kill process1 when I scroll to next item in listbox. when I implement the process1.Kill() (by uncommenting the kill method) in speak function, the program doesn't read "hello". What happens is that the process1 get killed immediately. So I would not implement method Kill() there under the speak function. But I want to kill process1 from main form when I change index of listbox (listBox1_SelectedIndexChange). Here is my main form code.

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        tts obj = new tts();
        obj.speak();
    }

What I want to do is this:

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        tts obj = new tts();
        obj.speak();
        process1.Kill();
    }

which of course wont work since process1 is under the scope of function speak in class tts. how to execute process1.Kill() from main form?

I would appreciate the basic code itself more than technical terms on what to do.


One thing you can do is create seprate class to manage process

here is example code how it look like :

class processmanagement
{
  Process p;

  public Process startprocess()
  {
    p = new Process();
  }

  public void killprocess(Process p1)
  {
    p = p1;
    if(p!=null)
      p.kill();
  }
}

or

class processmanagement
{
  Process p;

  public void startprocess()
  {
    p = new Process();
  }

  public void killprocess()
  {
    if(p!=null)
      p.kill();
  }
}


Make process1 an instance variable of your tts class and implement a method stopspeak there, like:

class tts {
  Process process1;
  public void speak() { .... }
  public void stopspeak() {
    if (process1 != null)
      process1.Kill();
    process1 = null;
  }
}

Additionally you should similarily keep track of your current tts class in you main window so that you can write

private tts obj;

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (obj != null) {
      obj.stopspeak();
      obj = null;
    }
    obj = new tts();
    obj.speak();
}


Your Speak method could return the process it's started:

public Process Speak()
{
    // Code as before
    return process1;
}

Then you can write:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    TextToSpeech obj = new TextToSpeech();
    Process process = obj.Speak();
    process.Kill();
}

If you want to actually start the process in one event and kill it in another, you'll probably want an instance variable in your form which maintains the last-started process:

private Process speechProcess;

private void StartButtonClicked(object sender, EventArgs e)
{
    // TODO: consider what you want to happen if there's already
    // a process running
    TextToSpeech obj = new TextToSpeech();
    speechProcess = obj.Speak();
}

private void StopButtonClicked(object sender, EventArgs e)
{
    // Possibly change the UI so that the button will be disabled
    // when there's no process running
    if (speechProcess == null)
    {
        return;
    }
    speechProcess.Kill();
    speechProcess = null;
}

EDIT: As noted in comments, this ties the TextToSpeech class to the idea of using a process. It would be provide better encapsulation if you made TextToSpeech responsible for the process management, so you could ask that to start and stop appropriately. Then you'd change the speechProcess variable above from a Process type variable to TextToSpeech.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜