aday

aday

  • NA
  • 5
  • 0

can not run asynchronously with com object

Sep 7 2008 3:19 AM

I wrote the following code to try to run code asynchronously and it seems not work:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SpeechLib;
using System.Threading;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        static BackgroundWorker bg = new BackgroundWorker();
        static  SpVoice myVoice = new SpVoice();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            foreach (ISpeechObjectToken token in myVoice.GetVoices("", ""))
            {
                comboBox1.Items.Add(token.GetDescription(0));               
            }
            comboBox1.SelectedIndex = 0;
            myVoice.Rate = -6;
            bg.DoWork += new DoWorkEventHandler(bg_DoWork);
        }

        void bg_DoWork(object sender, DoWorkEventArgs e)
        {
            myVoice.Speak("hello , this is my world", SpeechVoiceSpeakFlags.SVSFDefault);
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            myVoice.Voice=myVoice.GetVoices("","").Item(comboBox1.SelectedIndex);
            bg.RunWorkerAsync();         
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                myVoice.Speak("", SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
            }
            catch
            {

            }           
        }

    }
}

The problem is when text to speech is running , the  form seems freezed and I can't even push the button on the form.


Answers (2)