Click here to Skip to main content
16,017,650 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
actually i have develop magic 8 ball program using speech reconzer and sythnizer(asp.net C#)

but i have small Exception
that is

C#
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.


i have not design in design page.
my source code is

C#
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]);

        }
    }
}

please i want correct solution As soon as possible

thank u
Posted
Updated 17-Apr-14 4:29am
v2
Comments
Sampath Lokuge 17-Apr-14 10:29am    
Can you put your stack trace ?
[no name] 17-Apr-14 21:37pm    
Well the error is telling you everything you need to know. I am curious though... do you really expect to give people access to your web server so they can talk to it? Because that is what you are going to have to do.... but if you insist that you want to do this, see this http://stackoverflow.com/questions/672237/running-an-asynchronous-operation-triggered-by-an-asp-net-web-page-request

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900