您的位置:首页 > 编程语言 > C#

解决 Control.Invoke 必须用于与在独立线程上创建的控件交互。在智能设备上的(c# / PDA)

2012-06-18 17:39 375 查看
      
using System;

using System.Linq;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using OpenNETCF.Net;

using OpenNETCF.Net.NetworkInformation;

using System.Threading;

namespace GetMAC

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        // 声明一个委托

        private delegate void NewDel();

        // 创建一个 新线程的方法

        public void Kaishi()

        {

            Thread thread;

            ThreadStart threadstart = new ThreadStart(start);

            thread = new Thread(threadstart);

            thread.Start();

        }

        // 屏蔽错误的方法   说白了 就是通过了一个 委托 

        // 解决Control.Invoke 必须用于与在独立线程上创建的控件交互。

        private void start()

        {

            if (InvokeRequired)

            {

                // 要 努力 工作的 方法

                BeginInvoke(new NewDel(GetMac));

            }

        }

        // 这里是主要的为了提供带参数的时候方便

        public void Read()

        {

            Kaishi();

        }

        // 调用方法

        private void button1_Click(object sender, EventArgs e)

        {

          // Read();  //  两个都是一样的 ,主要是 为了扩展的 方便

           // Kaishi();

            

        }

        // 实质工作的 方法体

        private void  GetMac()

        {

            string mac = "";

            int a = 0;

            foreach (INetworkInterface currentInterface in NetworkInterface.GetAllNetworkInterfaces())

            {

                a = 1;

                mac = currentInterface.GetPhysicalAddress().ToString();

                if (a != 0)

                {

                    break;

                }

            }

            label1.Text = mac.Trim();

        }

        private void Form1_Load(object sender, EventArgs e)

        {

            //System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;

        }

        private void button2_Click(object sender, EventArgs e)

        {

            Application.Exit();

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