473,729 Members | 2,331 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.IsDisp osed) 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.t xt"))
{
sw.WriteLine(Da teTime.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 1299
First, how about (!this.IsDispos ed && 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
1346
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 in a worker thread and it is unhandled, does it bring the whole application down or just the worker thread that suffered it? b) if an exception is thrown in a function which doesn't have exception handling but the function that called it does, will...
13
2061
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 ignorance, with one of the compilers I am having trouble related to libuwind.so, which, to my knowledge, deals with the intricacies of unwinding the stack upon an exception. Executables compiled with this compiler crash with a SEGV after throw(), never...
8
7678
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) { if(MyBitmap != null) { Graphics g = e.Graphics;
6
2362
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 problem also with other imports (e.g. urllib2 etc.). And again very often in all these cases where I get weired Python exceptions, the problem is around re-functions - usually during re.compile calls during import (see some of the exceptions below). But...
0
8917
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8761
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2680
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2163
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.