Hello, I have a C# application that is almost complete, but I'm experiencing some hard to track down issues. The app is designed to sit in the system tray, waking up periodically to check various fields in an Access 2003 database, and send an email if needed. Double clicking the tray icon brings up a log in form and then a UI where the user can tweak settings and what not.
To achieve the scheduling, I start a BackgroundWorker when the app starts, and tell it to sleep for the SleepPeriod (user decides), using Thread.Sleep(). When it wakes up it uses a OleDbConnection to the db, checks the appropriate fields, sends emails if needed, and then goes to sleep again.
When the user hits the save button or minimizes the app (to the tray) all his settings are saved to tables in the same db using the same OleDbConnection.
It all works fine, except that for some reason the UI (main) thread hangs occasionally, seemingly at random. It seems to happen if you leave the app between one and four hours, but I've also had the same instance running for 3 days straight without it occurring.
It happens in the following manner: The user tries to restore the app by double clicking on the system tray, and the little log in form appears, but does not draw fully (bits of the background poke through) and is totally unresponsive. The background scheduling thread continues to work fine (send out emails, etc).
Oh, and I've also written a LogWriter that accepts Messages from both the scheduling and UI threads into a static Queue<Message>. When the LogWriter is created it starts another thread that wakes up every couple of minutes, and if the Message Queue is over a certain size, writes all the messages to a text file at once. This thread also continues to function as normal.
So, I don't know if you can help with the info I've been able to give you (might be too much). Any ideas on what could cause this sort of hang up (unresponsive windows, not even drawing) in a C# winforms app? Let me know if you'd like specific code snippets for anything I've described.
Also, on a potentially related note. Is it a good idea or a bad idea to use the same OleDbConnection across threads? I just now realized I'm doing this and might just have answered my own question. Except that the UI thread shouldn't be using the connection unless the user hits the "run now" button, which he obviously hasn't during the freeze ups, because they've typically happened overnight when the person is not around. Should I leave the connection open constantly, or open and close it whenever I need it?