专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > PowerDesigner

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

发布时间:2011-06-20 20:21:30 文章来源:www.iduyao.cn 采编人员:星星草
解决 Control.Invoke 必须用于与在独立线程上创建的控件交互。在智能设备上的(c# / PDA)
      

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();
        }
    }
}

友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: