有人能帮我看看我的这个秒表程序哪些地方出错吗,谢谢
我几乎是抄别人的程序,可添加到我的项目里就错了,这是代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
namespace iAuto2
{
public class Stopwatch : ModuleBase
{
public Stopwatch()
: base()
{
this.IsVisible = true;
this.id = 32;
}
protected override SimFormBase CreateSelfForm(string Tag)
{
Debug.Assert(this.info != null && this.info.module == this && this.IsModuleRun && this.IsVisible);
Debug.Assert(!this.IsFormRun && this.form == null);
SimFormBase f = new StopwatchForm();
f.Tag = Tag;
return f;
}
}
public class StopwatchForm : SimFormBase
{
private Boolean started = false;
private DateTime dt;
private DateTime dt2;
private Thread time;
private Boolean clear = false;
private SimControlBase m_ctrlDisplay; //秒表数据显示控件
public override void OnCreate()
{
base.OnCreate();
foreach (SimControlBase ctrl in m_Controls)
{
if (ctrl == null)
continue;
//设置菜单分类项目
if (ctrl.Type == SimControlBase.Control_Type.button)
{
ctrl.OnClickEvent += new SimControlBase.SimControlMouseEventHandler(this.BtnCtrlClick);
}
if (ctrl.Name == "Display")
{
ctrl.Text = "00:00:00.000";
m_ctrlDisplay = ctrl;
}
}
}
public override void OnActive()
{
base.OnActive();
Thread time = new Thread(new ThreadStart(timeStart));
time.SetApartmentState(ApartmentState.MTA);
this.OnDeactive();
}
public override void OnDeactive()
{
base.OnDeactive();
this.started = false;
if (time.ThreadState == System.Threading.ThreadState.Suspended)
{
time.Resume();
time.Abort();
}
}
private void BtnCtrlClick(SimControlBase Ctrl, MouseEventArgs e)
{
switch (Ctrl.Name)
{
case "Start":
if (this.started)
{
this.started = false;
Ctrl.BkImageName = "Stop";
time.Suspend();
this.dt2 = DateTime.Now;
}
else
{
this.started = true;
Ctrl.BkImageName = "Start";
if (time.ThreadState == System.Threading.ThreadState.Suspended)
{
if (this.clear)
{
this.dt = DateTime.Now;
this.clear = false;
}
else
{
this.dt = this.dt.AddTicks(DateTime.Now.Ticks - this.dt2.Ticks);
}
time.Resume();
}
else
{
this.dt = DateTime.Now;
time.Start();
}
}
break;
case "Reset":
m_ctrlDisplay.Text = "00:00:00.000asd";
this.clear = true;
break;
case "Exit":
this.Close();
this.Dispose();
break;
}
}
/// <summary>
/// 设置一个代理
/// </summary>
/// <param name="aa"></param>
private delegate void dd(Int64 aa);
private void timeStart()
{
Int64 ms = 0;
while (this.started)
{
Thread.Sleep(1);
ms = DateTime.Now.Ticks - this.dt.Ticks;
SimFormDisp.instance.Invoke(new dd(bb), ms);
}
}
public void bb(Int64 aa)
{
Int64 ms = aa / 10000;//毫秒
int hh = (int)(ms / (1000 * 60 * 60));//时
int mm = (int)((ms % (1000 * 60 * 60)) / (1000 * 60));//分
int ss = (int)((ms % (1000 * 60)) / 1000);//秒
ms = (int)(ms % (1000));//毫秒
m_ctrlDisplay.Text = hh.ToString("00") + ":" + mm.ToString("00") + ":" + ss.ToString("00") + "." + ms.ToString("000");
}
}
}