System.Drawing.dll中的System.Runtime.InteropServices.ExternalException

时间:2016-04-23 20:08:11

标签: c# winforms notifyicon

我有一个更新通知图标(“niCount”)的小班:

using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

namespace Test
{
    public partial class MainForm : Form
    {
        private int i = 0;

        public MainForm ()
        {
            InitializeComponent ();
        }

        private void Form1_Shown (object sender, EventArgs e)
        {
            bwUpdate.RunWorkerAsync ();
        }

        private void bwUpdate_DoWork (object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            Bitmap bmp = new Bitmap (16, 16);

            Graphics g = Graphics.FromImage (bmp);
            Pen p = new Pen (Color.Red);
            g.DrawLine (p, 0, 0, i % 16, i % 16);

            niCount.Icon = Icon.FromHandle (bmp.GetHicon ());

            bmp.Dispose ();
            p.Dispose ();
            g.Dispose ();
        }

        private void bwUpdate_RunWorkerCompleted (object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            i++;
            Text = i.ToString ();

            Thread.Sleep (1);

            bwUpdate.RunWorkerAsync ();
        }
    }
}

这可以按预期工作,但只要计数器“i”达到大约3400,就会产生以下异常(每个DoWork调用一次):

System.Runtime.InteropServices.ExternalException in System.Drawing.dll

这与Thread.Sleep值无关。在此之后,后台工作人员仍然工作并且计数器计数,但通知图标不再获得更新。怎么了?

0 个答案:

没有答案