C# NotifyIcon+;通知窗口

C# NotifyIcon+;通知窗口,c#,notifyicon,C#,Notifyicon,我正在使用NotifyIcon和通知窗口()。所有工作正常,但我没有设法显示通知窗口,窗口出现,但没有任何内容,如果我点击它的应用程序崩溃。这是我的密码: namespace test { public class NotificationIcon : ServiceBase { private static readonly ILog Logger = LogManager.GetLogger (typeof(NotificationIcon));

我正在使用NotifyIcon和通知窗口()。所有工作正常,但我没有设法显示通知窗口,窗口出现,但没有任何内容,如果我点击它的应用程序崩溃。这是我的密码:

namespace test
{
    public class NotificationIcon : ServiceBase
    {
        private static readonly ILog Logger = LogManager.GetLogger (typeof(NotificationIcon));

        private NotifyIcon notifyIcon;
        private ContextMenu notificationMenu;

        #region Initialize icon and menu
        public NotificationIcon(Boolean isService)
        {
            if(isService)
            {
                ....

            } else
            {
                Logger.Info("Start");

                notifyIcon = new NotifyIcon();
                notifyIcon.Visible = true;
                notificationMenu = new ContextMenu(InitializeMenu());

                popup = new NotificationWindow.PopupNotifier();

                ComponentResourceManager resources = new ComponentResourceManager(typeof(NotificationIcon));

                notifyIcon.ContextMenu = notificationMenu;
                notifyIcon.DoubleClick += new EventHandler(doubleClick);

                Thread t = new Thread(notificationThread);
                t.IsBackground = true;
                t.SetApartmentState(ApartmentState.STA);
                t.Start();
            }
        }      

        private NotificationWindow.PopupNotifier popup;


        private void notificationThread()
        {
            NamedPipeServerStream server = new NamedPipeServerStream("testpipe", PipeDirection.InOut);

            while (true)
            {
                server.WaitForConnection();
                StreamReader reader = new StreamReader(server);

                try
                {

                    while(true)
                    {

                        if(!tokens[4].Equals("A") && !tokens[4].Equals("B"))
                        {
                            showTip(tokens[4], 
                                    tokens[2] + tokens[3]);
                        }
                    }

                } catch (Exception e)
                {
                     Logger.Error("Unable to receive pipe message from client", e);

                     // Create a new pipe stream
                     server.Close();
                     server = new NamedPipeServerStream("testpipe", PipeDirection.InOut);

                } finally
                {
                    if(server.IsConnected) { server.Disconnect(); }
                }
            }    
        }

        private void showTip(String title, String msg)
        {
            //notifyIcon.ShowBalloonTip(5000, title, msg, ToolTipIcon.Info);

            Logger.Info("Show popup");

            popup.TitleText = title;
            popup.ContentText = msg;

            // This kills the application
            popup.Popup();  

            Logger.Info("Done");
        }

        #region Main - Program entry point
        /// <summary>Program entry point.</summary>
        /// <param name="args">Command Line Arguments</param>
        [STAThread]
        public static void Main(string[] args)
        {

            try
            {

                if (Environment.UserInteractive)
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);

                    NotificationIcon notificationIcon = new NotificationIcon(false);
                    Application.Run()
                    notificationIcon.notifyIcon.Dispose();

                } else
                {
                    Logger.Info("Start Service");

                    ServiceBase.Run(new NotificationIcon(true));
                }

            } catch (Exception e)
            {
                Logger.Error("Error during service start", e);
            }   
        }
    }
}
名称空间测试
{
公共类通知图标:ServiceBase
{
私有静态只读ILog Logger=LogManager.GetLogger(typeof(NotificationIcon));
私有通知图标通知图标;
私有上下文菜单通知菜单;
#区域初始化图标和菜单
公共通知图标(布尔isService)
{
如果(iService)
{
....
}否则
{
Logger.Info(“开始”);
notifyIcon=新的notifyIcon();
notifyIcon.Visible=true;
notificationMenu=新建上下文菜单(InitializeMenu());
popup=新建NotificationWindow.PopupNotifier();
ComponentResourceManager资源=新的ComponentResourceManager(typeof(NotificationIcon));
notifyIcon.ContextMenu=通知菜单;
notifyIcon.DoubleClick+=新事件处理程序(DoubleClick);
螺纹t=新螺纹(notificationThread);
t、 IsBackground=true;
t、 SetApartmentState(ApartmentState.STA);
t、 Start();
}
}      
private NotificationWindow.PopupNotifier弹出窗口;
私有void notificationThread()
{
NamedPipeServerStream服务器=新的NamedPipeServerStream(“testpipe”,PipeDirection.InOut);
while(true)
{
WaitForConnection();
StreamReader=新的StreamReader(服务器);
尝试
{
while(true)
{
如果(!tokens[4].Equals(“A”)和(&!tokens[4].Equals(“B”))
{
showTip(代币[4],
代币[2]+代币[3];
}
}
}捕获(例外e)
{
Logger.错误(“无法从客户端接收管道消息”,e);
//创建新的管道流
server.Close();
server=新名称pipeserverstream(“testpipe”,PipeDirection.InOut);
}最后
{
如果(server.IsConnected){server.Disconnect();}
}
}    
}
私有void showTip(字符串标题、字符串消息)
{
//notifyIcon.showBallootTip(5000,title,msg,ToolTipIcon.Info);
Logger.Info(“显示弹出窗口”);
popup.TitleText=标题;
popup.ContentText=msg;
//这将终止应用程序
popup.popup();
Logger.Info(“完成”);
}
#区域主-程序入口点
///程序入口点。
///命令行参数
[状态线程]
公共静态void Main(字符串[]args)
{
尝试
{
if(Environment.UserInteractive)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
NotificationIcon NotificationIcon=新的NotificationIcon(假);
Application.Run()
notificationIcon.notifyIcon.Dispose();
}否则
{
Logger.Info(“启动服务”);
Run(newnotificationicon(true));
}
}捕获(例外e)
{
记录器错误(“服务启动期间的错误”,e);
}   
}
}
}
如果在popup()调用之后添加一个while(true)Application.DoEvents(),我得到了一个部分解决方法。但是我想要一个干净的解决方案