G
Guest
Hi,
I was under the impression that the finally block always got executed no
matter what. Well I ran into a situtation that I can duplicate from a
console app as well as a web application.
For the console app if the try block is executing/processing its clode and
the console app is closed the finally block is not called.
The same is true for the web app. If the page_load or button_click is
processing cose and it is wrapped by a try/catch/finally or try/finally the
finally is not called if the web page is closed.
Give it a try, I was blown away by this. I thought no mwatter what the
finally block was called and I have very important logic that needs to be
called.
Below is very sloppy code, but it will do the trick to show this isn't
working. The same goes for the console. Just put the try/finally in the
main.
Code:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class GeneralTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(DateTime.Now.ToString());
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int x = 0; x < 100000000; x++)
{
for (int y = 0; y < 100000000; y++)
{
sb.Append(x.ToString() + " " + y.ToString());
}
}
}
finally
{
FileStream fsUpdateBackupDateTime = new FileStream(@"c:\zzzzcjm.txt",
FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fsUpdateBackupDateTime);
sw.Write(DateTime.Now.ToString());
sw.Flush();
sw.Close();
}
}
}
I guess is there anything that can be done or a way to make the finally or a
block of code to be called if the console app or web application is closed?
Thanks for your time!!
cmullins
I was under the impression that the finally block always got executed no
matter what. Well I ran into a situtation that I can duplicate from a
console app as well as a web application.
For the console app if the try block is executing/processing its clode and
the console app is closed the finally block is not called.
The same is true for the web app. If the page_load or button_click is
processing cose and it is wrapped by a try/catch/finally or try/finally the
finally is not called if the web page is closed.
Give it a try, I was blown away by this. I thought no mwatter what the
finally block was called and I have very important logic that needs to be
called.
Below is very sloppy code, but it will do the trick to show this isn't
working. The same goes for the console. Just put the try/finally in the
main.
Code:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class GeneralTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(DateTime.Now.ToString());
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int x = 0; x < 100000000; x++)
{
for (int y = 0; y < 100000000; y++)
{
sb.Append(x.ToString() + " " + y.ToString());
}
}
}
finally
{
FileStream fsUpdateBackupDateTime = new FileStream(@"c:\zzzzcjm.txt",
FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fsUpdateBackupDateTime);
sw.Write(DateTime.Now.ToString());
sw.Flush();
sw.Close();
}
}
}
I guess is there anything that can be done or a way to make the finally or a
block of code to be called if the console app or web application is closed?
Thanks for your time!!
cmullins