473,657 Members | 2,711 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Thread Closing Error on specfic machine.

Fr33dan
57 New Member
Hi,

I'm having trouble with a multi-threaded program crashing on a specific machine when the worker thread is not initialized at when the _FormClosing method of my main form called.

Here is my on _FormClosing method:
Expand|Select|Wrap|Line Numbers
  1. private void CycleBox_FormClosing(object sender, FormClosingEventArgs e)
  2.         {
  3.             // Set the boolean flags to stop cycling in worker thread.
  4.             // Closing prevent dialogs from opening
  5.             closing = true;
  6.             // Cycling tells the thread to stop running.
  7.             cycling = false;
  8.  
  9.             // If the thread is created and running.
  10.             if (workerThread != null && workerThread.IsAlive)
  11.             {
  12.                 // Tell the user we are working on closing.
  13.                 Log.writeMessage(log, "Finishing Cycle");
  14.                 // Wait for current cycle to complete.
  15.                 workerThread.Join();
  16.                 // Get rid of the dialog to the user.
  17.                 Log.closeMessage();
  18.             }
  19.         }
On my development machine (Windows XP) and my test machine (Windows 2000) this works without an issue. On the computer I need it to work (Windows 2000) on it throws this error:
Expand|Select|Wrap|Line Numbers
  1. See the end of this message for details on invoking 
  2. just-in-time (JIT) debugging instead of this dialog box.
  3.  
  4. ************** Exception Text **************
  5. System.NullReferenceException: Object reference not set to an instance of an object.
  6.    at Cycle_Box.CycleBox.CycleBox_FormClosing(Object sender, FormClosingEventArgs e)
  7.    at System.Windows.Forms.Form.OnFormClosing(FormClosingEventArgs e)
  8.    at System.Windows.Forms.Form.WmClose(Message& m)
  9.    at System.Windows.Forms.Form.WndProc(Message& m)
  10.    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
  11.    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
  12.    at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  13.  
  14.  
  15. ************** Loaded Assemblies **************
  16. mscorlib
  17.     Assembly Version: 2.0.0.0
  18.     Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
  19.     CodeBase: file:///c:/WINNT/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
  20. ----------------------------------------
  21. Cycle Box
  22.     Assembly Version: 1.0.0.0
  23.     Win32 Version: 1.0.0.0
  24.     CodeBase: file:///C:/Program%20Files/Isotemp%20Research/Cycle%20Box/Cycle%20Box.exe
  25. ----------------------------------------
  26. System.Windows.Forms
  27.     Assembly Version: 2.0.0.0
  28.     Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
  29.     CodeBase: file:///C:/WINNT/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
  30. ----------------------------------------
  31. System
  32.     Assembly Version: 2.0.0.0
  33.     Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
  34.     CodeBase: file:///C:/WINNT/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
  35. ----------------------------------------
  36. System.Drawing
  37.     Assembly Version: 2.0.0.0
  38.     Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
  39.     CodeBase: file:///C:/WINNT/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
  40. ----------------------------------------
  41. ATE
  42.     Assembly Version: 0.0.0.0
  43.     Win32 Version: 0.0.0.0
  44.     CodeBase: file:///C:/Program%20Files/Isotemp%20Research/Cycle%20Box/ATE.EXE
  45. ----------------------------------------
  46. System.Xml
  47.     Assembly Version: 2.0.0.0
  48.     Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
  49.     CodeBase: file:///C:/WINNT/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
  50. ----------------------------------------
  51.  
  52. ************** JIT Debugging **************
  53. To enable just-in-time (JIT) debugging, the .config file for this
  54. application or computer (machine.config) must have the
  55. jitDebugging value set in the system.windows.forms section.
  56. The application must also be compiled with debugging
  57. enabled.
  58.  
  59. For example:
  60.  
  61. <configuration>
  62.     <system.windows.forms jitDebugging="true" />
  63. </configuration>
  64.  
  65. When JIT debugging is enabled, any unhandled exception
  66. will be sent to the JIT debugger registered on the computer
  67. rather than be handled by this dialog box.
  68.  
  69.  
  70.  
