Form window not opening when I run the program because of a line in c#











up vote
-1
down vote

favorite












I wrote this code in c# and the issue here is that when I run the program the form window will not show up because of this line: RecognitionResult result = recEngine.Recognize();
But when I comment it out the code runs I also commented out the switch cases because they use the e.result..
My main goal with this code is to change the Grammar whenever I say the keyword so do you have a better way solving this ?
Thank you!



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Recognition;
using System.Speech.Synthesis;

namespace Lil_AI
{
public partial class Form1 : Form
{
SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
SpeechSynthesizer spekEngine = new SpeechSynthesizer();
public enum ProcessState
{
on,
command,
off
}

public ProcessState current_state = ProcessState.off;

public Form1()
{
InitializeComponent();
}

private void btnEnable_Click(object sender, EventArgs e)
{
recEngine.RecognizeAsync(RecognizeMode.Multiple);
btnDisable.Enabled = true;
}

private void Form1_Load(object sender, EventArgs e)
{
recEngine.SetInputToDefaultAudioDevice();
spekEngine.SetOutputToDefaultAudioDevice();
//RecognitionResult result = recEngine.Recognize();

Choices oncommands = new Choices();
oncommands.Add(new string { "Please", "Bye" });
GrammarBuilder ongBuilder = new GrammarBuilder();
ongBuilder.Append(oncommands);
Grammar ongrammar = new Grammar(ongBuilder);

Choices commands = new Choices();
commands.Add(new string { "Turn on the lights", "Bye" });
GrammarBuilder gBuilder = new GrammarBuilder();
gBuilder.Append(commands);
Grammar grammar = new Grammar(gBuilder);

Choices offcommands = new Choices();
offcommands.Add(new string { "Hello", "Bye" });
GrammarBuilder offgBuilder = new GrammarBuilder();
offgBuilder.Append(offcommands);
Grammar offgrammar = new Grammar(offgBuilder);

// recEngine.LoadGrammar(offgrammar);

if (current_state == ProcessState.on)
{
recEngine.LoadGrammarAsync(ongrammar);
spekEngine.Speak("I'm waiting for your commands");
/* switch (result.Text)
{
case "Please":
current_state = ProcessState.command;
break;
case "Bye":
spekEngine.Speak("Bye, bye");
current_state = ProcessState.off;
break;
}*/
}
if (current_state == ProcessState.command)
{
recEngine.LoadGrammarAsync(grammar);
/* switch (result.Text)
{
case "Turn the lights on":
spekEngine.Speak("Of course");
break;
case "Bye":
spekEngine.Speak("Bye, bye");
current_state = ProcessState.off;
break;
}*/
}
if (current_state == ProcessState.off)
{
recEngine.LoadGrammarAsync(offgrammar);
/* switch (result.Text)
{
case "Hello":
spekEngine.Speak("Hi there");
current_state = ProcessState.on;
break;
}*/
}
}

private void btnDisable_Click(object sender, EventArgs e)
{
recEngine.RecognizeAsyncStop();
btnDisable.Enabled = false;
}
}
}









share|improve this question






















  • Try handling exceptions and see what the error is
    – Crowcoder
    Nov 9 at 10:31










  • Don't use a blocking call like Recognize(), use RecognizeAsync() instead. And subscribe the SpeechDetected event to know when it recognized something.
    – Hans Passant
    Nov 9 at 13:07










  • @Crowcoder There is no exception the program runs bur without the Form window... any other idea ?
    – Áron Tóth
    Nov 10 at 21:40










  • I managed to get the exception by placing that line to the btnEnable_Click method: System.InvalidOperationException: 'Cannot perform this operation while the recognizer is doing recognition.' This is obviously because I already started the RecognizeAsync() so Instead of forcing this I will find out another solution.
    – Áron Tóth
    Nov 10 at 22:48















up vote
-1
down vote

favorite












I wrote this code in c# and the issue here is that when I run the program the form window will not show up because of this line: RecognitionResult result = recEngine.Recognize();
But when I comment it out the code runs I also commented out the switch cases because they use the e.result..
My main goal with this code is to change the Grammar whenever I say the keyword so do you have a better way solving this ?
Thank you!



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Recognition;
using System.Speech.Synthesis;

