473,474 Members | 1,649 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Form creation exception error; swapping delaying object creation?

Hi all,

I have a really odd problem with some Visual Basic .NET 2003 code;

I have a program that creates a number of windows which contain
RichTextBox, Timers (disabled) and menus. The code runs fine and
creates the windows as required, BUT if the program is left to do
nothing for several hours, when it is brought back into focus it
generates an exception error (Object reference not set to instance of
an object - this always coincides with a LOT of disk activity) when it
tries to access a window handle. The really odd thing is, if the
excepton is trapped with a breakpoint, examining the values it's
trying to manipulate (and which presumably causes the exception)
reveals nothing wrong.

The code is:

Dim NewForm As New Form2
Dim MyWindowHandle as System.IntPtr

NewForm.Show()
Try
MyWindowHandle = NewForm.Handle ' this occassionally goes "bang"
Catch ex As Exception
MsgBox("Exception") ' by this point, NewForm.Handle has a valid
handle in it.
End Try

If I put a breakpoint on the MsgBox line, when the code generates an
exception, NewForm.Handle has a valid value; I can see absolutely no
reason why this code causes an exception. The NewForm.Show() works
without a problem. I wondered if my code was hitting some
resource-limit in XP (like the limit on the number of window handles
that existed in Windows 3.1) so I tried running my "create window"
code 1,000 times via a loop, and it worked fine. In general ("real")
use, the code doesn't get called more than about 30 times.

The code is far more likely to crash if it's run in Debug mode via VS
itself; if it's compiled in Release mode and run outside of VS, the
crashes are less frequent.

My theory is that if the program is left doing nothing for several
hours, XP or .NET swaps the code to disk and when the program's
brought back into focus, it's retrieved from disk but (I'm guessing
here) the code starts executing _before_ all the bits and pieces are
restored to RAM, hence the exception. By the time the breakpoint is
activated, everything's in place and that's why my VS 'watches' on the
variables show nothing amiss. If my theory is correct, is there some
way a program can test whether it (and its objects) are all present
and correct before accessing things like window handles? A couple of
other programs I've written also exhibit this problem. Since things
like MS Word and Excel don't go horribly wrong when brought into
focus, surely there's something Microsoft & others are doing that I'm
not (like not writing programs that use the .NET Framework, I suppose
;-) ). Changing the code to look like the following seemed to confirm
my theory:

Dim NewForm As New Form2
Dim MyWindowHandle as System.IntPtr

NewForm.Show()
Try
MyWindowHandle = NewForm.Handle ' this occassionally goes "bang"
Catch ex As Exception
MsgBox("Exception") ' This gets called fairly often
Thread.Sleep(10000) ' sleep 10 seconds
Try
MyWindowHandle = NewForm.Handle
Catch ex As Exception
MsgBox("Exception #2") 'This rarely gets called
End Try
End Try

The first "MyWindowHandle = NewForm.Handle" crashes more than the 2nd
one, yet they are both the same thing.

My PC is running Windows XP Home Edition SP1 with all the latest
fixes, I'm using Visual Studio 2003 Standard Edition, with the latest
patch (the 75MB one released last year to apply "documentation
updates" (75 megabytes of them??)). I'm running Microsoft .NET
Framework 1.1. My PC has 512MB RAM and an Intel P4 (non-HT) 3.06Ghz
CPU.

Thanks in advance for any help or suggestions,
Fiona.
Nov 22 '05 #1
0 1307

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

Similar topics

0
by: Fiona McBride | last post by:
Hi all, I have a really odd problem with some Visual Basic .NET 2003 code; I have a program that creates a number of windows which contain RichTextBox, Timers (disabled) and menus. The code...
7
by: Bil Muh | last post by:
Esteemede Developers, I would like to Thank All of You in advance for your sincere guidances. I am developing a software using Visual C++ .NET Standard Edition with Windows Form (.NET)...
8
by: Sergio Otoya | last post by:
Hi all, I need to add an input hidden field to an existing form (post). I have tried a couple things like adding the '<INPUT type=hidden name=idSelectedURL value=http://server/documents>' to...
4
by: Stuart Perryman | last post by:
Hi, I have the following code which works just fine in IE6 but not in Firefox. It is an extract of several table rows each with an individual form. It is generated by php. <form...
3
by: Professor Frink | last post by:
First off, I apologize if this gets long. I'm simply trying to give you all enough information to help me out. I'm writing (almost finished, actually), my first VB.Net application. It's a forms...
4
by: Craig831 | last post by:
First off, I apologize if this gets long. I'm simply trying to give you all enough information to help me out. I'm writing (almost finished, actually), my first VB.Net application. It's a forms...
0
by: Fiona McBride | last post by:
Hi all, I have a really odd problem with some Visual Basic .NET 2003 code; I have a program that creates a number of windows which contain RichTextBox, Timers (disabled) and menus. The code...
2
by: Richard Collette | last post by:
Hi, I have a service, that runs perfectly when executed outside of the web service environment. When called as a web service I get the exception listed below sporadically. A call to the web...
0
by: Smokey Grindle | last post by:
I am using the user creation wizard in asp.net 2.0 and using it to send the user an email message when their account is created, well this works great if the address is a valid mailbox, but when it...
4
by: ThunderMusic | last post by:
Hi, I have a custom form that works fine when I debug it or run it in release mode but cannot be loaded in the designer... Actually, it can be loaded in the designer when no control is on it, but...
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
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,...
1
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...
0
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,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.