English 中文(简体)
在单独的线索上显示 WPF - “通知Icon”
原标题:Displaying WPF-"NotifyIcon" on a separate thread

我正在做一个办公室添加, 我需要显示一个显示进度的通知对话框, 我使用 < a href=" http://www.codeproject.com/ articles/ 36468/WPF- 通知Icon" rel=“ nofollow” > Philipp Sumi s wpf- notifificon 。

我需要显示 < a href=>" "http://www.codeproject.com/ artss/ 3646/68/WPF-通知Icon" rel="nofollow" >nofoltiticon 从一条单独的线索中显示 < a href=", 因为我有很多代码已经在主线上执行, 这就导致 wpf- notificiticon 屏蔽和等待, 因为窗口信息队列中的信息没有被处理 。

我知道,我宁愿在一条单独的线上执行这个消耗时间的代码,从主线上显示通知并相应更新,但不幸的是,这不是一个替代办法,因为整个解决方案都是单行读取的。

示例:

    private FancyPopup fancyPopup;

    private void button1_Click(object sender, EventArgs e)
    {
        notifyIcon = new TaskbarIcon();
        notifyIcon.Icon = Resources.Led;

        fancyPopup = new FancyPopup();

        Thread showThread = new Thread(delegate()
        {
            notifyIcon.ShowCustomBalloon(fancyPopup, System.Windows.Controls.Primitives.PopupAnimation.Fade, null);
        });

        showThread.Start();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        fancyPopup.TextB.Text = "Doing something...";

        //Keep the main thread busy.
        Thread.Sleep(5000);

        fancyPopup.TextB.Text = "Done doing something...";
    }

Update I have been able to progress a little further with this updated code:

我在新线索上创建任务barIcon 对象, 并使用 application.Run 处理该线索上的应用程序消息环...

    private FancyPopup fancyPopup;

    private void button1_Click(object sender, EventArgs e)
    {
        Thread showThread = new Thread(delegate()
        {
            notifyIcon = new TaskbarIcon();
            notifyIcon.Icon = Resources.Led;

            fancyPopup = new FancyPopup();


            notifyIcon.ShowCustomBalloon(fancyPopup, System.Windows.Controls.Primitives.PopupAnimation.Fade, null);

            System.Windows.Forms.Application.Run();
        });

        showThread.SetApartmentState(ApartmentState.STA);
        showThread.Start();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        fancyPopup.Dispatcher.Invoke(new Action(delegate
        {
            fancyPopup.TextB.Text = "Doing something...";
        }));

        //Keep the main thread busy.
        Thread.Sleep(5000);

        fancyPopup.Dispatcher.Invoke(new Action(delegate
        {
            fancyPopup.TextB.Text = "Done doing something..."; 
        }));
    }
最佳回答

我解决了自己的问题,我不得不在单独的STA线上初始化“强”通知Icon

        var myThread = new Thread(delegate()
        {
            notifyIcon = new NotifyIcon();

            Application.Run();
        });

        myThread.SetApartmentState(ApartmentState.STA);
        myThread.Start();

然后,我只好调用我的通知对话框的UI。

问题回答

暂无回答




相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签