<< 点击显示目录 >> 主页 PVI通信 > PVI帮助信息 > PVI Services > 事件处理 > 控制台应用程序中的事件处理 |
"ConsoleApplication "例子
下面的例子解释了如何在一个控制台应用程序中处理事件。该应用程序打开一个与全局进程变量的连接,并将其值输出到控制台。为了用 Application.Run 方法 执行消息(消息循环) , 必须添加System.Windows.Forms.dll 组件作为参考。
// Definition of global communication objects
static Service service;
static Cpu cpu;
static Variable variable;
/// <summary>
/// Create a service, CPU, and variable object
/// </summary>
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Connecting Service, Cpu, Variable ...");
service = new Service("Service");
service.Error += new PviEventHandler(Error);
service.Connect();
cpu = new Cpu(service,"Cpu");
cpu.Connection.DeviceType = DeviceType.Serial;
cpu.Connection.Serial.Channel = 1;
cpu.Connect();
variable = new Variable(cpu,"count");
variable.Connect();
variable.Active = true;
variable.ValueChanged += new VariableEventHandler(ValueChanged);
// Execute messages (message loop)
Application.Run();
}
/// <summary>
/// End application if an error occurs
/// </summary>
static void Error(object sender, PviEventArgs e)
{
Console.WriteLine(String.Format("Error:{0}",e.ErrorText));
Application.Exit();
}
/// <summary>
/// Output value and end application
/// </summary>
static void ValueChanged(object sender, VariableEventArgs e)
{
Variable var = (Variable)sender;
Console.WriteLine("Value={0}",var.Value.ToString());
Application.Exit();
}