473,387 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Having trouble with background threads and exceptions

Hey guys,

Here is the scenario:

I have the main form open and a background thread is running. The
background thread sometimes needs access to the main forms graphics object
and gets it by calling Graphics g = CreateGraphics().

Now, I close my main form while the background thread is running and when
the background thread gets to the CreateGraphics() line, it throws an
exception (since the main form has been disposed).

I couldn't think of any way to prevent that exception from occuring. Since I
need the forms graphics object to use in that background thread, and checking
if(!this.IsDisposed) isn't guaranteed to always work, I thought I can at
least log the exception. However, that doesn't seem to work either.

The background thread method is enclosed in a try..catch block and in the
catch block I have:

using (StreamWriter sw = new StreamWriter(@"C:\Errors\Log.txt"))
{
sw.WriteLine(DateTime.Now + " [ERROR] " + ex_.Message);
}

When I'm debugging, if I close my main form and, if the exception is thrown,
which is not always, break on the WriteLine I can see the error message fine.
However, after the app closes, the file "Log.txt" is created but is empty.

Does any one have any ideas as to why? I also tried displaying a MessageBox
with the error info but as far as I can tell, either the MessageBox doesn't
appear or flashes quickly and disappears.

Thanks alot,
-Flack
Mar 15 '06 #1
1 1281
First, how about (!this.IsDisposed && this.Visible)
Second, have you tried explicitly flushing/closing the streamwriter?

Mar 15 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Beeeeeeeeeeeeves | last post by:
Hi I have a service which starts extra threads which take a long time, this is so it can respond to events while long processes are running. I was just wondering, a) if an exception is thrown...
13
by: Jacek Dziedzic | last post by:
Hi! <OT, background> I am in a situation where I use two compilers from different vendors to compile my program. It seems that recently, due to a misconfiguration, library conflict or my...
8
by: Flack | last post by:
Hey guys, In my app I have a bitmap where drawing is done and in the form's paint method I show the bitmap: private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) {...
6
by: robert | last post by:
I get python crashes and (in better cases) strange Python exceptions when (in most cases) importing and using cookielib lazy on demand in a thread. It is mainly with cookielib, but remember the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.