用户代码无效操作异常 [英] invalid operation exception was unhandeld by usercode

查看:126
本文介绍了用户代码无效操作异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我已经开发了使用语音重新计算器和sythnizer(asp.net C#)的魔术8球程序。



但我有小的例外





actually i have develop magic 8 ball program using speech reconzer and sythnizer(asp.net C#)

but i have small Exception
that is

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没有设计设计页面。

我的源代码是





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

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

推荐答案

这篇关于用户代码无效操作异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