代码如下: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();
}
}
}
}
当不出异常时关闭后没有任何问题,当出过异常,异常已解决,关闭程序时会弹出一个错误框。什么原因?
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();
}
}
}
}
当不出异常时关闭后没有任何问题,当出过异常,异常已解决,关闭程序时会弹出一个错误框。什么原因?
currentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptions);
private void UnhandledExceptions(object sender, UnhandledExceptionEventArgs args)
{ }
//应该是这里导致了问题
catch (Exception)
{
ad = new Audio("D:\\我的音乐\\少儿歌曲\\21.wav");
b = false;
textBox2.Text += "asdf";
}
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";
}
}
这已不是我要问的了,我现在想知道如何去掉关闭后的消息框.
在Main函数中添加对未处理异常的处理事件AppDomain.CurrentDomain.UnhandledException +=new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
否则就加try catch
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)
{
//请问事件代码怎么写?
}
}请问事件代码怎么写?
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.");
}
但必须在事件处理的大括号里写处理代码,否则不行.
On error resume next
就是你要的,也是c#无法实现的。
if (checkBox1.Checked == true)
{
ad = new Audio("D:\\我的音乐\\少儿歌曲\\21.wav");
b = false;
textBox2.Text += "asdf";
}2. 是不是调试的时候出现, 运行的时候就不会了。