c#线程-线程间发消息-线程给窗体传值

这篇文章,部分代码是一个好友(高手)BoC2008告诉我的。。。

首先是个超简单的例子:

using System.Threading;

namespace ConsoleApplication1
{

    class Program
    {
        public static Thread thread1 = new Thread(new ThreadStart(HelloWorld));//这里ThreadStart()好像必须要接static的方法
        static void Main(string[] args)
        {

            Thread thread1 = new Thread(new ThreadStart(HelloWorld));
            Thread thread2 = new Thread(new ThreadStart(HellodotNet));
            thread1.Start();
            thread2.Start();

            Console.Read();

        }

       protected static void HelloWorld()
        {

            while (1 == 1)
            {
                Console.WriteLine("Hello World!");
                Thread.Sleep(1000);
            }
           
        }

        protected static void HellodotNet()
        {

            for (int i = 1; i < 3; i++)
            {
                Console.WriteLine("Hello dotNet!");
                Thread.Sleep(1000);
            }


        }
    }
}
 

 //----------------------------------------------------注解

            //Thread thread = new Thread(new ThreadStart(HelloWorld));
            //thread.Start();//开始一个线程

            //thread.Priority = ThreadPriority.Highest;

            //if (thread.IsAlive)//判断线程状态
            //{
            //    Console.WriteLine("Thread is Alive");
            //    Console.WriteLine(thread.ThreadState.ToString());
            //}

            //if (thread.ThreadState == ThreadState.Running)//挂起一个线程
            //{
            //    Thread.Sleep(1000);//暂停线程
            //    Console.WriteLine("Thread is Suspend");
            //    Console.WriteLine(thread.ThreadState.ToString());
            //}

            //if (thread.ThreadState == ThreadState.Suspended)//唤醒一个线程
            //{
            //    thread.Resume();
            //    Console.WriteLine("Thread is Resume");
            //    Console.WriteLine(thread.ThreadState.ToString());
            //}

            //thread.Abort();

            //if (!thread.IsAlive)
            //{
            //    Console.WriteLine("Thread is Over");
            //}

//--------------------------------------------------------------------

下面才是我要得瑟的东西,虽然是朋友告诉我的,我也不太明白吧。。。但是我觉得很厉害

1:说明:我要写个线程每秒去触发读卡器工作,然后把读到的结果传给form窗体

2:代码:

线程类:

using System.Threading;
using System.Runtime.InteropServices//这个是用来引用dll的

namespace cmp
{
    public delegate void CardReadHandler(string param);//建了个委托


    public class CardReader
    {
        [DllImport("rfdll32.dll", EntryPoint = "rf_card", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_card();//读卡
        [DllImport("rfdll32.dll", EntryPoint = "rf_rd_card", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        public static extern int rf_rd_card(int a, int b, int Idata, int c, byte[] sData);

       //-------------------------------------引用外部的dll中的方法

        public Thread thread1 = new Thread(new ThreadStart(ReadKA));

        private static string kaid = "";

        private static CardReadHandler onCardRead;//委托

        public event CardReadHandler CardRead//一个事件
        {
            add { onCardRead += new CardReadHandler(value); }
            remove { onCardRead -= new CardReadHandler(value); }
        }


        //读卡
        protected static void ReadKA()
        {

            while (1 == 1)
            {
                Thread.Sleep(2000);//线程2秒释放下内存
                int read = rf_card();//读卡的方法
                if (read == 0)//有卡
                {
                    byte[] by = new byte[40];
                    int rd_card = rf_rd_card(18, 0, 2, 16, by);
                    Encoding ed = Encoding.ASCII;
                    string data = ed.GetString(by);
                    if (data == "dddddddddddddddddddddddddddddddd/0/0/0/0/0/0/0/0")//空值
                    { }
                    else
                    {
                        if (kaid + "/0/0/0/0/0/0/0/0" == data)//如果是同一张卡
                        {
                            onCardRead("0");//给另一个线程传值

                        }
                        else
                        {
                            kaid = data.Substring(0,8);
                            onCardRead(kaid);
                        }
                    }

                }//没卡
                else
                {

                    onCardRead("1");//没有卡
                }

            }


        }
    }
}

3:在form窗体里:

namespace cmp
{
    public partial class gjcmp : Form
    {
        private static string ka_id = "";//学生id
        //private static string yemian = "";//判断是那个页面
        public gjcmp()
        {
            InitializeComponent();


            //线程
            CardReader cd = new CardReader();
            cd.CardRead += this.OnCardRead;//这两行要写上去调上面线程的事件
        }

        //线程返回的卡id
        private void OnCardRead(string param)//上面写的线程会不断给这个方法传值,所以操作写在这里
        {
                ka_id = param;
                object[] pList = { this, System.EventArgs.Empty };
                label1.BeginInvoke(new System.EventHandler(UpdateUI), pList);//这两行是还切换线程的,这样就可以给当前线程的控件传值了           

        }

        //切换线程
        private void UpdateUI(object o, System.EventArgs e)
        {
                                label1.Text = ka_id;
        }
         

  }

}

到这后台线程给主线程发消息就完成了。

本人也是初学,如有疏漏请指教。

4:最后补充句:from窗体获得页面值的方法:

 private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)//跳转页面
        {
            System.Windows.Forms.HtmlDocument documnet = this.webBrowser1.Document;
            yemian = documnet.All["Text1"].GetAttribute("value");
        }

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值