using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace empty
{
class Program
{
static Program()
{
AppDomain.CurrentDomain.ProcessExit += ExitHandler;
}
static void Main(string[] args)
{
}
static void ExitHandler(object o, EventArgs args)
{
using (FileStream fs = new FileStream("file.bin", FileMode.Create))
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, new double[30000000]);
}
using (FileStream fs = new FileStream("file.bin", FileMode.Create))
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, new double[30000000]);
}
Console.WriteLine("end");
}
}
}
I expecting to get output:
“end”
but get nothing. What am I doing wrong?
I intentionally use 2 serializations because such behaviour doesn’t happen with 1 serialization.
If you look at the documentation for
ProcessExit, you’ll find this:So, if whatever action you’re doing in that handler takes more than two seconds, it’s not going to execute completely. This seems to be exactly the problem you’re having.