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.Threading;
using System.Diagnostics;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private List<string> strs = null;
private void Form1_Load(object sender, EventArgs e)
{
strs = new List<string>();
}
private void timer1_Tick(object sender, EventArgs e)
{
try
{
int num = int.Parse(lblSecondCount.Text);
num++;
strs.Add("a");
lblSecondCount.Text = num.ToString();
if (num == 5)
{
timer1.Enabled = false;
throw new Exception("5秒了,抛出异常!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
MessageBox.Show("即将重新启动应用程序!");
this.Close();
/* 以下两种方式都可以重启应用程序 */
// 方式一
Restart();
// 方式二
Run2();
}
}
private void Restart()
{
Thread thtmp = new Thread(new ParameterizedThreadStart(Run1));
object appName = Application.ExecutablePath;
Thread.Sleep(2000);
thtmp.Start(appName);
}
private void Run1(Object obj)
{
Process ps = new Process();
ps.StartInfo.FileName = obj.ToString();
ps.Start();
}
private void Run2()
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.WorkingDirectory = Application.StartupPath;
psi.FileName = Application.ExecutablePath;
psi.Arguments = "";
try
{
Process.Start(psi);
}
catch (System.ComponentModel.Win32Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
}
}