NotifyIcon事件不触发 [英] NotifyIcon Events not firing

查看:166
本文介绍了NotifyIcon事件不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的,有一个真正神秘的问题,开始。我是英国的一名软件开发人员,拥有超过15年的经验,但只在.NET开发了18个月。我的NotifyIcon鼠标事件没有触发!



我正在使用C#编写一个以NotifyIcon(主应用程序图标)开头的应用程序,并在鼠标右键显示一个ContextMenu -Click。这个工作正常:ContextMenu,形成启动和鼠标点击事件触发。



一点点的背景:应用程序应该感觉到一个usb设备的插入(它),询问它并创建另一个NotifyIcon('设备图标'),以允许用户与该设备进行交互。我上一段提到的主应用程序图标允许用户与数据库进行交互并配置软件。



为了封装这些设备交互功能,我已经构建了一个'设备类',
包含允许与设备交互的设备NotifyIcon,ContextMenu,表单,鼠标点击事件等。



问题
当我从ManagementEventWatcher EventArrived事件中实例化设备类时,问题开始。如果我在我的程序Main中实例化设备类,那么当我点击notifyicon时,事件会正常启动。



所以,有人可以帮我吗? >

提前干杯



Matthew

解决方案

IIRC,事件使用(而不是 WaitForNextEvent )工作异步。我有兴趣知道事件正在发生的线程。我想知道您的图标是否没有消息泵服务消息。



您是否在任何地方使用表单?还是有消息循环的其他东西?我会尝试使用表单(使用 Control.Invoke ),并请求表单显示图标 - 因为它应该有一个有效的消息泵。






对不起延迟;阅读你的意见,听起来你已经有了一个解决方法。唯一的问题是跨线程的问题;理想情况下,您会要求UI在UI线程上进行这样的更改 ;例如,如果您有一个表单踢(拥有图标) - 添加到您的表单类:

  //不是属性,因为没有必要添加一个复杂的x线程get
public void SetIconVisible(bool isVisible){
if(this。 InvokeRequired){
this.Invoke((MethodInvoker)delegate {
myIcon.Visible = isVisible;
});
} else {
myIcon.Visible = isVisible;
}
}

这样做一个线程切换(如果需要) UI线程。任何用途?


I'm new here and have a really mysterious problem to start off. I'm a software developer in the UK and have over 15 years of experience but have only been developing in .Net for 18 months. My NotifyIcon mouse events are not firing!

I am using C# to write an application that starts as a NotifyIcon ('main app icon') and displays a ContextMenu on mouse right-click. This works fine: ContextMenu, forms launching and mouse click events firing.

A tiny bit of background: the application is supposed to sense the insertion of a usb 'device' (it does), interrogate it and create another NotifyIcon ('device icon') to allow the user to interact with that device. The 'main app icon', mentioned in my previous paragraph, allows the user to interact with the database and to configure the software.

To encapsulate these device interaction functions, I have constructed a 'device class' that contains the device NotifyIcon, ContextMenu, forms, mouse click events etc which allow interaction with the device.

The Problem The problem starts when I instantiate my 'device class' from the ManagementEventWatcher EventArrived event. If I instantiate the 'device class' in my program Main then the events fire correctly when I click the notifyicon.

So, please, can someone help me?

Cheers, in advance

Matthew

解决方案

IIRC, the event usage (rather than WaitForNextEvent) works async. I would be interested to know what thread the event is being raised on. I wonder if there is no message pump servicing messages for your icon.

Do you have a form anywhere? Or something else with a message loop? I would be tempted to call into the form (using Control.Invoke), and ask the form to show the icon - since then it should have an active message pump.


Sorry for the delay; reading your comments, it sounds like you've broadly got a workaround. The only gotcha is cross-threaded issues; ideally, you would ask the UI to make such a change on the UI thread; for example, if you have a Form kicking around (owning the icon) - add to your form class:

// not a property, as there is no need to add a complex x-thread "get"
public void SetIconVisible(bool isVisible) {
    if(this.InvokeRequired) {
        this.Invoke((MethodInvoker) delegate {
            myIcon.Visible = isVisible;
        });
    } else {
        myIcon.Visible = isVisible;
    }
}

This does a thread-switch (if needed) to the UI thread. Any use?

这篇关于NotifyIcon事件不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