using System.Threading;
private CancellationTokenSource cts;
private void btn_Grab_Click(object sender, EventArgs e)
{
if(image.isCamOK==false)
{
var result = image.OpenCam(camName);
if (result.IsSucess)
{
AddLog(0, "打开相机成功");
this.tb_ExposureTime.Value = image.GetExposureTime();
this.txt_ExposureTime.Text = image.GetExposureTime().ToString();
this.tb_Gain.Value = image.GetGain();
this.txt_Gain.Text = image.GetGain().ToString();
this.chk_Trigger.Checked = image.GetTriggerMode();
}
else
{
AddLog(2, "打开相机失败");
return;
}
}
cts = new CancellationTokenSource();
Task.Run(() =>
{
GrabImageThread();
},cts.Token);
AddLog(0, "开始连续采图");
}
private void GrabImageThread()
{
while (!cts.IsCancellationRequested)
{
if (image.isCamOK)
{
var result = image.GrabImage(ref hWindowHandle, ref hImage);
if (result.IsSucess)
{
//处理
}
else
{
AddLog(1, "采图失败");
break;
}
}
else
{
AddLog(1, "采图失败");
break;
}
}
}
private void btn_StopGrab_Click(object sender, EventArgs e)
{
AddLog(0, "停止连续采图");
//image.CloseCam();
cts?.Cancel();
}