473,503 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
+ 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 1310

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

Similar topics

0
277
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
3492
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
4280
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
9281
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
2929
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
2227
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
1374
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
4932
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
2579
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
5797
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
7093
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
7287
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,...
0
7348
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...
1
7006
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...
1
5021
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...
0
3166
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1519
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 ...
1
744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
397
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.