And I cannot figure out where the null reference is coming from. The only object a refer to I'm checking for a null reference and I'm pretty sure primitives can't be null C#
Oct 30 '09 #1
4 2212
tlhintoq
3,525 Recognized Expert Specialist
System.NullRefe renceException: Object reference not set to an instance of an object.
at Cycle_Box.Cycle Box.CycleBox_Fo rmClosing(Objec t sender, FormClosingEven tArgs e)
I can make some suggestions.
  • Assume that everything is broken, or at least not ideal.
  • Presume that the user is going to provide data in a format or manner that you just didn't expect. If you use a textbox for a number, the user will type "One".
  • Assume that hardware breaks in the middle of what you are doing, so you have to recover.
  • Take a few extra lines of code to get standards like the boot drive, the number thousands seperator etc. Don't assume that you have a C: drive or that a comma is the separator because not everyone is in America.
  • Check that files/folders exist, even if you just did a call to make it: You may not have permissions.
  • Don't assume the harddrive has room for what you are doing: They do fill up. Usually right in the middle of you writing to your log file.
  • Get used to placing breakpoints and walking through the code line by line. Double check EVERYTHING on every line. Keep the "Locals" and "Autos" windows open so you can see your values.
    • Put a breakpoint on line 1.
    • When the code stops there, walk through line by line with F-10.
    • Check the values of your assumptions.
  • Stop. Breath. Relax. Then reason out the problem. Cut it down by sections or halves. "The value was good here, then at this method it wasn't. Where did it go between 'A' and 'B'?"
  • Range check and validate values. Confirm that you didn't get a zero when you are only set to accept 1-10. Confirm your objects and values aren't null. Initialize them to a known default if possible. If a selection can be from 0-10, then initialize to -1: Now you have something to check for.

In the case of a null reference exception you know exactly where it happens because the compiler should break on that line. Just hover the mouse over the objects and the tooltip will show you the values. See which is null.

Now, if this doesn't happen on your test machine, but only in the real world...
A) VMware. A great product that lets you have virtual computers on your computer. You can test in a dozen different OS versions without a dozen different PCs. Best of all, it integrates with Visual Studio so you can have VS throw the project being debugged directly into VMware. You can use its remote debug feature to build in XP, yet debug in Win2k
B) In the method that is failing you need to add more robust value validation before running code. You can't assume things are not null.

Example:
Expand|Select|Wrap|Line Numbers
  1. Graphics g = Graphics.FromImage(m_Undo);