namespace Lil_AI
{
public partial class Form1 : Form
{
SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
SpeechSynthesizer spekEngine = new SpeechSynthesizer();
public enum ProcessState
{
on,
command,
off
}

public ProcessState current_state = ProcessState.off;

public Form1()
{
InitializeComponent();
}

private void btnEnable_Click(object sender, EventArgs e)
{
recEngine.RecognizeAsync(RecognizeMode.Multiple);
btnDisable.Enabled = true;
}

private void Form1_Load(object sender, EventArgs e)
{
recEngine.SetInputToDefaultAudioDevice();
spekEngine.SetOutputToDefaultAudioDevice();
//RecognitionResult result = recEngine.Recognize();

Choices oncommands = new Choices();
oncommands.Add(new string { "Please", "Bye" });
GrammarBuilder ongBuilder = new GrammarBuilder();
ongBuilder.Append(oncommands);
Grammar ongrammar = new Grammar(ongBuilder);

Choices commands = new Choices();
commands.Add(new string { "Turn on the lights", "Bye" });
GrammarBuilder gBuilder = new GrammarBuilder();
gBuilder.Append(commands);
Grammar grammar = new Grammar(gBuilder);

Choices offcommands = new Choices();
offcommands.Add(new string { "Hello", "Bye" });
GrammarBuilder offgBuilder = new GrammarBuilder();
offgBuilder.Append(offcommands);
Grammar offgrammar = new Grammar(offgBuilder);

// recEngine.LoadGrammar(offgrammar);

if (current_state == ProcessState.on)
{
recEngine.LoadGrammarAsync(ongrammar);
spekEngine.Speak("I'm waiting for your commands");
/* switch (result.Text)
{
case "Please":
current_state = ProcessState.command;
break;
case "Bye":
spekEngine.Speak("Bye, bye");
current_state = ProcessState.off;
break;
}*/
}
if (current_state == ProcessState.command)
{
recEngine.LoadGrammarAsync(grammar);
/* switch (result.Text)
{
case "Turn the lights on":
spekEngine.Speak("Of course");
break;
case "Bye":
spekEngine.Speak("Bye, bye");
current_state = ProcessState.off;
break;
}*/
}
if (current_state == ProcessState.off)
{
recEngine.LoadGrammarAsync(offgrammar);
/* switch (result.Text)
{
case "Hello":
spekEngine.Speak("Hi there");
current_state = ProcessState.on;
break;
}*/
}
}

private void btnDisable_Click(object sender, EventArgs e)
{
recEngine.RecognizeAsyncStop();
btnDisable.Enabled = false;
}
}
}









share|improve this question






















  • Try handling exceptions and see what the error is
    – Crowcoder
    Nov 9 at 10:31










  • Don't use a blocking call like Recognize(), use RecognizeAsync() instead. And subscribe the SpeechDetected event to know when it recognized something.
    – Hans Passant
    Nov 9 at 13:07










  • @Crowcoder There is no exception the program runs bur without the Form window... any other idea ?
    – Áron Tóth
    Nov 10 at 21:40










  • I managed to get the exception by placing that line to the btnEnable_Click method: System.InvalidOperationException: 'Cannot perform this operation while the recognizer is doing recognition.' This is obviously because I already started the RecognizeAsync() so Instead of forcing this I will find out another solution.
    – Áron Tóth
    Nov 10 at 22:48













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I wrote this code in c# and the issue here is that when I run the program the form window will not show up because of this line: RecognitionResult result = recEngine.Recognize();
But when I comment it out the code runs I also commented out the switch cases because they use the e.result..
My main goal with this code is to change the Grammar whenever I say the keyword so do you have a better way solving this ?
Thank you!



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Recognition;
using System.Speech.Synthesis;

namespace Lil_AI
{
public partial class Form1 : Form
{
SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
SpeechSynthesizer spekEngine = new SpeechSynthesizer();
public enum ProcessState
{
on,
command,
off
}

public ProcessState current_state = ProcessState.off;

public Form1()
{
InitializeComponent();
}

private void btnEnable_Click(object sender, EventArgs e)
{
recEngine.RecognizeAsync(RecognizeMode.Multiple);
btnDisable.Enabled = true;
}

private void Form1_Load(object sender, EventArgs e)
{
recEngine.SetInputToDefaultAudioDevice();
spekEngine.SetOutputToDefaultAudioDevice();
//RecognitionResult result = recEngine.Recognize();

Choices oncommands = new Choices();
oncommands.Add(new string { "Please", "Bye" });
GrammarBuilder ongBuilder = new GrammarBuilder();
ongBuilder.Append(oncommands);
Grammar ongrammar = new Grammar(ongBuilder);

Choices commands = new Choices();
commands.Add(new string { "Turn on the lights", "Bye" });
GrammarBuilder gBuilder = new GrammarBuilder();
gBuilder.Append(commands);
Grammar grammar = new Grammar(gBuilder);

Choices offcommands = new Choices();
offcommands.Add(new string { "Hello", "Bye" });
GrammarBuilder offgBuilder = new GrammarBuilder();
offgBuilder.Append(offcommands);
Grammar offgrammar = new Grammar(offgBuilder);

// recEngine.LoadGrammar(offgrammar);

if (current_state == ProcessState.on)
{
recEngine.LoadGrammarAsync(ongrammar);
spekEngine.Speak("I'm waiting for your commands");
/* switch (result.Text)
{
case "Please":
current_state = ProcessState.command;
break;
case "Bye":
spekEngine.Speak("Bye, bye");
current_state = ProcessState.off;
break;
}*/
}
if (current_state == ProcessState.command)
{
recEngine.LoadGrammarAsync(grammar);
/* switch (result.Text)
{
case "Turn the lights on":
spekEngine.Speak("Of course");
break;
case "Bye":
spekEngine.Speak("Bye, bye");
current_state = ProcessState.off;
break;
}*/
}
if (current_state == ProcessState.off)
{
recEngine.LoadGrammarAsync(offgrammar);
/* switch (result.Text)
{
case "Hello":
spekEngine.Speak("Hi there");
current_state = ProcessState.on;
break;
}*/
}
}

private void btnDisable_Click(object sender, EventArgs e)
{
recEngine.RecognizeAsyncStop();
btnDisable.Enabled = false;
}
}
}









