簡體   English   中英

在沒有觸發的Windows窗體應用程序上單擊與NotifyIcon關聯的ContextMenu的MenuItem上的事件,但需要再單擊一次圖標才能工作

[英]Click event on MenuItem of a ContextMenu associated to NotifyIcon on a Windows form application not firing but requires one more click on icon to work

我已經創建了一個應用程序,使用異步套接字編程從客戶端獲取數據。 用戶可以在tasktray中將應用程序視為圖標,並可以使用托盤圖標上的右鍵菜單“關閉”應用程序。 為此,我編寫了一個Windows窗體應用程序,它是一個隱形的窗體應用程序,它與一個始終可見的通知圖標相關聯。 在notifyicon中添加了一個contextmenu並向用戶顯示一個'close'選項作為contextmenu menuitem。

右鍵單擊托盤圖標時,上下文菜單顯示正常。 但是當單擊contextmenu的menuitem時,應用程序不會按編碼關閉。 它需要再次右鍵單擊托盤圖標(單擊關閉選項后),然后關閉所有資源。 請查找相關代碼,如下 -

public partial class Form1 : Form
{
    private ContextMenu m_menu;
    public static ManualResetEvent allDone = new ManualResetEvent(false);
    public Form1()
    {
        InitializeComponent();
        m_menu = new ContextMenu();
        m_menu.MenuItems.Add(0, new MenuItem("Close", new System.EventHandler(menuItem1_Click)));
        notifyIcon1.ContextMenu =this.m_menu;
        notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);
        // Initiate listening for sockets
        StartListening();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    // State object for reading client data asynchronously
    public class StateObject
    {
        // Client socket, size of receive buffer, receive buffer and received data string.
        public Socket workSocket = null;
        public const int BufferSize = 1024;
        public byte[] buffer = new byte[BufferSize];
        public StringBuilder sb = new StringBuilder();
    }

    // socket program goes here, with msdn AcceptCallback and ReceiveCallback

    public static void StartListening()
    {              // Create a TCP/IP socket.
            string port = ConfigurationManager.AppSettings["port"];
            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, Convert.ToInt32(port));
            Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        // Bind the socket to the local endpoint and listen for incoming connections.
        try
            {
                listener.Bind(localEndPoint);
                listener.Listen(1);

            while (true)
                {
                    // Set the event to nonsignaled state.
                    allDone.Reset();
                    // Start an asynchronous socket to listen for connections.
                    listener.BeginAccept(new AsyncCallback(AcceptCallback),listener);
                    // Wait until a connection is made before continuing.
                    allDone.WaitOne();
                }
            }
            catch (Exception ex)
            {
        }
    }

        // AsyncResult tells status of asynchronous operation
    private static void AcceptCallback(IAsyncResult AR)
    {
            // Signal the main thread to continue.
            allDone.Set();
            // handler is the socket to accept incoming connection and create socket to handle remote host communications
            // Get the socket that handles the client request.
            Socket listener = (Socket)AR.AsyncState;
            Socket handler = listener.EndAccept(AR);
            // Create the state object.
            StateObject state = new StateObject();
            state.workSocket = handler;
            handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                 new AsyncCallback(ReceiveCallback), state);
    }


    private void menuItem1_Click(Object sender, System.EventArgs e)
    {
        this.Close();
        // Application.Exit();
        Environment.Exit(0);
    }

    private void notifyIcon1_DoubleClick(object sender, EventArgs e)
    {
        Show();
        ShowInTaskbar = true;
    }
}

我懷疑它可能是一個多線程問題,但我是編程新手,無法精確定位。 任何有關如何解決此問題的線索都非常感謝。

它適用於以下代碼

 private void menuItem1_Click(Object sender, System.EventArgs e)
    {
     Application.ExitThread();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM