Network Connected and Disconnected Events in a Forms Application

Network Connected and Disconnected Events in a Forms Application

Lester Memmott (Intel)的头像

We've had a few people ask for sample code demonstrating the use of the Intel Mobile Platfrom SDK (See: http://www.intel.com/software/products/mobileplatform) within a Windows Forms application. Note that the SDK comes with extensive sample code but it is limited to console application. For those who would like a forms app that runs on .Net v1.1 see the attached zip file. The source code shows a simple forms application that receive the connected and disconnected events and updates the GUI each time this happens.

Thanks,
Lester Memmott

2 帖子 / 0 全新
最新文章
如需更全面地了解编译器优化,请参阅优化注意事项
Lester Memmott (Intel)的头像

It looks like the forum is having trouble with attachments so here it is as text:

// _____________________________________________________________________________

// Copyright (C) Intel Corporation (2006)

// DISCLAIMER OF WARRANTY

// NEITHER INTEL NOR ITS SUPPLIERS MAKE ANY REPRESENTATION OR WARRANTY OR

// CONDITION OF ANY KIND WHETHER EXPRESS OR IMPLIED (EITHER IN FACT OR BY

// OPERATION OF LAW) WITH RESPECT TO THE SOURCE CODE. INTEL AND ITS SUPPLIERS

// EXPRESSLY DISCLAIM ALL WARRANTIES OR CONDITIONS OF MERCHANTABILITY OR

// FITNESS FOR A PARTICULAR PURPOSE. INTEL AND ITS SUPPLIERS DO NOT WARRANT

// THAT THE SOURCE CODE IS ERROR-FREE OR THAT OPERATION OF THE SOURCE CODE WILL

// BE SECURE OR UNINTERRUPTED AND HEREBY DISCLAIM ANY AND ALL LIABILITY ON

// ACCOUNT THEREOF. THERE IS ALSO NO IMPLIED WARRANTY OF NON-INFRINGEMENT.

// SOURCE CODE IS LICENSED TO LICENSEE ON AN "AS IS" BASIS AND NEITHER INTEL

// NOR ITS SUPPLIERS WILL PROVIDE ANY SUPPORT, ASSISTANCE, INSTALLATION,

// TRAINING OR OTHER SERVICES. INTEL AND ITS SUPPLIERS WILL NOT PROVIDE ANY

// UPDATES, ENHANCEMENTS OR EXTENSIONS.

// ____________________________________________________________________________

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using Intel.Mobile.Base;

using Intel.Mobile.Context;

namespace EventFormSample

{

///

/// Summary description for Form1.

///

public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.Label label1;

private System.Windows.Forms.Label Label_Status;

public System.Windows.Forms.TextBox textBox_info;

private ContextClass myClass;

private System.Windows.Forms.Label label2;

public static EventObserver m_clObserver;

///

/// Required designer variable

///

private System.ComponentModel.Container components = null;

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

//

// TODO: Add any constructor code after InitializeComponent call

//

}

///

/// Clean up any resources being used.

///

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

///

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

///

private void InitializeComponent()

{

this.label1 = new System.Windows.Forms.Label();

this.Label_Status = new System.Windows.Forms.Label();

this.textBox_info = new System.Windows.Forms.TextBox();

this.label2 = new System.Windows.Forms.Label();

this.SuspendLayout();

//

// label1

//

this.label1.Location = new System.Drawing.Point(16, 16);

this.label1.Name = "label1";

this.label1.Size = new System.Drawing.Size(40, 23);

this.label1.TabIndex = 0;

this.label1.Text = "Status:";

//

// Label_Status

//

this.Label_Status.Location = new System.Drawing.Point(64, 16);

this.Label_Status.Name = "Label_Status";

this.Label_Status.TabIndex = 1;

//

// textBox_info

//

this.textBox_info.Location = new System.Drawing.Point(16, 72);

this.textBox_info.Multiline = true;

this.textBox_info.Name = "textBox_info";

this.textBox_info.ScrollBars = System.Windows.Forms.ScrollBars.Horizontal;

this.textBox_info.Size = new System.Drawing.Size(256, 152);

this.textBox_info.TabIndex = 2;

this.textBox_info.Text = "";

//

// label2

//

this.label2.Location = new System.Drawing.Point(16, 48);

this.label2.Name = "label2";

this.label2.Size = new System.Drawing.Size(100, 16);

this.label2.TabIndex = 3;

this.label2.Text = "Event Log :";

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(292, 238);

this.Controls.Add(this.label2);

this.Controls.Add(this.textBox_info);

this.Controls.Add(this.Label_Status);

this.Controls.Add(this.label1);

this.Name = "Form1";

this.Text = "EventTestForm";

this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);

this.Load += new System.EventHandler(this.Form1_Load);

this.ResumeLayout(false);

}

#endregion

///

/// The main entry point for the application.

///

[MTAThread]

static void Main()

{

Application.Run(new Form1());

}

private void Form1_Load(object sender, System.EventArgs e)

{

try

{

myClass = new ContextClass();

ConnectivityInstance myInstance = (ConnectivityInstance) myClass.GetInstance("Co
nnectivity");

m_clObserver = new EventObserver();

myInstance.Connected.AddObserver( m_clObserver );

myInstance.Disconnected.AddObserver( m_clObserver );

Label_Status.Text = myInstance.IsConnected.GetValue()?"Connected":"Disconnected";

m_clObserver.MyEvent += new EventObserver.MyEventHandler(EventHandler);

}

catch (IntelMobileException ex)

{

MessageBox.Show(Form.ActiveForm, "Message: " + ex.Message, "Exception" );

}

}

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)

{

ConnectivityInstance myInstance = (ConnectivityInstance) myClass.GetInstance("Connectivity");

myInstance.Connected.RemoveObserver( m_clObserver );

myInstance.Disconnected.AddObserver( m_clObserver );

m_clObserver.Close();

}

private void EventHandler(bool bStatus, String sInfo)

{

textBox_info.AppendText( sInfo );

textBox_info.AppendText( "
" );

Label_Status.Text = bStatus?"Connected":"Disconnected";

}

}

public class EventObserver : Observer

{

public delegate void MyEventHandler(bool bStatus, String sInfo);

public event MyEventHandler MyEvent;

public override void Notify(Event ev)

{

try

{

if ( ev.GetType() == EventBase.EventType.eConnected )

{

MyEvent( true, ev.GetTSstring() + " Connected event occurred for " + ev.GetOriginator().GetType() );

}

if ( ev.GetType() == EventBase.EventType.eDisconnected )

{

MyEvent ( false, ev.GetTSstring() + " Disconnected event occurred for " + ev.GetOriginator().GetType() );

}

}

catch (IntelMobileException ex)

{

MessageBox.Show(Form.ActiveForm, "Message: " + ex.Message, "Exception" );

}

}

}

}

登陆并发表评论。