代码如下:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX.AudioVideoPlayback;namespace WindowsPlayer
{
    public partial class Form1 : Form
    {
        private Audio ad = new Audio("D:\\我的音乐\\少儿歌曲\\21.wav");
        private int PlayIndex, n;
        private bool b;        public Form1()
        {
            InitializeComponent();
            b = true;
            PlayIndex = 0;
            n = 0;
        }        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            this.Set_Play();
            if (checkBox1.Checked == true)
            {
                timer1.Start();
            }
            else
            {
                timer1.Stop();
            }
        }        private void Set_Play()
        {
            if (checkBox1.Checked == true)
            {
                try
                {
                    switch (PlayIndex)
                    {
                        case 0:
                            ad.Open("D:\\我的音乐\\少儿歌曲\\notify.wav", true);
                            break;
                        case 1:
                            ad.Open("D:\\我的音乐\\少儿歌曲\\好爸爸坏爸爸a.mp3", true);
                            break;
                        default:
                            ad.Open("D:\\我的音乐\\少儿歌曲\\泥娃娃.mp3", true);
                            break;
                    }
                    ad.Ending += new EventHandler(ad_Ending);
                }
                catch (Exception)
                {
                    ad = new Audio("D:\\我的音乐\\少儿歌曲\\21.wav"); 
                    b = false;
                    textBox2.Text += "asdf";
                }
            }
            else
            {
                try
                {
                    ad.Stop();
                }
                catch
                {
                }
            }
        }        void ad_Ending(object sender, EventArgs e)
        {
            b = false;
            textBox2.Text += "asdf";
        }        private void timer1_Tick(object sender, EventArgs e)
        {
            if (b == false)
            {
                b = true;
                if (PlayIndex < 2)
                {
                    PlayIndex++;
                }
                else
                {
                    PlayIndex = 0;
                }
                n++;
                textBox1.Text = n.ToString();
                this.Set_Play();
            }
        }
    }
}
当不出异常时关闭后没有任何问题,当出过异常,异常已解决,关闭程序时会弹出一个错误框。什么原因?

解决方案 »

  1.   

    AppDomain currentDomain = AppDomain.CurrentDomain;  
    currentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptions);  
      private void UnhandledExceptions(object sender, UnhandledExceptionEventArgs args)  
      { } 
     
      

  2.   

    ad.Ending += new EventHandler(ad_Ending);
    //应该是这里导致了问题
            catch (Exception)
                    {
                        ad = new Audio("D:\\我的音乐\\少儿歌曲\\21.wav"); 
                        b = false;
                        textBox2.Text += "asdf";
                    }
      

  3.   

    总觉得try-catch用多了,一般一个够了!改一下你的结构试试!
    try
    {
          if (checkBox1.Checked == true)
          {
             switch (PlayIndex)
             //..........
          }
          else
          {
             ad.Stop();
          }
    }
    catch
    {
          if (checkBox1.Checked == true)
          {
               ad = new Audio("D:\\我的音乐\\少儿歌曲\\21.wav"); 
               b = false;
               textBox2.Text += "asdf";
          }
    }
      

  4.   

    4楼:当出异常时,catch里不添加"ad = new Audio("D:\\我的音乐\\少儿歌曲\\21.wav"); "这一句,当执行"ad.Stop();"时又会弹出另一个错误框,提示没有将对象引用到实例,只有添加上这一句才行.
    这已不是我要问的了,我现在想知道如何去掉关闭后的消息框.
      

  5.   

    AppDomain.CurrentDomain同一处理异常
    在Main函数中添加对未处理异常的处理事件AppDomain.CurrentDomain.UnhandledException +=new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
    否则就加try catch
      

  6.   

    8楼的朋友,您好.请看我添加的有什么问题?using System;
    using System.Collections.Generic;
    using System.Windows.Forms;namespace WindowsPlayer
    {
        static class Program
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
                AppDomain app = AppDomain.CurrentDomain;
                app.UnhandledException += new UnhandledExceptionEventHandler(app_UnhandledException);
            }        static void app_UnhandledException(object sender, UnhandledExceptionEventArgs e)
            {
                //请问事件代码怎么写?
            }
        }请问事件代码怎么写?
      

  7.   

    8楼的朋友,我把您告诉我的代码写在了如下处
            private void Set_Play()
            {
                AppDomain app = AppDomain.CurrentDomain;
                app.UnhandledException += new UnhandledExceptionEventHandler(app_UnhandledException);
                if (checkBox1.Checked == true)
                {
                    try
                    {
                        switch (PlayIndex)
                        {
                            case 0:
                                ad.Open("D:\\我的音乐\\少儿歌曲\\notify.wav", true);
                                break;
                            case 1:
                                ad.Open("D:\\我的音乐\\少儿歌曲\\好爸爸坏爸爸a.mp3", true);
                                break;
                            default:
                                ad.Open("D:\\我的音乐\\少儿歌曲\\泥娃娃.mp3", true);
                                break;
                        }
                        ad.Ending += new EventHandler(ad_Ending);
                    }
                    catch (Exception)
                    {
                        ad = new Audio("D:\\我的音乐\\少儿歌曲\\21.wav"); 
                        b = false;
                        textBox2.Text += "asdf";
                    }
                }
                else
                {
                    try
                    {
                        ad.Stop();
                    }
                    catch
                    {
                    }
                }
            }        void app_UnhandledException(object sender, UnhandledExceptionEventArgs e)
            {
                MessageBox.Show(e.ExceptionObject.ToString());//处理代码
                //throw new Exception("The method or operation is not implemented.");
            }
    但必须在事件处理的大括号里写处理代码,否则不行.
      

  8.   

     Microsoft.DirectX.AudioVideoPlayback.Audio.Finalize()出问题了,内存错误了。建议检查你的代码,确定所使用的资源都正确的释放了。
      

  9.   

    没有引发异常时,可正常关闭程序.一旦引发异常后,在关闭时就会出问题.按照8楼的提示,我把关闭时的错误信息捕捉到了,以消息框的形式弹出.错误信息如下:System.AccessViolxtionException:尝试读取或写入受保护的内存。这通常指示其他内存已损坏。请问怎样才能在关闭时不出错误?
      

  10.   

    换用vb.net来写把。那强大的
    On error resume next
    就是你要的,也是c#无法实现的。
      

  11.   

    可能情况:1.  如果在下面的语句有异常,你的程序没有 catch 
    if (checkBox1.Checked == true)
          {
               ad = new Audio("D:\\我的音乐\\少儿歌曲\\21.wav"); 
               b = false;
               textBox2.Text += "asdf";
          }2. 是不是调试的时候出现, 运行的时候就不会了。