1

次のアプリは、システム トレイ アイコンにディスク アクティビティを表示します。約 30 ~ 40 分間正常に実行された後、終了し、デスクトップにアイコンが残ります。不要なバックグラウンド タスクとしてシステムによって強制終了されているかのようです。なぜこれが起こっているのですか、どうすればそれを防ぐことができますか?

public partial class MainWindow : Window
{
    public System.Windows.Forms.NotifyIcon ni = new System.Windows.Forms.NotifyIcon();
    public MainWindow()
    {
        InitializeComponent();

        ni.Visible = true;
        ni.Text = "disktray"; // tooltip text show over tray icon
        CreateTextIcon("0");
        ni.DoubleClick +=
            delegate (object sender, EventArgs args)
            {
                //this.Show();
                //this.WindowState = WindowState.Normal;
                ni.Visible = false;
                ni.Dispose();
                System.Windows.Application.Current.Shutdown();
            };
    }
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        CreateTextIcon("0");
        DispatcherTimer timer = new DispatcherTimer()
        {
            Interval = TimeSpan.FromMilliseconds(1024)
        };
        timer.Tick += Timer_Tick;
        timer.Start();

        this.Hide();
    }
    private void Timer_Tick(object sender, EventArgs e)
    {
        Diskpercent();
        string iii = diskpercentvalue.ToString();
        CreateTextIcon(iii);
    }
    public PerformanceCounter myCounter =
       new PerformanceCounter("PhysicalDisk", "% Disk Time", "_Total");
    public int diskpercentvalue = 0;
    public void Diskpercent()
    {
        var d = Convert.ToInt32(myCounter.NextValue());
        if (d > 99) d = 99; // can go over 100%
        diskpercentvalue = d;
    }

    public System.Drawing.Font fontToUse = 
        new System.Drawing.Font("Microsoft Sans Serif", 16, System.Drawing.FontStyle.Regular, GraphicsUnit.Pixel);
    public System.Drawing.Brush brushToUse = new SolidBrush(System.Drawing.Color.White);
    public Bitmap bitmapText = new Bitmap(16, 16);
    public IntPtr hIcon;
    public void CreateTextIcon(string str)
    {
        //System.Drawing.Font fontToUse = new System.Drawing.Font("Microsoft Sans Serif", 16, System.Drawing.FontStyle.Regular, GraphicsUnit.Pixel);
        //System.Drawing.Brush brushToUse = new SolidBrush(System.Drawing.Color.White);
        //Bitmap bitmapText = new Bitmap(16, 16);
        Graphics g = System.Drawing.Graphics.FromImage(bitmapText);
        //IntPtr hIcon;
        g.Clear(System.Drawing.Color.Transparent);
        g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
        g.DrawString(str, fontToUse, brushToUse, -4, -2);
        hIcon = (bitmapText.GetHicon());
        ni.Icon = System.Drawing.Icon.FromHandle(hIcon);
    }

}
4

2 に答える 2