开发者

Threaded console app throwing error, but execution carries on as normal and nothing is logged

I've got a small console application which has a fairly simple task of collecting realtime data from a data server and throwing it in a database. The app makes a TCP connection to a server which t开发者_运维技巧hen holds the connection open and throws data down the pipe. The app collects it, reformats it and then chucks it in an MSSQL database.

The application is multi-threaded - on startup, I spin up a 'Collector' thread that's in charge of managing the TCP connection to the data source and throwing data messages onto an internal queue. There's a separate 'processor' thread that looks for messages in the queue, pulls them off individually and creates a database insert transaction. The initial startup thread does pretty much nothing at all once the other threads are up and running.

Everything's fairly extensively logged using NLog, both to file and to console.

The issue I'm having is that on occasion, Windows (Server 2008) throws up an error dialog similar (but obviously not the same!) to the one below. When this happens, the console logging pauses so i can see the approximate time the error is thrown. If I click 'Close the program', the app continues running as normal, but the size of the message queue rapidly goes up, as if the Collector thread has been running all along but the processor thread has been blocked. There's nothing recorded in the logs about any exceptions being thrown.

http://img833.imageshack.us/img833/7533/pleaseclosethisprogram.png

Given that I think I'm catching as many exceptions as possible and logging as far as possible, I'm stumped as to what this might be. I originally thought it might be the logging mechanism throwing an exception, but after ripping out log4net and replacing it with Nlog, I'm seeing the same behavior. This is extremely difficult to reproduce given it only happens irregularly, about twice per week. The app is written in C# .NET 4.

If anyone's got any ideas about steps I can take to troubleshoot this issue, that would be hugely appreciated.

edit

Ok, I've got some details from the event log, I'd completely forgotten that .NET exceptions get logged in there as well. here's the data:

Faulting application name: DataArchiver.exe, version: 1.0.0.0, time stamp: 0x4dd027df
Faulting module name: unknown, version: 0.0.0.0, time stamp: 0x00000000
Exception code: 0x00000000
Fault offset: 0x0039b8f0
Faulting process id: 0x59c
Faulting application start time: 0x01cc13358473b6da
Faulting application path: D:\DataArchiver\DataArchiver.exe
Faulting module path: unknown
Report Id: f352ef11-7ffc-11e0-9cd0-005056bc0006


For start, you could make sure that you catch any unhandled exceptions in your app by wrapping the entire Main method in a try/catch block. Also, add an event handler for the AppDomain.CurrentDomain.UnhandledException event. This will catch event thrown by your background threads. Both handlers should log the error and then allow the application to close.

If you have a thread running in background, and its IsBackground property is not set to true, it will keep your application alive. Your main program logic should also take care to notify all threads that they are supposed to finish in case of an exception, user close request or windows shutdown.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