Presumes that m_Undo must be good (not null)(actually exists)(not in use)(you have permissions)(do esn't time out when accessed). If that assumption fails so does the program, because you can't make anything from a file if the file is null. Get used to

validating data and assumptions in your code if you want it to be robust. For example:
Expand|Select|Wrap|Line Numbers
  1. if (m_Undo != null)
  2. {
  3.    bool bSuccess = false;
  4.    // Do your thing here, for example:
  5.    if (myObject != null) bSuccess = true;
  6.    // or
  7.    if (denominator > 0) bSuccess = true;
  8.    // or
  9.    if (MyFunctionReturn != Failed) bSuccess = true;
  10.    // Hurray, your thing worked!
  11.  
  12.    if (bSuccess)
  13.    {
  14.       // Then do this other thing if it worked
  15.    }
  16.    else
  17.    {
  18.       // Then do the failure recovery part / user failure message
  19.    }
  20.  
  21.    return bSuccess; // If you want the calling method to know if it worked.
  22. }
It could be that your properties of 'closing' or 'cycling' are null... or the log. If the application is quiting maybe the log has already been disposed of by the time this form tries to close and wants to access it.

Try putting in more log entries to see how far it gets before failure
Expand|Select|Wrap|Line Numbers
  1. private void CycleBox_FormClosing(object sender, FormClosingEventArgs e)
  2.         {
  3.                 Log.writeMessage(log, "Form closing");
  4.  
  5.             // Set the boolean flags to stop cycling in worker thread.
  6.             // Closing prevent dialogs from opening
  7.                 Log.writeMessage(log, "closing true");
  8.  
  9.             closing = true;
  10.             // Cycling tells the thread to stop running.
  11.                 Log.writeMessage(log, "cycling true");
  12.  
  13.             cycling = false;
  14.                  Log.writeMessage(log, "cycling false");
  15.  
  16.             // If the thread is created and running.
  17.             if (workerThread != null && workerThread.IsAlive)
  18.             {
  19.                 // Tell the user we are working on closing.
  20.                 Log.writeMessage(log, "Finishing Cycle");
  21.                 // Wait for current cycle to complete.
  22.                 workerThread.Join();
  23.                 // Get rid of the dialog to the user.
  24.                 Log.closeMessage();
  25.             }
  26.         }
Oct 30 '09 #2
Fr33dan
57 New Member
That's good advice and I do perform a great deal of data validation and checking for my log (all encapsulated in my Log class) and I do enough here (closing and cycling are both primative bools and I get a expression is always 'true' warning if I check it).

I found my error and it is really annoying. So when I would run the code on my development machine (and testing machine) I would always use the debug build configuration so I can use the debugger (or remote debugger). Then I would switch to release before I built my Setup and Deployment project. Because of this the release build of my actual code project was never created. It was instead including an old version that was built weeks ago (before I added the data validation). By building that project before my Setup project all my problems went away.

Thanks for you help though. If I would have had trouble figuring out the problem in the actual wrong could this would have been very helpful.
Oct 30 '09 #3
tlhintoq
3,525 Recognized Expert Specialist
Umm... The debugger still works when you pick 'Release' build.

If you have precompile commands that are tied to 'Debug' they will execute when in debug versus when in 'release'.

I do almost all of my debugging on the 'release' build. I only go to 'debug' when I want the added thousand log entries to be written and lots of extra console comments. For example:

Expand|Select|Wrap|Line Numbers
  1.             #if _DEBUG
  2.             Console.WriteLine(e.EventType.ToString());
  3.             #endif
  4.  
Oct 31 '09 #4
Fr33dan
57 New Member
Oh I was not aware of that worked. Well I guess that's what I get for just assuming things about the language. Oh well, another error another lesson. Thanks for your help.
Oct 31 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
2483
by: Gardner Pomper | last post by:
Hi, I am pretty new to python, so be gentle :) I have a python script that spawns a number of threads (configurable) on a 12 processor AIX RISC-6000 machine. It works fine, so long as I am running less than 5 threads. With more than 5, I get errors, but only on AIX, not on Linux. The python script is fairly simple, because each thread just spawns a korn
2
3340
by: Jon Wright | last post by:
Trying to work around the lack of pexpect for native windows python I had a play with os.popen2 and some threads. This looks promising enough for what I want to do, but I hit on a problem with getting the script to exit cleanly. Tried replacing popen with subprocess but I am still confused. Either it needs to be killed from the task manager, or I get a variety of different complaints and popping up "application error" windows. I managed to...
4
4929
by: Dr. J | last post by:
How to terminate a blocked thread? In my form's "load" I launch a TCP listening thread that stays in an infinite loop waiting for incoming TCP packets. In this form's "closing" I try to terminate this thread by calling the "Abort" funcion. But the thread does not terminate and after the form is closed this thread keeps running at blocked state. Basically the application keeps running because this thread does not terminate while...
5
8103
by: Stephen Lamb | last post by:
I have a background worker thread which I start from a form's HandleCreated event that makes calls back to the form using Invoke. During shutdown the form is disposed and the background worker thread is aborted by the system. How is one to keep the background thread from calling form.Invoke after the form's window handle has been destroyed? This is definitely happening in my application. I haven't read anything about this problem in...
0
3175
by: Andrew | last post by:
I get a Null Reference Exception if I close a modeless form (that is, a form displayed using Show()) when a selection is made from a ComboxBox. If the form is modal (displayed using ShowDialog()) or the selection is made from, say, a ListBox, no exception is thrown. I have included a simple example below. The error message refers to Unsafe Native Methods, but the code is 100% managed. What is going on ? I am using C#.NET 2003, Standard...
3
8147
by: Andrew | last post by:
I get a Null Reference Exception if I close a non-modal dialog (that is, a form opened with Show()) when a selection is made from a ComboBox. The error message refers to Unsafe Native Methods, but the code is 100% managed. The exception is not thrown if the dialog was modal (opened with ShowDialog()) or if the selection is made from, say, a ListBox. I have included a simple example below. I am using C#.NET 2003, Standard Edition.
6
1855
by: Adrian Lin | last post by:
Is there a way to read a specfic line in a text file? For instance if I wanted to just read the 5th line is that possible without having to step through the other lines? Essentially I am doing huge calculations in which genetic matrices are saved in text files instead of being held in arrays that take up memory. I am trying to do something where the genetic matrix is stored in a text file and I call specfic lines for calculations then put...
5
2209
by: taylorjonl | last post by:
I am completely baffled. I am writting a daemon application for my work to save me some time. The application works fine at my home but won't work right here at work. Basically I have a MainForm what has a Start/Stop button that starts and stops the processing thread. private void StartButton_Click(object sender, System.EventArgs e) { if( bStopSignal ) { // disable controls that aren't valid when running
7
1597
by: Uwe | last post by:
Hello, I have an application in which I want to show a loadscreen. (I'm loading data from the WEB and as long the app load the data I show an animated screen which says Loading Data. I used a thread for this. It works fine on a machine where I have installed Visual Studio (2005 or 2008) as EXE or within VS. I run it o n a machine where Visual Studio is not installed it crashes when I try to start the wait screen (after closing) a second...
0
8319
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,...
0
8837
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8612
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...
0
7347
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6175
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
4171
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...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2739
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1732
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.