share|improve this question













I wrote this code in c# and the issue here is that when I run the program the form window will not show up because of this line: RecognitionResult result = recEngine.Recognize();
But when I comment it out the code runs I also commented out the switch cases because they use the e.result..
My main goal with this code is to change the Grammar whenever I say the keyword so do you have a better way solving this ?
Thank you!



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Recognition;
using System.Speech.Synthesis;

namespace Lil_AI
{
public partial class Form1 : Form
{
SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
SpeechSynthesizer spekEngine = new SpeechSynthesizer();
public enum ProcessState
{
on,
command,
off
}

public ProcessState current_state = ProcessState.off;

public Form1()
{
InitializeComponent();
}

private void btnEnable_Click(object sender, EventArgs e)
{
recEngine.RecognizeAsync(RecognizeMode.Multiple);
btnDisable.Enabled = true;
}

private void Form1_Load(object sender, EventArgs e)
{
recEngine.SetInputToDefaultAudioDevice();
spekEngine.SetOutputToDefaultAudioDevice();
//RecognitionResult result = recEngine.Recognize();

Choices oncommands = new Choices();
oncommands.Add(new string { "Please", "Bye" });
GrammarBuilder ongBuilder = new GrammarBuilder();
ongBuilder.Append(oncommands);
Grammar ongrammar = new Grammar(ongBuilder);

Choices commands = new Choices();
commands.Add(new string { "Turn on the lights", "Bye" });
GrammarBuilder gBuilder = new GrammarBuilder();
gBuilder.Append(commands);
Grammar grammar = new Grammar(gBuilder);

Choices offcommands = new Choices();
offcommands.Add(new string { "Hello", "Bye" });
GrammarBuilder offgBuilder = new GrammarBuilder();
offgBuilder.Append(offcommands);
Grammar offgrammar = new Grammar(offgBuilder);

// recEngine.LoadGrammar(offgrammar);

if (current_state == ProcessState.on)
{
recEngine.LoadGrammarAsync(ongrammar);
spekEngine.Speak("I'm waiting for your commands");
/* switch (result.Text)
{
case "Please":
current_state = ProcessState.command;
break;
case "Bye":
spekEngine.Speak("Bye, bye");
current_state = ProcessState.off;
break;
}*/
}
if (current_state == ProcessState.command)
{
recEngine.LoadGrammarAsync(grammar);
/* switch (result.Text)
{
case "Turn the lights on":
spekEngine.Speak("Of course");
break;
case "Bye":
spekEngine.Speak("Bye, bye");
current_state = ProcessState.off;
break;
}*/
}
if (current_state == ProcessState.off)
{
recEngine.LoadGrammarAsync(offgrammar);
/* switch (result.Text)
{
case "Hello":
spekEngine.Speak("Hi there");
current_state = ProcessState.on;
break;
}*/
}
}

private void btnDisable_Click(object sender, EventArgs e)
{
recEngine.RecognizeAsyncStop();
btnDisable.Enabled = false;
}
}
}






c# visual-studio speech-recognition state-machines speech






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 10:14









Áron Tóth

11




11












  • Try handling exceptions and see what the error is
    – Crowcoder
    Nov 9 at 10:31










  • Don't use a blocking call like Recognize(), use RecognizeAsync() instead. And subscribe the SpeechDetected event to know when it recognized something.
    – Hans Passant
    Nov 9 at 13:07










  • @Crowcoder There is no exception the program runs bur without the Form window... any other idea ?
    – Áron Tóth
    Nov 10 at 21:40










  • I managed to get the exception by placing that line to the btnEnable_Click method: System.InvalidOperationException: 'Cannot perform this operation while the recognizer is doing recognition.' This is obviously because I already started the RecognizeAsync() so Instead of forcing this I will find out another solution.
    – Áron Tóth
    Nov 10 at 22:48


















  • Try handling exceptions and see what the error is
    – Crowcoder
    Nov 9 at 10:31










  • Don't use a blocking call like Recognize(), use RecognizeAsync() instead. And subscribe the SpeechDetected event to know when it recognized something.
    – Hans Passant
    Nov 9 at 13:07










  • @Crowcoder There is no exception the program runs bur without the Form window... any other idea ?
    – Áron Tóth
    Nov 10 at 21:40










  • I managed to get the exception by placing that line to the btnEnable_Click method: System.InvalidOperationException: 'Cannot perform this operation while the recognizer is doing recognition.' This is obviously because I already started the RecognizeAsync() so Instead of forcing this I will find out another solution.
    – Áron Tóth
    Nov 10 at 22:48
















