//C#语法
...
        bool isClose;
        public void MessageEngine()
        {
            while (isClose)
            {
                //(A处:)监视N个信号,(执行流将一直堵塞在这里,直到获得至少一个信号。
                //this.Do(message) 
            }
        } 
...
A处,一般有象Console.ReadLine();或windowsObj.ShowDialog()等,我想知道有没有更底层的命令 可以来完成A处,提出的需求,包括使用线程方案等,如果方便的话请不妨给出代码表述。

解决方案 »

  1.   

    要求越“底层”越好,不要用象ShowDialog() ,或 Console.ReadLine()之类 的
      

  2.   

    使用信号量啊,比如Mutex之类的
      

  3.   

    如果无法用C#的方案突进到windows消息循环本身,那我觉得还是不爽。换句话说,任何NET类库中的能完成的A处任务的方法或方案,都可能代表对一种消息循环本身的调用而没有机会深入其中。我的意思深入到象Console.ReadLine()这种方法所能达到的,让执行流,停下来等待某个信号输入的机制内部,这种机制可能对于C#来说不可触及。如果有办法能够达到这种机制的内部那么应该,怎么来达到,用什么方法,什么技术?
      

  4.   

    给Application添加消息过滤器来达到捡取消息的目的,方法:
    Application.AddMessageFilter(IMessageFilter value)
      

  5.   

    我的blog里有一个,就是自己创建个线程,然后实现了一个自己的消息机制的。效率是很高的。到底多高。我也说不上。反正几乎不占用cpu。模拟windows的messagequeue的原理来的。完全模拟。代码也少,也封装起来了。……
    ……
    ……不过就是现在blog打不开,csdn的问题。
      

  6.   

    #Region "自定义消息队列线程"
    Public Structure CustomMessage
        Public Message As Integer
        Public param As Object
    End StructurePublic Class CustomMessageQueue
        Private th As Threading.Thread
        Public Msg As New CustomMessage
        Public Delegate Function PerTranslateMessageHandler(ByRef m As CustomMessage) As Boolean
        Public PerTranslateMessage As PerTranslateMessageHandler    Public Shared Sub PostMessage(ByRef msgQueue As CustomMessageQueue, ByRef m As CustomMessage)
            msgQueue.Msg = m
            Threading.Monitor.Enter(msgQueue)
            Threading.Monitor.Pulse(msgQueue)
            Threading.Monitor.Exit(msgQueue)
        End Sub    Public Sub PostMessage(ByRef m As CustomMessage)
            Msg = m
            Threading.Monitor.Enter(Me)
            Threading.Monitor.Pulse(Me)
            Threading.Monitor.Exit(Me)
        End Sub    Public Sub PostQuitMessage()
            Msg.Message = -1
            Threading.Monitor.Enter(Me)
            Threading.Monitor.Pulse(Me)
            Threading.Monitor.Exit(Me)
        End Sub    Private Sub ThreadProc()
            While Msg.Message <> -1 'enum -1 for exit thread
                If (Msg.Message <> 0) Then
                    If Not PerTranslateMessage Is Nothing Then
                        If PerTranslateMessage.Invoke(Msg) Then
                            Msg.Message = 0 'Set message to unused
                            Threading.Monitor.Enter(Me)
                            Threading.Monitor.Wait(Me)
                            Threading.Monitor.Exit(Me)
                            Continue While
                        End If
                    End If
                    DefaultMessageTranslate()
                End If
                Threading.Monitor.Enter(Me)
                Threading.Monitor.Wait(Me)
                Threading.Monitor.Exit(Me)
            End While
        End Sub    Private Sub DefaultMessageTranslate()
            '以下可以定义默认的消息处理,可以封装成自己要用的   
            Select Case Msg.Message
                Case 1 '我自己定义,1表示显示消息号或消息的解释
                    If Not Msg.param Is Nothing OrElse TypeOf Msg.param Is String Then
                        MessageBox.Show(DirectCast(Msg.param, String))
                    Else
                        Dim strMsg As String = String.Format("{0:d}", Msg.Message)
                        MessageBox.Show(strMsg)
                    End If
            End Select
            Msg.Message = 0 'Set message to unused
        End Sub    Public Sub New()
            th = New Threading.Thread(AddressOf ThreadProc)
            PerTranslateMessage = Nothing
        End Sub    Public Sub StartThread()
            Try
                th.Start()
            Catch
                Dim nLayer As Integer = GC.GetGeneration(th)
                GC.Collect(nLayer)
                th = New Threading.Thread(AddressOf ThreadProc)
                th.Start()
            End Try
        End Sub
    End Class
    #End Region
    //这是那个带有自定义消息循环的类,下面贴调用方法
      

  7.   

    Public Class Form1
        Private myMsgQueue As CustomMessageQueue
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim msg As New CustomMessage
            msg.Message = 1
            msg.param = New String("我的自定义消息 ID:1")
            CustomMessageQueue.PostMessage(myMsgQueue, msg)
        End Sub    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim msg As New CustomMessage
            msg.Message = 2
            myMsgQueue.PostMessage(msg)
        End Sub    Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
            myMsgQueue.PostQuitMessage()
        End Sub    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            myMsgQueue = New CustomMessageQueue()
            myMsgQueue.StartThread()
            myMsgQueue.PerTranslateMessage = New CustomMessageQueue.PerTranslateMessageHandler(AddressOf CustomMessageProc)
        End Sub    Private Function CustomMessageProc(ByRef m As CustomMessage) As Boolean
            If m.Message = 1 Then
                MessageBox.Show("我拦截到 id = 1 的消息了。并且,就到此为止了。呵呵")
                Return True
            Else
                MessageBox.Show(m.Message.ToString)
            End If
            Return False
        End Function
    End Class
      

  8.   

    谢谢!,但能不能弄成C#啊,我看不懂VB  55555555555555555555
      

  9.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace CustomMessageQueue_CSharp
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private CustomMessageQueue myMsgQueue;
            private bool CustomMessageProc(ref CustomMessage m)
            {
                if (m.Message == 1)
                {
                    MessageBox.Show("我拦截到 id = 1 的消息了。并且,就到此为止了。呵呵");
                    return true;
                }
                else
                {
                    MessageBox.Show(m.Message.ToString());
                }
                return false;
            }        private void Button1_Click(object sender, EventArgs e)
            {
                CustomMessage msg = new CustomMessage();
                msg.Message = 1;
                msg.param = "我的自定义消息 ID:1";
                CustomMessageQueue.PostMessage(ref myMsgQueue, ref msg);
            }        private void Button2_Click(object sender, EventArgs e)
            {
                CustomMessage msg = new CustomMessage();
                msg.Message = 2;
                myMsgQueue.PostMessage(ref msg);
            }        private void Form1_Load(object sender, EventArgs e)
            {
                myMsgQueue = new CustomMessageQueue();
                myMsgQueue.StartThread();
                myMsgQueue.PerTranslateMessage = new CustomMessageQueue.PerTranslateMessageHandler(CustomMessageProc);
            }        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                myMsgQueue.PostQuitMessage();
            }
        }    #region 自定义消息队列线程
        public struct CustomMessage
        {
            public int Message;
            public Object param;
        }    public class CustomMessageQueue
        {
            private System.Threading.Thread th;
            public CustomMessage Msg = new CustomMessage();
            public delegate bool PerTranslateMessageHandler(ref CustomMessage m);
            public PerTranslateMessageHandler PerTranslateMessage;        public static void PostMessage(ref CustomMessageQueue msgQueue, ref CustomMessage m)
            {
                msgQueue.Msg = m;
                System.Threading.Monitor.Enter(msgQueue);
                System.Threading.Monitor.Pulse(msgQueue);
                System.Threading.Monitor.Exit(msgQueue);
            }        public void PostMessage(ref CustomMessage m)
            {
                Msg = m;
                System.Threading.Monitor.Enter(this);
                System.Threading.Monitor.Pulse(this);
                System.Threading.Monitor.Exit(this);
            }        public void PostQuitMessage()
            {
                Msg.Message = -1;
                System.Threading.Monitor.Enter(this);
                System.Threading.Monitor.Pulse(this);
                System.Threading.Monitor.Exit(this);
            }        private void ThreadProc()
            {
                while (Msg.Message != -1) //enum -1 for exit thread
                {
                    if (Msg.Message != 0)
                    {
                        if (PerTranslateMessage != null)
                        {
                            if (PerTranslateMessage.Invoke(ref Msg))
                            {
                                Msg.Message = 0; //Set message to unused
                                System.Threading.Monitor.Enter(this);
                                System.Threading.Monitor.Wait(this);
                                System.Threading.Monitor.Exit(this);
                                continue;
                            }
                        }
                        DefaultMessageTranslate();
                    }
                    System.Threading.Monitor.Enter(this);
                    System.Threading.Monitor.Wait(this);
                    System.Threading.Monitor.Exit(this);
                }
            }        private void DefaultMessageTranslate()
            {
                //以下可以定义默认的消息处理,可以封装成自己要用的   
                switch (Msg.Message)
                {
                    case 1: //我自己定义,1表示显示消息号或消息的解释
                        if (Msg.param != null)
                        {
                            if (Msg.param is string)
                            {
                                MessageBox.Show(Msg.param as string);
                            }
                        }
                        else
                        {
                            string strMsg = string.Format("{0:d}", Msg.Message);
                            MessageBox.Show(strMsg);
                        }
                        break;
                }
                Msg.Message = 0; //Set message to unused
            }        public CustomMessageQueue()
            {
                th = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProc));
                PerTranslateMessage = null;
            }        public void StartThread()
            {
                try
                {
                    th.Start();
                }
                catch
                {
                    int nLayer = GC.GetGeneration(th);
                    GC.Collect(nLayer);
                    th = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProc));
                    th.Start();
                }
            }
        }
        #endregion
    }