Asynchronous operations are not allowed in this context. Page starting an asynchronous operation has to have the Async attribute set to true and an asynchronous operation can only be started on a page prior to PreRenderComplete event.
using System; using System.Threading; using System.Speech.Recognition; using System.Speech.Synthesis; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace magic_ball { public partial class Magic : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { SpeechRecognitionEngine _recognizer = new SpeechRecognitionEngine(); Choices options = new Choices(); options.Add("Application"); options.Add("Close"); GrammarBuilder grammer = new GrammarBuilder(); grammer.Append(options); Grammar G = new Grammar(grammer); _recognizer.LoadGrammar(G); _recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(_recognizer_SpeechRecognized); _recognizer.SetInputToDefaultAudioDevice(); // set the input of the speech recognizer to the default audio device _recognizer.RecognizeAsync(RecognizeMode.Multiple); // recognize speech asynchronous } void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) { string str = null; str = e.Result.Text.ToUpper(); switch (e.Result.Text.ToUpper()) { case "Application Close": System.Environment.Exit(1); break; default: generateRandomSpeech(); break; } } private static void generateRandomSpeech() { string[] responce = new string[15]; responce[0] = "As I see it, yes"; responce[1] = "It is certain"; responce[2] = "Most likely"; responce[3] = "Outlook good"; responce[4] = "Signs point to yes"; responce[5] = "Without a doubt"; responce[6] = "Yes"; responce[7] = "Yes - definitely"; responce[8] = "Reply hazy, try again"; responce[9] = "Ask again later"; responce[10] = "Better not tell you now"; responce[11] = "Cannot predict now"; responce[12] = "My reply is no"; responce[13] = "My sources say no"; responce[14] = "Outlook not so good"; responce[15] = "Very doubtful"; Random generator = new Random(); int randomValue = 0; randomValue = generator.Next(1, 15); var synthesizer = new SpeechSynthesizer(); synthesizer.Volume = 100; synthesizer.Rate = 1; synthesizer.SelectVoiceByHints(VoiceGender.Male, VoiceAge.Senior); synthesizer.Speak(responce[randomValue]); } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)