【一百分结贴】这段代码无法释放内存吗?
asp.net程序里增加了这段代码后,服务器内存不断增加,直至程序池内存超限回收!我不太懂程序,查了很久也不见效,请高手指教(替换前后的全部程序代码在这里):
WebBrowser m_WebBrowser = null;
private void _GenerateHtmlToImgImage()
{
m_WebBrowser = new WebBrowser();
System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
t.Enabled = false;
t.Interval = 10000;
t.Tick += t_Tick;
m_WebBrowser.ScrollBarsEnabled = false;
t.Enabled = true;
m_WebBrowser.Navigate(m_Url);
m_WebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
while (m_WebBrowser != null && m_WebBrowser.ReadyState != WebBrowserReadyState.Complete)
Application.DoEvents();
if (m_WebBrowser != null)
m_WebBrowser.Dispose();
m_WebBrowser = null;
}
void t_Tick(object sender, EventArgs e)
{
((System.Windows.Forms.Timer)sender).Stop();
WebBrowser tmp = m_WebBrowser;
if (tmp != null)
{
m_WebBrowser = null;
if (tmp.ReadyState != WebBrowserReadyState.Complete)
{
tmp.Stop();
tmp.Dispose();
}
}
WebBrowser tmp = m_WebBrowser;
if (tmp != null)
{
m_WebBrowser = null;
if (tmp.ReadyState != WebBrowserReadyState.Complete)
{
tmp.Stop();
tmp.Dispose();
}
}
if (m_WebBrowser != null)
{
if (m_WebBrowser.ReadyState != WebBrowserReadyState.Complete)
{
m_WebBrowser.Stop();
}
m_WebBrowser.Dispose();
m_WebBrowser = null;
}
public Bitmap GenerateHtmlToImgImage()
{
Thread m_thread = new Thread(new ThreadStart(_GenerateHtmlToImgImage));
m_thread.SetApartmentState(ApartmentState.STA);
m_thread.Start();
bool success = m_thread.Join(15000);//考虑到处理图片也要时间,增加5秒
if(success)
return m_Bitmap;
else
{
m_thread.Abort();//終止线程
throw new Exception("操作超时");//这里返回的异常,由外部调用者捕获并做其它处理。
}
return m_Bitmap;
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
using(Bitmap bmp = HtmlToImg.GetHtmlToImg("http://www.baidu.com/", 800, 600, 800, 600))
{
bmp.Save(Server.MapPath(DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
catch(Exception ex)
{
HttpResponse response = HttpContext.Current.Response;
response.Write(ex.Message);
response.Close();
}
}
public class GHtmlToImage
{
/// <summary>
/// 地址
/// </summary>
private string _url = "about:blank";
/// <summary>
/// 宽
/// </summary>
private int _width = 800;
/// <summary>
/// 高
/// </summary>
private int _height = 600;
/// <summary>
/// 缩略图宽
/// </summary>
private int _swidth = 320;
/// <summary>
/// 缩略图高
/// </summary>
private int _sheight = 250;
private Bitmap _image;
private Bitmap _thumbnailimage;
private object _lock = new object();
private object _lock2 = new object();
public string Url
{
set { _url = value; }
}
public int Width
{
set { _width = value; }
}
public int Height
{
set { _height = value; }
}
public int SWidth
{
set { _swidth = value; }
}
public int SHeight
{
set { _sheight = value; }
}
public Bitmap Image
{
get { return _image; }
}
public Bitmap ThumbnailImage
{
get { return _thumbnailimage; }
}
public string DocText
{
get;
set;
}
public int runtime
{
get;
set;
}
public GHtmlToImage()
{
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
}
/*
[STAThreadAttribute]
static void Main()
{
}
*/
public void Init()
{
lock (_lock)
{
Thread _thread = new Thread(new ThreadStart(_GenerateHtmlToImgImage));
_thread.Name = "Galsun.Fcx.htmlToImage";
_thread.SetApartmentState(ApartmentState.STA);
_thread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
_thread.Start();
_thread.Priority = ThreadPriority.Highest;
TimeSpan ts = new TimeSpan(0, 0, 0, 30);
_thread.Join();
GC.Collect();
GC.Collect();
}
}
[STAThread]
private void _GenerateHtmlToImgImage()
{
Monitor.Enter(this);
try
{
#region 普通
WebBrowser _WebBrowser = new WebBrowser();
_WebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
//_WebBrowser.ProgressChanged += new WebBrowserProgressChangedEventHandler(WebBrowser_ProgressChanged);
_WebBrowser.ScrollBarsEnabled = false;
_WebBrowser.Navigate(_url);
System.Windows.Forms.Timer _tm = new System.Windows.Forms.Timer();
_tm.Interval=1000;
_tm.Tick += new EventHandler(Time_Tick);
_tm.Enabled = true;
//Application.SetCompatibleTextRenderingDefault(false);
Application.Run();
//Application.EnableVisualStyles();
_WebBrowser.Dispose();
#endregion
#region 新模式
/*
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
MainForm _form = new MainForm();
_form._url = _url;
_form._width = _width;
_form._height = _height;
_form._swidth = _swidth;
_form._sheight = _sheight;
_form.Width = _width;
_form.Height = _height;
Application.Run(_form);
_image = _form._image;
_thumbnailimage = _form._thumbnailimage;
*/
#endregion
}
finally
{
Monitor.Exit(this);
}
}
private void Time_Tick(object sender, EventArgs e)
{
if (runtime > 0)
{
Thread.Sleep(100);
Application.ExitThread();
}
}
private void WebBrowser_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
{
WebBrowser _webBrowser = (WebBrowser)sender;
if (e.CurrentProgress == e.MaximumProgress && _webBrowser.ReadyState == WebBrowserReadyState.Complete)
{
Application.ExitThread();
}
}
private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
Monitor.Enter(this);
try
{
WebBrowser _webBrowser = (WebBrowser)sender;
if (_webBrowser.ReadyState == WebBrowserReadyState.Complete)
{
DocText = _webBrowser.DocumentText;
if(_webBrowser.Document.Body.ScrollRectangle.Height> _height)
{
_height=_webBrowser.Document.Body.ScrollRectangle.Height;
}
_webBrowser.ClientSize = new Size(_width, _height);
//_webBrowser.ScrollBarsEnabled = false;
//Application.DoEvents();
_image = new Bitmap(_webBrowser.Bounds.Width, _webBrowser.Bounds.Height);
_webBrowser.BringToFront();
_webBrowser.DrawToBitmap(_image, _webBrowser.Bounds);
_thumbnailimage = (Bitmap)_image.GetThumbnailImage(_swidth, _sheight, null, IntPtr.Zero);
runtime++;
}
}
finally
{
Monitor.Exit(this);
}
}
}