none
线程中事件委托的方法为何没执行? RRS feed

  • 问题

  • 在buttion2的click事件中,如果直接使用注释掉的代码,

    当音乐播放和结束事件发生的时候的时候,audio_starting和audio_ending都会执行。

    如果采用开一个线程的方法,音乐能播放,但是事件方法不会执行,请大侠帮我看看为什么?

    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 System.Media; using System.IO; using Microsoft.DirectX; using Microsoft.DirectX.DirectSound; using Microsoft.DirectX.AudioVideoPlayback; using System.Threading; namespace WindowsFormsApplication1 { public partial class Form1 : Form { Audio audio; public Form1() { InitializeComponent(); Form.CheckForIllegalCrossThreadCalls = false; } private void button2_Click(object sender, EventArgs e) { Thread playThread = new Thread(new ThreadStart(play)); playThread.Priority = ThreadPriority.Highest; playThread.Start(); //Audio audio = new Audio("hello.mp3"); //audio.Starting += new EventHandler(audio_Starting); //audio.Ending += new EventHandler(audio_Ending); //audio.Play(); } void audio_Starting(object sender, EventArgs e) { MessageBox.Show("开始播放"); } void audio_Ending(object sender, EventArgs e) { MessageBox.Show("播放完成"); } void play() { audio = new Audio("hello.mp3"); audio.Starting += new EventHandler(audio_Starting); audio.Ending += new EventHandler(audio_Ending); audio.Play(); } } }


    2012年10月7日 13:20

答案

  • 建议你这样做(只把Play放到外面)

    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            Audio audio;      
            
            public Form1()
            {
                InitializeComponent();
                Form.CheckForIllegalCrossThreadCalls = false;
            }
            private void button2_Click(object sender, EventArgs e)
            {
                Thread playThread = new Thread(new ThreadStart(play));
                playThread.Priority = ThreadPriority.Highest; 
                playThread.IsBackGround = true;           
                playThread.Start();
    
                Audio audio = new Audio("hello.mp3");
                audio.Starting += new EventHandler(audio_Starting);
                audio.Ending += new EventHandler(audio_Ending);
            }
            void audio_Starting(object sender, EventArgs e)
            {
                MessageBox.Show("开始播放");
            }
            void audio_Ending(object sender, EventArgs e)
            {
                MessageBox.Show("播放完成");
            }
            void play()
            {
                audio.Play();
            }
        }
    }
    

    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处

    2012年10月8日 2:32
    版主

全部回复

  • 建议你这样做(只把Play放到外面)

    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            Audio audio;      
            
            public Form1()
            {
                InitializeComponent();
                Form.CheckForIllegalCrossThreadCalls = false;
            }
            private void button2_Click(object sender, EventArgs e)
            {
                Thread playThread = new Thread(new ThreadStart(play));
                playThread.Priority = ThreadPriority.Highest; 
                playThread.IsBackGround = true;           
                playThread.Start();
    
                Audio audio = new Audio("hello.mp3");
                audio.Starting += new EventHandler(audio_Starting);
                audio.Ending += new EventHandler(audio_Ending);
            }
            void audio_Starting(object sender, EventArgs e)
            {
                MessageBox.Show("开始播放");
            }
            void audio_Ending(object sender, EventArgs e)
            {
                MessageBox.Show("播放完成");
            }
            void play()
            {
                audio.Play();
            }
        }
    }
    

    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处

    2012年10月8日 2:32
    版主
  • 建议你这样做(只把Play放到外面)

    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            Audio audio;      
            
            public Form1()
            {
                InitializeComponent();
                Form.CheckForIllegalCrossThreadCalls = false;
            }
            private void button2_Click(object sender, EventArgs e)
            {
                Thread playThread = new Thread(new ThreadStart(play));
                playThread.Priority = ThreadPriority.Highest; 
                playThread.IsBackGround = true;           
                playThread.Start();
    
                Audio audio = new Audio("hello.mp3");
                audio.Starting += new EventHandler(audio_Starting);
                audio.Ending += new EventHandler(audio_Ending);
            }
            void audio_Starting(object sender, EventArgs e)
            {
                MessageBox.Show("开始播放");
            }
            void audio_Ending(object sender, EventArgs e)
            {
                MessageBox.Show("播放完成");
            }
            void play()
            {
                audio.Play();
            }
        }
    }

    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处

    谢谢!果然是高手,

    。我稍微调整了一下,可以了:

    我想,是不是因为这个audio对象跨2个线程所以不可以?还望高手辅导辅导!

    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 Microsoft.DirectX;
    using Microsoft.DirectX.DirectSound;
    using Microsoft.DirectX.AudioVideoPlayback;
    using System.Threading;
    
    namespace WindowsFormsApplication6
    {
        public partial class Form1 : Form
        {
            Audio audio;
            public Form1()
            {
                InitializeComponent();
                Form.CheckForIllegalCrossThreadCalls = false;
            }
            private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    audio = new Audio("hello.mp3");
                    audio.Starting += new EventHandler(audio_Starting);
                    audio.Ending += new EventHandler(audio_Ending);
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                Thread playThread = new Thread(new ThreadStart(play));
                playThread.Priority = ThreadPriority.Highest;
                playThread.IsBackground = true;
                playThread.Start();
            }
            void audio_Starting(object sender, EventArgs e)
            {
                MessageBox.Show("开始播放");
            }
            void audio_Ending(object sender, EventArgs e)
            {
                MessageBox.Show("播放完成");
            }
            void play()
            {
                audio.Play();
            }
    
        }
    }
    

    2012年10月8日 5:05
  • 这个不是很清楚,照例应该放到里边去也是可以的。

    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处

    2012年10月8日 5:11
    版主