Ich habe derzeit ein komisches Problem:
Ich schreibe eine Konsolenanwendung und möchte, bevor die Konsole geschlossen wird, den Speicher aufräumen (manuelles GC).
Code:
private delegate bool ConsoleEventDelegate(int eventType);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool SetConsoleCtrlHandler(ConsoleEventDelegate callback, bool add);
private static Dictionary<string, IRunable> runablesDict;
private static List<Thread> runablesThreadList;
public static void Main(string[] args)
{
runablesDict = new Dictionary<string, IRunable>();
runablesThreadList = new List<Thread>();
SetConsoleCtrlHandler(new ConsoleEventDelegate(ConsoleEventCallback), true);
InitializeRunables();
RunRunables(args);
}
private static void InitializeRunables()
{
runablesDict.Add("market_fetcher", new MarketFetcher());
}
private static void RunRunables(string[] arguments)
{
foreach (KeyValuePair<string, IRunable> runableEntry in runablesDict)
{
Thread runableThread = new Thread(() =>
{
runableEntry.Value.Run(runableEntry.Key, arguments);
});
runablesThreadList.Add(runableThread);
runableThread.Start();
}
}
private static void UnloadRunables()
{
foreach (KeyValuePair<string, IRunable> runableEntry in runablesDict)
runableEntry.Value.Dispose();
foreach (Thread runableThread in runablesThreadList)
KillThread(runableThread);
}
private static bool ConsoleEventCallback(int eventType)
{
if (eventType == 2)
UnloadRunables();
return false;
}
[SecurityPermissionAttribute(SecurityAction.Demand, ControlThread = true)]
private static void KillThread(Thread thread)
{
thread.Abort();
}
Kann natürlich auch sein, dass ich komplett daneben liege, und das garnichts mit der Eingabe zu tun hat - das Problemm tritt aber eben nur bei dieser Situation auf.
Ich hoffe, ihr habt eine Idee, wie ich das lösen kann







