Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > .NET Languages
You last visited: Today at 01:03

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[C#] Console-Application crasht bei SetConsoleCtrlHandler und Console.ReadLine()

Discussion on [C#] Console-Application crasht bei SetConsoleCtrlHandler und Console.ReadLine() within the .NET Languages forum part of the Coders Den category.

Reply
 
Old   #1
 
pvpDealer's Avatar
 
elite*gold: 368
Join Date: Apr 2015
Posts: 688
Received Thanks: 32
[C#] Console-Application crasht bei SetConsoleCtrlHandler und Console.ReadLine()

Servus!
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();
}
Das funktioniert soweit auch sehr gut, aber: Sobald die Konsole gerade auf eine Eingabe wartet (bspw. durch Console.ReadLine()) und ich die Konsole schließe (egal ob per Ctrl+C oder per Close-Button) crashed die Anwendung. Als Unhandled Exception erhalte ich eine NullReferenceException - ich kann den Code aber nicht debuggen, da das Programm crashed, bevor der Eventcallback aufgerufen wird.

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
pvpDealer is offline  
Old 05/06/2015, 18:23   #2
 
elite*gold: 8
Join Date: Sep 2014
Posts: 625
Received Thanks: 178
Wieso möchtest du vor dem Beenden den GC anschmeißen? Wenn das Programm beendet wird, dann wird automatisch jeglicher Speicher wieder freigegeben, der vom Prozess reserviert wurde.
qqdev is offline  
Old 05/06/2015, 19:39   #3


 
elite*gold: 1091
Join Date: Jun 2007
Posts: 19,836
Received Thanks: 7,180
Quote:
Originally Posted by qqdev View Post
Wieso möchtest du vor dem Beenden den GC anschmeißen? Wenn das Programm beendet wird, dann wird automatisch jeglicher Speicher wieder freigegeben, der vom Prozess reserviert wurde.
Vermutlich um seine Ressourcen noch sauber freizugeben und laufende Threads zu beenden, ohne dass gewisse Daten verloren gehen. Gut, aus dem Beispiel oben geht das nicht wirklich hervor aber ich würde einfach mal schätzen, dass sowas gedacht war. Wenn es jedoch wirklich nur bei einem einfachen Thread.Abort bleibt und Value.Dispose nichts serialisiert (oder ähnliches), kann man das OS auch einfach alles killen lassen, das macht dann auch keinen Sinn es selbst zu tun.

Er hat das Problem übrigens anscheinend schon behoben (siehe cross-post auf stackoverflow). Auch wenn ich nicht glaube, dass genau das zur Problemlösung geführt hat.
Mostey is offline  
Old 05/07/2015, 02:42   #4
 
pvpDealer's Avatar
 
elite*gold: 368
Join Date: Apr 2015
Posts: 688
Received Thanks: 32
Habe nach einigem Suchen dann das gefunden:

Genau das hat das Problem behoben.

Bzgl. dem manuellem GC:
Ich habe einige Klassen die das IDisposable Interface implementieren; beim Schließen rufe ich dann Dispose() der erwähnten Klassen auf. Dabei logge ich mich bspw. bei EPvP aus (epvpapi) - falls ihr eine Idee habt, wie man das besser lösen kann, immer her damit :b
pvpDealer is offline  
Old 05/11/2015, 10:19   #5


 
MrSm!th's Avatar
 
elite*gold: 7110
Join Date: Jun 2009
Posts: 28,902
Received Thanks: 25,407
Du kannst Dispose im Destruktor der Klasse aufrufen, dann wird das ganze automatisch beim anstoßen des GCs erledigt.
MrSm!th is offline  
Reply


Similar Threads Similar Threads
WTS New PS4 Console 500GB..$400..Xbox One Console...$400
10/21/2021 - Consoles Trading - 1 Replies
Shipping We ship using USPS or UPS We ship within 3 business day from receipt of payment. We do not change your shipping address by email. Please make sure your address is correct before checking out and making payment.on our website We do ship to APO, FPO, Post Office box addresses. We do ship to worldwide its free Support With any question regarding our items please do not hesitate to message us and we will be happy to answer and assist you.
Wie stellt man die Console ein oder für was ist die console genau?
02/10/2013 - Rappelz Private Server - 3 Replies
Hallo und zwar wollte ich fragen was die Console Genau ist, und wie man diese dann Richtig einstellen tut. Wäre sehr nett wen ihr mir dabei Helfen könnt. Google und suche hier bei epvp haben bis jetzt nicht´s gefunden. Falls es euch hilft ich habe 7.3 Files Auth, Ubload, und Gameserver Funktionieren Einwandfrei.
[RELEASE] Console Commands for Lame Console Look
12/07/2011 - CO2 PServer Guides & Releases - 12 Replies
Well, to avoid any further useless posts on this subject... here are a ton of console based commands that will control the game from the server console. I will not explain what each does, it should be pretty easy to figure out, and I don't intend to answer stupid questions. If its a good question, I will answer it. public static void DoStuff() { bool flag = true; Console.Clear(); Console.ForegroundColor = ConsoleColor.Red; ...



All times are GMT +1. The time now is 01:03.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.