Try handling exceptions and see what the error is
– Crowcoder
Nov 9 at 10:31




Try handling exceptions and see what the error is
– Crowcoder
Nov 9 at 10:31












Don't use a blocking call like Recognize(), use RecognizeAsync() instead. And subscribe the SpeechDetected event to know when it recognized something.
– Hans Passant
Nov 9 at 13:07




Don't use a blocking call like Recognize(), use RecognizeAsync() instead. And subscribe the SpeechDetected event to know when it recognized something.
– Hans Passant
Nov 9 at 13:07












@Crowcoder There is no exception the program runs bur without the Form window... any other idea ?
– Áron Tóth
Nov 10 at 21:40




@Crowcoder There is no exception the program runs bur without the Form window... any other idea ?
– Áron Tóth
Nov 10 at 21:40












I managed to get the exception by placing that line to the btnEnable_Click method: System.InvalidOperationException: 'Cannot perform this operation while the recognizer is doing recognition.' This is obviously because I already started the RecognizeAsync() so Instead of forcing this I will find out another solution.
– Áron Tóth
Nov 10 at 22:48




I managed to get the exception by placing that line to the btnEnable_Click method: System.InvalidOperationException: 'Cannot perform this operation while the recognizer is doing recognition.' This is obviously because I already started the RecognizeAsync() so Instead of forcing this I will find out another solution.
– Áron Tóth
Nov 10 at 22:48












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










@Crowcoder, @Hans Passant thank you for the help. I managed to solve the issue by getting the grammars from a class.



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Recognition;
using System.Speech.Synthesis;

