首页 新闻 会员 周边 捐助

c#下如何使用消息函数

0
悬赏园豆:100 [已解决问题] 解决于 2009-01-29 22:32

做了一个关于消息传递的小程序,不知道为什么消息总是收不到呢?我把代码贴出来,麻烦大家给看看。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private Thread SQL_Thread;
        public struct tagMSG
        {
            public int hwnd;
            public uint message;
            public int wParam;
            public long lParam;
            public uint time;
            public int pt;
        }
        public tagMSG msg;
        public const int WM_CX_NULL = 0x400 + 100;
        [DllImport("user32.dll")]
        private static extern int PostThreadMessage(int threadId, uint msg, int wParam, int lParam);
        [DllImport("user32.dll")]
        private static extern int GetMessage(ref tagMSG lpMsg, int hwnd, int wMsgFilterMin, int wMsgFilterMax);
        public void SQL_ThreadExectue()
        {
            //Thread.Sleep(50);
            while (GetMessage(ref msg, 0, 0, 0) > 0)
            {

                if (msg.message == WM_CX_NULL)
                {
                    MessageBox.Show("消息收到!");
                }
            }
           
        }
        private void button1_Click(object sender, EventArgs e)
        {
           
            PostThreadMessage(SQL_Thread.ManagedThreadId, WM_CX_NULL, 1, 1);
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            msg = new tagMSG();
            SQL_Thread = new Thread(new ThreadStart(this.SQL_ThreadExectue));
            SQL_Thread.Priority = ThreadPriority.BelowNormal;
            SQL_Thread.Start();
        }
    }
}

锲而不舍的主页 锲而不舍 | 初学一级 | 园豆:50
提问于:2009-01-23 23:02
< >
分享
最佳答案
0

问题出在 SQL_Thread.ManagedThreadId

由于你调用的是Win32 API,这个API 无法识别管理线程,你必须发送消息到Windows的线程ID上,而不是管理线程的ID上。

修改你的的代码如下:

添加:

        [DllImport("kernel32.dll")]
        private static extern int GetCurrentThreadId();

        private int _SQLThreadId = 0;

修改:

        public void SQL_ThreadExectue()
        {

            _SQLThreadId = GetCurrentThreadId();

            //Thread.Sleep(50);
            while (GetMessage(ref msg, 0, 0, 0) > 0)
            {
                if (msg.message == WM_CX_NULL)
                {
                    MessageBox.Show("消息收到!");
                }
            }

        }

修改:

        private void button1_Click(object sender, EventArgs e)
        {
            PostThreadMessage(_SQLThreadId, WM_CX_NULL, 1, 1);
        }

修改完后,我测试了一下,可以收到消息了。

eaglet | 专家六级 |园豆:17139 | 2009-01-28 07:39
其他回答(3)
0

是不是hwnd为0的关系。

hoodlum1980 | 园豆:573 (小虾三级) | 2009-01-25 08:40
0
三角猫 | 园豆:105 (初学一级) | 2009-01-28 12:57
0

恭喜恭喜,说白了,来抢点分!

海洋——海纳百川,有容乃大. | 园豆:210 (菜鸟二级) | 2009-01-29 17:26
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册