static  WeakReference wr = new WeakReference(null);
        private void SetTheCsOnlineSight()
        {
            try
            {
                if (!isReult)
                {
                    return;
                }
                IntPtr hscrdc = GetWindowDC(csHwnd);//调用api
                Rectangle rect = new Rectangle();
                GetClientRect(csHwnd, ref rect);//调用api获得窗口信息
                Graphics g = wr.Target as Graphics; //Graphics.FromHdc(hscrdc);
                int height = 0;
                if (g == null)
                {
                    g = Graphics.FromHdc(hscrdc);
                    wr.Target = g;
                }
                if (rdWindow.Checked)
                {
                    //窗口化时需要加上任务栏高度
                    height = rect.Height / 2 + (Screen.PrimaryScreen.Bounds.Height - Screen.PrimaryScreen.WorkingArea.Height) + u;
                }
                if (rdAllScreen.Checked)
                {
                    height = rect.Height / 2 + u;
                }
                g.DrawEllipse(new Pen(color), rect.Width / 2 + l, height, w, h);//准星大小
                g.FillEllipse(new SolidBrush(color), (rect.Right - rect.Left) / 2 + l, height, fw, fh);//填充为实型 
            }
            catch { }
            finally
            {
                GC.Collect();
            }
        }改成弱引用还是会内存溢出,求高手解答,在线等.

解决方案 »

  1.   

    你调用的API是非托管的么?如果是他是不是提供了清理方法让你调用?
      

  2.   

    ::closehandle(hscrdc );VC下面用这个。
    marsh.free(hscrdc );
      

  3.   

    .net的程序为什么要这么用。
    graphics g=this.creategraphics();就可以了。
      

  4.   

    系统API呀
    只是那个画圆的,内存一直增长,最后就内存溢出
      

  5.   

    我这个是要在指定的窗口上画圆,并不是程序本身上的窗口,所以才Graphics.FromHdc(hscrdc);
      

  6.   

    我这个是要在指定的窗口上画圆,并不是程序本身上的窗口,所以才Graphics.FromHdc(hscrdc);
      

  7.   

    http://baike.baidu.com/view/1080548.htm
      

  8.   

    在windows下释放资源,用这个。//此处为释放内存资源,去掉试试看?
        EndHandle := GetCurrentProcess();
        if GetProcessWorkingSetSize(EndHandle, currentMinWorkingSetValue, currentMaxWorkingSetValue) then
        begin
            SetProcessWorkingSetSize(EndHandle, currentMinWorkingSetValue, currentMaxWorkingSetValue);
        end;
      

  9.   

    GetProcessWorkingSetSize()
    SetProcessWorkingSetSize()
    把这两个函数在c#里定义了,然后调用看看。另外,如果你是在别的窗口上画图,一定要找到别的程序的句柄,
    EndHandle := GetCurrentProcess();
    就是这句话,必须是别的程序的进程句柄。
    你现在光释放你程序的资源,所以会出错