namespace AI_Graph
{

public partial class Form1 : Form
{
public SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
public SpeechSynthesizer spekEngine = new SpeechSynthesizer();

public class DemoCode
{
public Grammar on_Grammar()
{
Choices oncommands = new Choices();
oncommands.Add(new string { "Please", "Bye" });
GrammarBuilder ongBuilder = new GrammarBuilder();
ongBuilder.Append(oncommands);
Grammar ongrammar = new Grammar(ongBuilder);
ongrammar.Name = "Standby";

return ongrammar;
}

public Grammar command_Grammar()
{
Choices commands = new Choices();
commands.Add(new string { "Turn on the lights", "Bye" });
GrammarBuilder gBuilder = new GrammarBuilder();
gBuilder.Append(commands);
Grammar grammar = new Grammar(gBuilder);
grammar.Name = "ON";

return grammar;
}
public Grammar off_Grammar()
{
Choices offcommands = new Choices();
offcommands.Add(new string { "Hello", "Bye", "Good night", "Good morning" });
GrammarBuilder offgBuilder = new GrammarBuilder();
offgBuilder.Append(offcommands);
Grammar offgrammar = new Grammar(offgBuilder);
offgrammar.Name = "OFF";

return offgrammar;
}

}

public DemoCode _Grammar = new DemoCode();

public enum ProcessState
{
on,
command,
off
}

public ProcessState current_state = ProcessState.off;

public Form1()
{
InitializeComponent();
}

public void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
here:
do
{
if (current_state == ProcessState.on)
{
recEngine.LoadGrammarAsync(_Grammar.on_Grammar());
spekEngine.Speak("I'm waiting for your commands");
switch (e.Result.Text)
{
case "Please":
current_state = ProcessState.command;
richTextBox1.Text += e.Result.Grammar.Name;
break;
case "Bye":
spekEngine.Speak("Bye, bye");
current_state = ProcessState.off;
richTextBox1.Text += e.Result.Grammar.Name;
break;
}
}
if (current_state == ProcessState.command)
{
recEngine.LoadGrammarAsync(_Grammar.command_Grammar());
switch (e.Result.Text)
{
case "Turn the lights on":
spekEngine.Speak("Of course");
current_state = ProcessState.on;
richTextBox1.Text += e.Result.Grammar.Name;
break;
case "Bye":
spekEngine.Speak("Bye, bye");
current_state = ProcessState.off;
richTextBox1.Text += e.Result.Grammar.Name;
break;
}
}
if (current_state == ProcessState.off)
{
recEngine.LoadGrammarAsync(_Grammar.off_Grammar());
switch (e.Result.Text)
{
case "Hello":
spekEngine.Speak("Hi there");
current_state = ProcessState.on;
richTextBox1.Text += e.Result.Grammar.Name;
break;
case "Bye":
spekEngine.Speak("Bye, bye");
current_state = ProcessState.off;
richTextBox1.Text += e.Result.Grammar.Name;
break;
}
}
} while (e.Result.Text != "Good night");

if (e.Result.Text == "Good morning")
{
goto here;
}
}


public void recEngine_SpeechDetected(object sender, SpeechDetectedEventArgs e)
{
richTextBox1.Text += " Speech detected at AudioPosition = " + e.AudioPosition;
}

private void Form1_Load(object sender, EventArgs e)
{
recEngine.LoadGrammarAsync(_Grammar.off_Grammar());
recEngine.SetInputToDefaultAudioDevice();
spekEngine.SetOutputToDefaultAudioDevice();
recEngine.SpeechRecognized += recEngine_SpeechRecognized;
}

private void btnEnable_Click(object sender, EventArgs e)
{
recEngine.RecognizeAsync(RecognizeMode.Multiple);
btnDisable.Enabled = true;
}

private void btnDisable_Click(object sender, EventArgs e)
{
recEngine.RecognizeAsyncStop();
btnDisable.Enabled = false;
}
}
}





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53223758%2fform-window-not-opening-when-i-run-the-program-because-of-a-line-in-c-sharp%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    @Crowcoder, @Hans Passant thank you for the help. I managed to solve the issue by getting the grammars from a class.



    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Speech.Recognition;
    using System.Speech.Synthesis;

    namespace AI_Graph
    {

    public partial class Form1 : Form
    {
    public SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
    public SpeechSynthesizer spekEngine = new SpeechSynthesizer();

    public class DemoCode
    {
    public Grammar on_Grammar()
    {
    Choices oncommands = new Choices();
    oncommands.Add(new string { "Please", "Bye" });
    GrammarBuilder ongBuilder = new GrammarBuilder();
    ongBuilder.Append(oncommands);
    Grammar ongrammar = new Grammar(ongBuilder);
    ongrammar.Name = "Standby";

    return ongrammar;
    }

    public Grammar command_Grammar()
    {
    Choices commands = new Choices();
    commands.Add(new string { "Turn on the lights", "Bye" });
    GrammarBuilder gBuilder = new GrammarBuilder();
    gBuilder.Append(commands);
    Grammar grammar = new Grammar(gBuilder);
    grammar.Name = "ON";

    return grammar;
    }
    public Grammar off_Grammar()
    {
    Choices offcommands = new Choices();
    offcommands.Add(new string { "Hello", "Bye", "Good night", "Good morning" });
    GrammarBuilder offgBuilder = new GrammarBuilder();
    offgBuilder.Append(offcommands);
    Grammar offgrammar = new Grammar(offgBuilder);
    offgrammar.Name = "OFF";

    return offgrammar;
    }

    }

    public DemoCode _Grammar = new DemoCode();

    public enum ProcessState
    {
    on,
    command,
    off
    }

    public ProcessState current_state = ProcessState.off;

    public Form1()
    {
    InitializeComponent();
    }

    public void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
    here:
    do
    {
    if (current_state == ProcessState.on)
    {
    recEngine.LoadGrammarAsync(_Grammar.on_Grammar());
    spekEngine.Speak("I'm waiting for your commands");
    switch (e.Result.Text)
    {
    case "Please":
    current_state = ProcessState.command;
    richTextBox1.Text += e.Result.Grammar.Name;
    break;
    case "Bye":
    spekEngine.Speak("Bye, bye");
    current_state = ProcessState.off;
    richTextBox1.Text += e.Result.Grammar.Name;
    break;
    }
    }
    if (current_state == ProcessState.command)
    {
    recEngine.LoadGrammarAsync(_Grammar.command_Grammar());
    switch (e.Result.Text)
    {
    case "Turn the lights on":
    spekEngine.Speak("Of course");
    current_state = ProcessState.on;
    richTextBox1.Text += e.Result.Grammar.Name;
    break;
    case "Bye":
    spekEngine.Speak("Bye, bye");
    current_state = ProcessState.off;
    richTextBox1.Text += e.Result.Grammar.Name;
    break;
    }
    }
    if (current_state == ProcessState.off)
    {
    recEngine.LoadGrammarAsync(_Grammar.off_Grammar());
    switch (e.Result.Text)
    {
    case "Hello":
    spekEngine.Speak("Hi there");
    current_state = ProcessState.on;
    richTextBox1.Text += e.Result.Grammar.Name;
    break;
    case "Bye":
    spekEngine.Speak("Bye, bye");
    current_state = ProcessState.off;
    richTextBox1.Text += e.Result.Grammar.Name;
    break;
    }
    }
    } while (e.Result.Text != "Good night");

    if (e.Result.Text == "Good morning")
    {
    goto here;
    }
    }


    public void recEngine_SpeechDetected(object sender, SpeechDetectedEventArgs e)
    {
    richTextBox1.Text += " Speech detected at AudioPosition = " + e.AudioPosition;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    recEngine.LoadGrammarAsync(_Grammar.off_Grammar());
    recEngine.SetInputToDefaultAudioDevice();
    spekEngine.SetOutputToDefaultAudioDevice();
    recEngine.SpeechRecognized += recEngine_SpeechRecognized;
    }

    private void btnEnable_Click(object sender, EventArgs e)
    {
    recEngine.RecognizeAsync(RecognizeMode.Multiple);
    btnDisable.Enabled = true;
    }

    private void btnDisable_Click(object sender, EventArgs e)
    {
    recEngine.RecognizeAsyncStop();
    btnDisable.Enabled = false;
    }
    }
    }





    share|improve this answer

























      up vote
      0
      down vote



      accepted










      @Crowcoder, @Hans Passant thank you for the help. I managed to solve the issue by getting the grammars from a class.



      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Windows.Forms;
      using System.Speech.Recognition;
      using System.Speech.Synthesis;

      namespace AI_Graph
      {

      public partial class Form1 : Form
      {
      public SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
      public SpeechSynthesizer spekEngine = new SpeechSynthesizer();

      public class DemoCode
      {
      public Grammar on_Grammar()
      {
      Choices oncommands = new Choices();
      oncommands.Add(new string { "Please", "Bye" });
      GrammarBuilder ongBuilder = new GrammarBuilder();
      ongBuilder.Append(oncommands);
      Grammar ongrammar = new Grammar(ongBuilder);
      ongrammar.Name = "Standby";

      return ongrammar;
      }

      public Grammar command_Grammar()
      {
      Choices commands = new Choices();
      commands.Add(new string { "Turn on the lights", "Bye" });
      GrammarBuilder gBuilder = new GrammarBuilder();
      gBuilder.Append(commands);
      Grammar grammar = new Grammar(gBuilder);
      grammar.Name = "ON";

      return grammar;
      }
      public Grammar off_Grammar()
      {
      Choices offcommands = new Choices();
      offcommands.Add(new string { "Hello", "Bye", "Good night", "Good morning" });
      GrammarBuilder offgBuilder = new GrammarBuilder();
      offgBuilder.Append(offcommands);
      Grammar offgrammar = new Grammar(offgBuilder);
      offgrammar.Name = "OFF";

      return offgrammar;
      }

      }

      public DemoCode _Grammar = new DemoCode();

      public enum ProcessState
      {
      on,
      command,
      off
      }

      public ProcessState current_state = ProcessState.off;

      public Form1()
      {
      InitializeComponent();
      }

      public void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
      {
      here:
      do
      {
      if (current_state == ProcessState.on)
      {
      recEngine.LoadGrammarAsync(_Grammar.on_Grammar());
      spekEngine.Speak("I'm waiting for your commands");
      switch (e.Result.Text)
      {
      case "Please":
      current_state = ProcessState.command;
      richTextBox1.Text += e.Result.Grammar.Name;
      break;
      case "Bye":
      spekEngine.Speak("Bye, bye");
      current_state = ProcessState.off;
      richTextBox1.Text += e.Result.Grammar.Name;
      break;
      }
      }
      if (current_state == ProcessState.command)
      {
      recEngine.LoadGrammarAsync(_Grammar.command_Grammar());
      switch (e.Result.Text)
      {
      case "Turn the lights on":
      spekEngine.Speak("Of course");
      current_state = ProcessState.on;
      richTextBox1.Text += e.Result.Grammar.Name;
      break;
      case "Bye":
      spekEngine.Speak("Bye, bye");
      current_state = ProcessState.off;
      richTextBox1.Text += e.Result.Grammar.Name;
      break;
      }
      }
      if (current_state == ProcessState.off)
      {
      recEngine.LoadGrammarAsync(_Grammar.off_Grammar());
      switch (e.Result.Text)
      {
      case "Hello":
      spekEngine.Speak("Hi there");
      current_state = ProcessState.on;
      richTextBox1.Text += e.Result.Grammar.Name;
      break;
      case "Bye":
      spekEngine.Speak("Bye, bye");
      current_state = ProcessState.off;
      richTextBox1.Text += e.Result.Grammar.Name;
      break;
      }
      }
      } while (e.Result.Text != "Good night");

      if (e.Result.Text == "Good morning")
      {
      goto here;
      }
      }


      public void recEngine_SpeechDetected(object sender, SpeechDetectedEventArgs e)
      {
      richTextBox1.Text += " Speech detected at AudioPosition = " + e.AudioPosition;
      }

      private void Form1_Load(object sender, EventArgs e)
      {
      recEngine.LoadGrammarAsync(_Grammar.off_Grammar());
      recEngine.SetInputToDefaultAudioDevice();
      spekEngine.SetOutputToDefaultAudioDevice();
      recEngine.SpeechRecognized += recEngine_SpeechRecognized;
      }

      private void btnEnable_Click(object sender, EventArgs e)
      {
      recEngine.RecognizeAsync(RecognizeMode.Multiple);
      btnDisable.Enabled = true;
      }

      private void btnDisable_Click(object sender, EventArgs e)
      {
      recEngine.RecognizeAsyncStop();
      btnDisable.Enabled = false;
      }
      }
      }





      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        @Crowcoder, @Hans Passant thank you for the help. I managed to solve the issue by getting the grammars from a class.



        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;
        using System.Drawing;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;
        using System.Windows.Forms;
        using System.Speech.Recognition;
        using System.Speech.Synthesis;

        namespace AI_Graph
        {

        public partial class Form1 : Form
        {
        public SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
        public SpeechSynthesizer spekEngine = new SpeechSynthesizer();

        public class DemoCode
        {
        public Grammar on_Grammar()
        {
        Choices oncommands = new Choices();
        oncommands.Add(new string { "Please", "Bye" });
        GrammarBuilder ongBuilder = new GrammarBuilder();
        ongBuilder.Append(oncommands);
        Grammar ongrammar = new Grammar(ongBuilder);
        ongrammar.Name = "Standby";

        return ongrammar;
        }

        public Grammar command_Grammar()
        {
        Choices commands = new Choices();
        commands.Add(new string { "Turn on the lights", "Bye" });
        GrammarBuilder gBuilder = new GrammarBuilder();
        gBuilder.Append(commands);
        Grammar grammar = new Grammar(gBuilder);
        grammar.Name = "ON";

        return grammar;
        }
        public Grammar off_Grammar()
        {
        Choices offcommands = new Choices();
        offcommands.Add(new string { "Hello", "Bye", "Good night", "Good morning" });
        GrammarBuilder offgBuilder = new GrammarBuilder();
        offgBuilder.Append(offcommands);
        Grammar offgrammar = new Grammar(offgBuilder);
        offgrammar.Name = "OFF";

        return offgrammar;
        }

        }

        public DemoCode _Grammar = new DemoCode();

        public enum ProcessState
        {
        on,
        command,
        off
        }

        public ProcessState current_state = ProcessState.off;

        public Form1()
        {
        InitializeComponent();
        }

        public void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
        here:
        do
        {
        if (current_state == ProcessState.on)
        {
        recEngine.LoadGrammarAsync(_Grammar.on_Grammar());
        spekEngine.Speak("I'm waiting for your commands");
        switch (e.Result.Text)
        {
        case "Please":
        current_state = ProcessState.command;
        richTextBox1.Text += e.Result.Grammar.Name;
        break;
        case "Bye":
        spekEngine.Speak("Bye, bye");
        current_state = ProcessState.off;
        richTextBox1.Text += e.Result.Grammar.Name;
        break;
        }
        }
        if (current_state == ProcessState.command)
        {
        recEngine.LoadGrammarAsync(_Grammar.command_Grammar());
        switch (e.Result.Text)
        {
        case "Turn the lights on":
        spekEngine.Speak("Of course");
        current_state = ProcessState.on;
        richTextBox1.Text += e.Result.Grammar.Name;
        break;
        case "Bye":
        spekEngine.Speak("Bye, bye");
        current_state = ProcessState.off;
        richTextBox1.Text += e.Result.Grammar.Name;
        break;
        }
        }
        if (current_state == ProcessState.off)
        {
        recEngine.LoadGrammarAsync(_Grammar.off_Grammar());
        switch (e.Result.Text)
        {
        case "Hello":
        spekEngine.Speak("Hi there");
        current_state = ProcessState.on;
        richTextBox1.Text += e.Result.Grammar.Name;
        break;
        case "Bye":
        spekEngine.Speak("Bye, bye");
        current_state = ProcessState.off;
        richTextBox1.Text += e.Result.Grammar.Name;
        break;
        }
        }
        } while (e.Result.Text != "Good night");

        if (e.Result.Text == "Good morning")
        {
        goto here;
        }
        }


        public void recEngine_SpeechDetected(object sender, SpeechDetectedEventArgs e)
        {
        richTextBox1.Text += " Speech detected at AudioPosition = " + e.AudioPosition;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        recEngine.LoadGrammarAsync(_Grammar.off_Grammar());
        recEngine.SetInputToDefaultAudioDevice();
        spekEngine.SetOutputToDefaultAudioDevice();
        recEngine.SpeechRecognized += recEngine_SpeechRecognized;
        }

        private void btnEnable_Click(object sender, EventArgs e)
        {
        recEngine.RecognizeAsync(RecognizeMode.Multiple);
        btnDisable.Enabled = true;
        }

        private void btnDisable_Click(object sender, EventArgs e)
        {
        recEngine.RecognizeAsyncStop();
        btnDisable.Enabled = false;
        }
        }
        }





        share|improve this answer












        @Crowcoder, @Hans Passant thank you for the help. I managed to solve the issue by getting the grammars from a class.



        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;
        using System.Drawing;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;
        using System.Windows.Forms;
        using System.Speech.Recognition;
        using System.Speech.Synthesis;

        namespace AI_Graph
        {

        public partial class Form1 : Form
        {
        public SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
        public SpeechSynthesizer spekEngine = new SpeechSynthesizer();

        public class DemoCode
        {
        public Grammar on_Grammar()
        {
        Choices oncommands = new Choices();
        oncommands.Add(new string { "Please", "Bye" });
        GrammarBuilder ongBuilder = new GrammarBuilder();
        ongBuilder.Append(oncommands);
        Grammar ongrammar = new Grammar(ongBuilder);
        ongrammar.Name = "Standby";

        return ongrammar;
        }

        public Grammar command_Grammar()
        {
        Choices commands = new Choices();
        commands.Add(new string { "Turn on the lights", "Bye" });
        GrammarBuilder gBuilder = new GrammarBuilder();
        gBuilder.Append(commands);
        Grammar grammar = new Grammar(gBuilder);
        grammar.Name = "ON";

        return grammar;
        }
        public Grammar off_Grammar()
        {
        Choices offcommands = new Choices();
        offcommands.Add(new string { "Hello", "Bye", "Good night", "Good morning" });
        GrammarBuilder offgBuilder = new GrammarBuilder();
        offgBuilder.Append(offcommands);
        Grammar offgrammar = new Grammar(offgBuilder);
        offgrammar.Name = "OFF";

        return offgrammar;
        }

        }

        public DemoCode _Grammar = new DemoCode();

        public enum ProcessState
        {
        on,
        command,
        off
        }

        public ProcessState current_state = ProcessState.off;

        public Form1()
        {
        InitializeComponent();
        }

        public void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
        here:
        do
        {
        if (current_state == ProcessState.on)
        {
        recEngine.LoadGrammarAsync(_Grammar.on_Grammar());
        spekEngine.Speak("I'm waiting for your commands");
        switch (e.Result.Text)
        {
        case "Please":
        current_state = ProcessState.command;
        richTextBox1.Text += e.Result.Grammar.Name;
        break;
        case "Bye":
        spekEngine.Speak("Bye, bye");
        current_state = ProcessState.off;
        richTextBox1.Text += e.Result.Grammar.Name;
        break;
        }
        }
        if (current_state == ProcessState.command)
        {
        recEngine.LoadGrammarAsync(_Grammar.command_Grammar());
        switch (e.Result.Text)
        {
        case "Turn the lights on":
        spekEngine.Speak("Of course");
        current_state = ProcessState.on;
        richTextBox1.Text += e.Result.Grammar.Name;
        break;
        case "Bye":
        spekEngine.Speak("Bye, bye");
        current_state = ProcessState.off;
        richTextBox1.Text += e.Result.Grammar.Name;
        break;
        }
        }
        if (current_state == ProcessState.off)
        {
        recEngine.LoadGrammarAsync(_Grammar.off_Grammar());
        switch (e.Result.Text)
        {
        case "Hello":
        spekEngine.Speak("Hi there");
        current_state = ProcessState.on;
        richTextBox1.Text += e.Result.Grammar.Name;
        break;
        case "Bye":
        spekEngine.Speak("Bye, bye");
        current_state = ProcessState.off;
        richTextBox1.Text += e.Result.Grammar.Name;
        break;
        }
        }
        } while (e.Result.Text != "Good night");

        if (e.Result.Text == "Good morning")
        {
        goto here;
        }
        }


        public void recEngine_SpeechDetected(object sender, SpeechDetectedEventArgs e)
        {
        richTextBox1.Text += " Speech detected at AudioPosition = " + e.AudioPosition;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        recEngine.LoadGrammarAsync(_Grammar.off_Grammar());
        recEngine.SetInputToDefaultAudioDevice();
        spekEngine.SetOutputToDefaultAudioDevice();
        recEngine.SpeechRecognized += recEngine_SpeechRecognized;
        }

        private void btnEnable_Click(object sender, EventArgs e)
        {
        recEngine.RecognizeAsync(RecognizeMode.Multiple);
        btnDisable.Enabled = true;
        }

        private void btnDisable_Click(object sender, EventArgs e)
        {
        recEngine.RecognizeAsyncStop();
        btnDisable.Enabled = false;
        }
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 11:32









        Áron Tóth

        11




        11






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53223758%2fform-window-not-opening-when-i-run-the-program-because-of-a-line-in-c-sharp%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python