View difference between Paste ID: zNfREPSB and rczHbGp7
SHOW: | | - or go back to the newest paste.
1
using System;
2
using System.ComponentModel;
3
using System.Diagnostics;
4
using System.Drawing;
5
using System.Threading;
6
using System.Windows.Forms;
7
8
namespace Systraytest
9
{
10
    public partial class Form1 : Form, IDisposable
11
    {
12
        private readonly NotifyIcon _icon;
13
        private readonly ContextMenu _trayMenu;
14
        private BackgroundWorker _worker;
15
16
        private readonly MenuItem _startItem;
17
        private readonly MenuItem _stopItem;
18
        private readonly MenuItem _exitItem;
19
20
        public Form1()
21
        {
22
            InitializeComponent();
23
24
            _startItem = new MenuItem("Start", OnStartClicked);
25
            _stopItem = new MenuItem("Stop", OnStopClicked);
26
            _exitItem = new MenuItem("Exit", OnExitClicked);
27
28
            _trayMenu = new ContextMenu();
29
            SetupTrayMenu(false);
30
31
            _icon = new NotifyIcon();
32
            _icon.Text = "SysTrayTest";
33
            _icon.Icon = new Icon(SystemIcons.Application, 40, 40);
34
            _icon.ContextMenu = _trayMenu;
35
            _icon.Visible = true;
36
            SetupWorker();
37
        }
38
39
        private void SetupWorker()
40
        {
41
            _worker = new BackgroundWorker();
42
            _worker.WorkerSupportsCancellation = true;
43
            _worker.DoWork += (s, ea) =>
44
            {
45
                var worker = s as BackgroundWorker;
46
47
                Debug.WriteLine("BWorker started!");
48
                while (!worker.CancellationPending)
49
                {
50
                    Debug.WriteLine("PFFFT");
51
                    Thread.Sleep(1000);
52
                }
53
54
                Debug.WriteLine("BWorker canceled!");
55
            };
56
57
            _worker.RunWorkerCompleted += (s, ea) =>
58
            {
59
                Debug.WriteLine("BWorker completed!");
60
                SetupTrayMenu(false);
61
            };
62
        }
63
64
        private void SetupTrayMenu(bool running)
65
        {
66
            _trayMenu.MenuItems.Clear();
67
            _trayMenu.MenuItems.Add(running ? _stopItem : _startItem);
68
            _trayMenu.MenuItems.Add(_exitItem);
69
        }
70
71
        private void OnStartClicked(object sender, EventArgs e)
72
        {
73
            _worker.RunWorkerAsync();
74
            SetupTrayMenu(true);
75
        }
76
77
        private void OnStopClicked(object sender, EventArgs e)
78
        {
79
            _worker.CancelAsync();
80
        }
81
82
        private void OnExitClicked(object sender, EventArgs e)
83
        {
84
            if (_worker.IsBusy && !_worker.CancellationPending)
85
            {
86
                _worker.CancelAsync();
87
            }
88
89
            Application.Exit();
90
        }
91
92
        protected override void OnLoad(EventArgs e)
93
        {
94
            Visible = false;
95
            ShowInTaskbar = false;
96
            base.OnLoad(e);
97
        }
98
    }
99
}