在 Windows 表单应用程序上与通知图标关联的上下文菜单的菜单项上的单击事件未触发,但需要再单击一次图标才能工作
本文关键字:单击 图标 工作 一次 事件 程序上 应用程序 应用 Windows 表单 通知 | 更新日期: 2023-09-27 18:32:55
我创建了一个应用程序,使用异步套接字编程从客户端获取数据。用户可以将应用程序视为任务托盘中的图标,并可以使用"关闭"应用程序右键单击托盘图标上的菜单。为此,我编写了一个Windows表单应用程序,使表单不可见,关联一个可见的通知图标总是。在通知图标中添加了一个上下文菜单,并将"关闭"选项显示为上下文菜单菜单项给用户。
右键单击托盘图标时,上下文菜单显示正常。但是当单击上下文菜单的菜单项时,应用程序不会关闭编码。它需要再次右键单击托盘图标(单击关闭选项后),然后关闭所有资源。请在下面找到相关代码-
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();
}