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

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

问题描述

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



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



一小部分背景:应用程序应该感测插入一个usb'device'它),询问它,并创建另一个NotifyIcon('设备图标'),以允许用户与该设备交互。



为了封装这些设备交互功能,我在上一段中提到的'主应用程序图标'允许用户与数据库交互并配置软件。构造了一个设备类,
包含设备NotifyIcon,ContextMenu,窗体,鼠标点击事件等,允许与设备交互。



问题
当我从ManagementEventWatcher EventArrived事件中实例化我的设备类时,问题开始。如果我在我的程序Main中实例化'device class',那么当我单击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天全站免登陆