C#中线程调用

来源:百度知道 编辑:UC知道 时间:2024/05/24 10:30:59
private void takepicturefunction()//replace take picture button,take a picture

{

//Take a picture and set it to the Picture Box image.
//The parameter is the shutter speed value between 0 and 4096
imagePreviewPictureBox.Image = theCamera.AquireBitmapImage(3000);该处提示在非创建该线程处调用

//Refresh the picture box so that the image is displayed
imagePreviewPictureBox.Refresh();
}
我想在其他程序段中调用takepicturefunction.在调试时出现如上问题,请问如何解决
private void displayLivePreviewCheckBox_CheckedChanged(object sender, EventArgs e)
{
checkBoxChecked = displayLivePreviewCheckBox.Checked;
if (displayLivePreviewCheckBox.Checked)
{
theCamera.CheckLink();
theCamera.SelectCamera(0);
theCamera.InitCamera(true);
theCamera.SetShutte

用委托完成:
public delegate void Deal();
private Deal deal = new Deal();

在Form_Load()里面加上
deal+= takepicturefunction;

线程函数中
{
if(InvokeRequire)
{
this.Invoke(deal);
}
}

线程调用,通白话的说是具有安全方面的限制的.需要进行一定的安全设置.
方法是,假设你在FORM1中创建了Image.
你需要设置
Form1.CheckForIllegalCrossThreadCalls = false;
这样所有在Form1中创建的控件都可以访问了.

加上这句就OK了.当然还有其他方法,你可以查看MSDN相应内容.