private void OnBackgroundTaskChanged(object sender, EventArgs e) { if(sender is ToolStripMenuItem) { activeTask = ((ToolStripMenuItem)sender).Tag as BackgoundTask; } else { activeTask = sender as BackgoundTask; } // show the controls TaskList.Visible = true; TaskStopList.Visible = true; TaskProgress.Visible = true; TaskList.Text = activeTask.Name; TaskList.Image = activeTask.Image; TaskProgress.Style = activeTask.ProgressStyle; TaskProgress.Value = (int)Math.Max(TaskProgress.Minimum, Math.Min(TaskProgress.Maximum, activeTask.ProgressValue)); }
public BackgoundTask RegisterBackgroundTask(string name, Image image, ProgressBarStyle progressStyle, int progressValue) { Debug.AssertNotNull(name, "Name is null"); BackgoundTask task = new BackgoundTask(); task.Name = name; task.Image = image; task.ProgressStyle = progressStyle; task.ProgressValue = progressValue; task.OnProgressChanged += OnBackgroundTaskProgressChanged; task.OnStopped += OnBackgroundTaskStopped; backgoundTasks.Add(task); ToolStripMenuItem menuItem = new ToolStripMenuItem(task.Name, task.Image, OnBackgroundTaskChanged); menuItem.Tag = task; TaskList.DropDownItems.Add(menuItem); task.AttachedControls.Add(new KeyValuePair<ToolStripMenuItem, ToolStripDropDownItem>(menuItem, TaskList)); ToolStripMenuItem stopMenuItem = new ToolStripMenuItem("Stop " + task.Name, task.Image, OnBackgroundTaskStop); stopMenuItem.Tag = task; TaskStopList.DropDownItems.Add(stopMenuItem); task.AttachedControls.Add(new KeyValuePair<ToolStripMenuItem, ToolStripDropDownItem>(stopMenuItem, TaskStopList)); OnBackgroundTaskChanged(task, null); return task; }