0
votes

I have a problem which i can't solve. I wrote an application which works in tray. Everything works fine but when i click right button (on tray icon) context menu open and then i click left button (on menu item) to execute the method, but then the context menu doesn't hide until this method is finish and stay on the top of the screen. Could you tell me how can i hide this context menu after click immediately ? My XAML code for TaskbarIcon:

  <tb:TaskbarIcon Name="Tray" IconSource="ico.ico" ToolTipText="text" Visibility="Visible" TrayLeftMouseUp="ShowWindow_Click">
            <tb:TaskbarIcon.ContextMenu>
                <ContextMenu Name="ContextMenu">
                    <ContextMenu.Style>
                        <Style TargetType="{x:Type ContextMenu}">
                            <Setter Property="ItemsPanel" Value="{StaticResource MenuTPL}"/>
                        </Style>
                    </ContextMenu.Style>
                    <MenuItem x:Name="ShowWindow" Click="ShowWindow_Click" Header="show"></MenuItem>
                    <MenuItem x:Name="GetPoints" Click="Run_Click" Header="points"></MenuItem>
                    <MenuItem x:Name="Finish" Click="Zakoncz_Click" Header="exit"></MenuItem>
                </ContextMenu>
            </tb:TaskbarIcon.ContextMenu>
        </tb:TaskbarIcon>
1

1 Answers

0
votes

If you have a time consuming method like in your case, you need to execute it on another thread. Otherwise the GUI (in your case the context menu) freezes because the time consuming task is executed on the UI-thread. To solve this you could for example use a BackgroundWorker.

Here I provide you a full working example for one way how you could do it:

XAML:

<Window x:Class="WpfApplication27.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:tb="http://www.hardcodet.net/taskbar"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <tb:TaskbarIcon Name="Tray" IconSource="ico.ico" ToolTipText="text" Visibility="Visible">
            <tb:TaskbarIcon.ContextMenu>
                <ContextMenu Name="ContextMenu">
                    <MenuItem x:Name="GetPoints" Click="Run_Click" Header="points"></MenuItem>
                </ContextMenu>
            </tb:TaskbarIcon.ContextMenu>
        </tb:TaskbarIcon>
    </Grid>
</Window>

CodeBehind:

public partial class MainWindow : Window
{
    private readonly BackgroundWorker _worker;

    public MainWindow()
    {
        InitializeComponent();
        _worker = new BackgroundWorker();
        _worker.DoWork += worker_DoWork;
    }


    private void Run_Click(object sender, RoutedEventArgs e)
    {   
        _worker.RunWorkerAsync();

    }

    private void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        //here is your time consuming task 
        for (int i = 0; i < 5000; i++)
        {
            Console.WriteLine(i + "Hello World");
        }
    }
}