public Program() { int resolution = GetTimerResolution(); int lastTimerResolution = resolution; nfIcon.ContextMenuStrip = CreateContextMenu(); nfIcon.Icon = GetIcon(resolution); nfIcon.Visible = true; /* Invoke a 'click' on the first item of the context menu */ (nfIcon.ContextMenuStrip.Items[0] as ToolStripMenuItem).PerformClick(); /* Timer tick: update nfIcon */ timer.Tick += delegate { try { resolution = GetTimerResolution(); /* Update icon on timer change */ if (resolution != lastTimerResolution) { /* Destroy the previous Icon Handle to avoid a Handle leak */ WinApiCalls.DestroyIcon(nfIcon.Icon.Handle); /* Update icon */ nfIcon.Icon = GetIcon(resolution); /* Save last timer */ lastTimerResolution = resolution; } } catch (Exception e) { nfIcon.ShowBalloonTip(5000, "Error", e.ToString(), ToolTipIcon.Error); }; }; }
private int GetTimerResolution() { /* Query the timer resolution from Windows API */ TimerCaps caps = WinApiCalls.QueryTimerResolution(); return((int)(caps.PeriodCurrent / 10000.0)); }