首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#CPU施用过高,求大神

2013-01-11 
C#CPU使用过高,求大神 private void xianshiyanse(){int i 0RECT rect new RECT()GetWindowRect(jub

C#CPU使用过高,求大神
 private void xianshiyanse()
        {
            int i = 0;
            RECT rect = new RECT();
            GetWindowRect(jubing, ref rect);
            KeyboardHookLib.HookStruct hookStruct = new KeyboardHookLib.HookStruct();

            tim.Interval = 1;
            tim.Tick += delegate
            {
                i++;
                if (i == 100)
                {
                    System.Threading.Thread.Sleep(50);
                    GC.Collect();
                    i = 0;
                }
                if (pictureBox1.BackgroundImage != null)
                {
                    pictureBox1.BackgroundImage.Dispose();
                }
                int x1 = rect.Left;
                int y1 = rect.Top;
                //Point P = new Point(x1, y1);
                Point P = new Point(MousePosition.X, MousePosition.Y);
                IntPtr h = GetDC(new IntPtr(0));        //指定坐标的句柄
                int c = GetPixel(h, P);//取指定点色彩
                int r = c % 256;
                int g = (c / 256) % 256;
                int b = c / 256 / 256;
                try
                {
                    pictureBox1.BackColor = Color.FromArgb(r, g, b);


                }
                catch
                {
                    System.Threading.Thread.Sleep(100);
                }
            };
            tim.Start();
        }




加了这个的话就出现内存不足,CPU过高的现状,延迟还有释放都用上了,还是没作用
[解决办法]
tim.Interval = 1
1毫秒一次。。。肯定高
[解决办法]
你这个根本没意义,用timer一样会阻塞,timer不是线程,你要用thread
[解决办法]
 tim.Interval = 1; //数值弄大点
 GC.Collect();//垃圾回收的太频繁了

热点排行