using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
//using System.Runtime.InteropServices; namespace WinAgilentSoftWare
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Button CreatThread;
/// <summary>
/// 必需的设计器变量。
/// </summary>
//定义公共变量
static int defaultRM, vi;
static int ThreadFlag = 0; static int i = 0; ServerClass serverObject ;
Thread InstanceCaller;
static DataTable  TTABLE ;
private System.Windows.Forms.Button CloseDevice;
private System.Windows.Forms.Label Sharpe;
private System.Windows.Forms.Button SetParameter;
private System.Windows.Forms.Label CommFlag;
private System.Windows.Forms.Button SaveExcel;
private System.Windows.Forms.DataGrid DataGrid;

private System.ComponentModel.Container components = null;
public class ServerClass
{
//------------动态的线程服务对象----------------------------------------------------------------------

public MainForm mainForm;
public void InstanceMethod()
{
Console.WriteLine("服务线程正在运行.");
//
short id_reg, devtype_reg;
float[] Valuev = new float[10]; //打开设备
visa32.viOpenDefaultRM(out defaultRM);
visa32.viOpen(defaultRM, "GPIBO::22::INSTR", visa32.VI_NULL,visa32.VI_NULL, out vi); visa32.viPrintf(vi,"*RST;*CLS\n"); //复位
visa32.viPrintf(vi,"CONG:VOLT:DC 10,0.003,(@103)\n");//配置
visa32.viPrintf(vi,"TRIG:SOUR EXT\n");

id_reg = 1;      //设备ID 
devtype_reg = 2; //设备型号 while (ThreadFlag == 0 )
{
i = i + 1 ;
//valuex = 0;
visa32.viPrintf(vi,"READ\n");
visa32.viScanf(vi,"%f",Valuev);
         Console.WriteLine("ID Register = {0}\n", id_reg);
//读取的数值Valuev里
//Thread.Sleep(1000); DataRow newRow1 = TTABLE.NewRow();
newRow1[0]=i;
newRow1[1]=i + 1;
newRow1[2]=i + 2;
newRow1[3]=i + 3;
newRow1[4]=i + 4;
newRow1[5]=i + 5;
newRow1[6]=i + 6;
newRow1[7]=i + 7;
newRow1[8]=i + 8;

    TTABLE.Rows.Add(newRow1); mainForm.DataGrid.CurrentCell =  new DataGridCell(i+1,1);
}
//得到机器的ID
//visa32.viIn16 (vi, visa32.VI_A16_SPACE, 0x00, out id_reg);  //得到机器型号
//visa32.viIn16 (vi, visa32.VI_A16_SPACE, 0x02, out devtype_reg); //输出结果
Console.WriteLine("ID Register = {0}\n", id_reg);
Console.WriteLine ("Device Type Register = {0}\n", devtype_reg); //关闭设备
visa32.viClose(vi);
visa32.viClose(defaultRM); // Pause for a moment to provide a delay to make threads more apparent.

//
Thread.Sleep(3000);
Console.WriteLine("服务线程运行结束.");
}
//------------静态的线程服务对象------------------------------------------------------------------------------------------------
public static void StaticMethod()
{
Console.WriteLine("The static服务线程正在运行.");
// Pause for a moment to provide a delay to make threads more apparent.
Thread.Sleep(5000);
Console.WriteLine("The static 服务线程运行结束.");
}
} public MainForm()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/////////////////////////////////////////////////
///
/////////////////////////////////////////////////
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{

}
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new MainForm());
} private void CreatThread_Click(object sender, System.EventArgs e)
{
            
Console.WriteLine("线程例子开始"); //创建一个对象
serverObject = new ServerClass();
            //创建一个线程      
serverObject.mainForm = this;
MainForm aa = new MainForm();
InstanceCaller = new Thread(new ThreadStart(serverObject.InstanceMethod));
//线程开始执行
InstanceCaller.Start();

} private void SetParameter_Click(object sender, System.EventArgs e)
{
//显示设置参数窗体
ParrameForm Temp = new ParrameForm();
Temp.ShowDialog();
} private void CloseDevice_Click(object sender, System.EventArgs e)
{
//撤消线程
InstanceCaller.Abort();
} private void SaveExcel_Click(object sender, System.EventArgs e)
{
// 建一个DataSet.
    DataSet myDataSet = new DataSet("myDataSet");
      
//建立1个 DataTables.
TTABLE = new DataTable("查询表");
}
}
}

解决方案 »

  1.   

    简化一下:
    namespace WinAgilentSoftWare
    {
    public class MainForm : System.Windows.Forms.Form
    {
    //定义公共变量
    static int defaultRM, vi;
    static int ThreadFlag = 0;
    static int i = 0;
    ServerClass serverObject ;
    Thread InstanceCaller;
    static DataTable  TTABLE ;

    public class ServerClass
    {
    //------------动态的线程服务对象--------------------------------------------

    public MainForm mainForm;
    public void InstanceMethod()
    {
    Console.WriteLine("服务线程正在运行.");
    while (ThreadFlag == 0 )
    {
    DataRow newRow1 = TTABLE.NewRow();
    newRow1[0]=i;
    newRow1[1]=i + 1;
    newRow1[2]=i + 2;
    newRow1[3]=i + 3;
    newRow1[4]=i + 4;
    newRow1[5]=i + 5;
    newRow1[6]=i + 6;
    newRow1[7]=i + 7;
    newRow1[8]=i + 8;
        TTABLE.Rows.Add(newRow1);
    mainForm.DataGrid.CurrentCell =  new DataGridCell(i+1,1);
    }