473,402 Members | 2,055 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,402 software developers and data experts.

How do I START to debug this?

I've an application I've been working on for some time which has a Windows
Forms User interface, a set of 'business object' classes and interacts with
a SQL Server database and some Legacy COM Objects.

All was working well until I added some Logging code.

The Logging code raises an Event from one of the buisiness objects to the
main form of the (MDI) Windows UI. This adds a row to a DataSet which is
the source for a DataGrid on a MDI Child window.

The problem is that with the Logging event active some VERY bizzare things
are happening. If this was C++ I would be looking for stack corruption, but
in c#?

Tracing is difficult. I do not necessarily get the same symptoms on each
run and where the code gets to depends on the data and exactly where these
logging events are raised.

In each case I get

n unhandled exception of type 'System.NullReferenceException' occurred in
system.windows.forms.dll
Additional information: Object reference not set to an instance of an object

Appearing at the Application.Run.

The stack frame has
UnsafeNativeMethods.DispatchMessageW

At its top.

If I trace through I might find that some Controls on one of the Windows
Forms (not the one which manages the log!) seem to be invalid with
'error:cannot obtain value' appearing when viewed in the watch window (as a
first pass it would appear that all references or properties cannot be
found, but value variables can).

If I modify my OnLog handler to NOT raise the event then these problems do
NOT occur and the program works fine.

I have commented out the code which creates and process the COM Object and
the same sort of things happen.

I don't know where to start, really, can anyone give some hints?

Thanks

Iain
Nov 15 '05 #1
5 2645
Iain,

Are the business objects running in a thread other than the UI thread?
If this is the case, then you will want to call Invoke (passing in a
delegate and any required parameters) to make sure that the code that
modifies the UI (or any code that will ultimately modify the UI) will be
called on the UI thread.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Iain" <id********@dircon.co.uk> wrote in message
news:OX**************@tk2msftngp13.phx.gbl...
I've an application I've been working on for some time which has a Windows
Forms User interface, a set of 'business object' classes and interacts with a SQL Server database and some Legacy COM Objects.

All was working well until I added some Logging code.

The Logging code raises an Event from one of the buisiness objects to the
main form of the (MDI) Windows UI. This adds a row to a DataSet which is
the source for a DataGrid on a MDI Child window.

The problem is that with the Logging event active some VERY bizzare things
are happening. If this was C++ I would be looking for stack corruption, but in c#?

Tracing is difficult. I do not necessarily get the same symptoms on each
run and where the code gets to depends on the data and exactly where these
logging events are raised.

In each case I get

n unhandled exception of type 'System.NullReferenceException' occurred in
system.windows.forms.dll
Additional information: Object reference not set to an instance of an object
Appearing at the Application.Run.

The stack frame has
UnsafeNativeMethods.DispatchMessageW

At its top.

If I trace through I might find that some Controls on one of the Windows
Forms (not the one which manages the log!) seem to be invalid with
'error:cannot obtain value' appearing when viewed in the watch window (as a first pass it would appear that all references or properties cannot be
found, but value variables can).

If I modify my OnLog handler to NOT raise the event then these problems do
NOT occur and the program works fine.

I have commented out the code which creates and process the COM Object and
the same sort of things happen.

I don't know where to start, really, can anyone give some hints?

Thanks

Iain

Nov 15 '05 #2
Thanks, Nicholas for this response.

Since the first post, I've reverted to the old Binary Chop means of
debugging - deleting bits of code until it works / fails.

The whole thing is fine unless the DataTable I'm populating via my events is
bound to my DataGrid. When it is the UI fails in one way or another
otherwise it is OK (this applies even if I defer the binding until after
some events have occurred).

Now I'm gonna seem ignorant. I don't see how to use Invoke.

Firstly, you are right the events are being called from a separate thread.
And I can appreciate this may be the underlying cause.

Oddly, in this thread, I already have a different event being called which
links to a Custom Control I've produced. This has never apparently shown
any problems.

So we have Thread 1 which is the UI. This contains an event handler which
will receive a new log event. It (indirectly) adds the data in this Log
event into a new row in a DataTable which in turn is bound to a DataGrid.

When my underlying Worker object is created it adds this handler (in the Top
window) to the event source in the Worker object.

At some point a 'Run' method is called which starts the thread which calls
the delegates on the Event Source, which raises the event and so on.

(I hope this makes some kind of sense).

Now from reading the books, Invoke seems to be mainly intended for calling
Methods on a control from another thread and I can't really see how it
applies here. I would have assumed that MS would manage the cross thread
marshalling of Delegates automatically, but who knows!

Add to that, that I have got a functionally very similar custom event being
raised alread which does not seem to cause any problems.

I don't know if this makes any sense, but I'd appreciate any further
feedback!

Iain
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:u5*************@TK2MSFTNGP11.phx.gbl...
Iain,

Are the business objects running in a thread other than the UI thread? If this is the case, then you will want to call Invoke (passing in a
delegate and any required parameters) to make sure that the code that
modifies the UI (or any code that will ultimately modify the UI) will be
called on the UI thread.

Hope this helps.

Nov 15 '05 #3
Iain,

You are incorrect in assuming that MS would handle any cross-thread
delegate issues. To be honest, I am glad they did it the way they did, as
making assumptions like that can be very, very bad.

If you have a data table/view that is attached to a grid (which is a UI
element), then changing the underlying data source will cause the UI to be
changed. When you make the change, the event is fired on the thread that
the change occured, and then the event handler attempts to update the UI as
a result. This is why you are having the problems that you are having.

Now, to get around this, call the Invoke method. This will have the
effect of calling the delegate on the thread that the control has affinity
with (in this case, the main UI thread). Say you wanted to update a data
table that is bound to a table. You can write a method like this:

// Updates the data table.
public static void UpdateDataTable(DataTable table, string field, object
value)
{
// Set the value on the first row of the data table.
table.Rows[0][field] = value;
}

Now, define a delegate which matches the signature:

public delegate void UpdateDataTableCallback(DataTable table, string field,
object value);

Finally, from the worker thread, call Invoke on a control or form that
was created on the main UI thread:

// Assume that mobjForm is the main form.
mobjForm.Invoke(new UpdateDataTableCallback(UpdateDataTable), new
object[3]{pobjDataTable, pstrField, pobjValue});

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Iain" <id********@dircon.co.uk> wrote in message
news:OK**************@TK2MSFTNGP10.phx.gbl...
Thanks, Nicholas for this response.

Since the first post, I've reverted to the old Binary Chop means of
debugging - deleting bits of code until it works / fails.

The whole thing is fine unless the DataTable I'm populating via my events is bound to my DataGrid. When it is the UI fails in one way or another
otherwise it is OK (this applies even if I defer the binding until after
some events have occurred).

Now I'm gonna seem ignorant. I don't see how to use Invoke.

Firstly, you are right the events are being called from a separate thread.
And I can appreciate this may be the underlying cause.

Oddly, in this thread, I already have a different event being called which
links to a Custom Control I've produced. This has never apparently shown
any problems.

So we have Thread 1 which is the UI. This contains an event handler which
will receive a new log event. It (indirectly) adds the data in this Log
event into a new row in a DataTable which in turn is bound to a DataGrid.

When my underlying Worker object is created it adds this handler (in the Top window) to the event source in the Worker object.

At some point a 'Run' method is called which starts the thread which calls
the delegates on the Event Source, which raises the event and so on.

(I hope this makes some kind of sense).

Now from reading the books, Invoke seems to be mainly intended for calling
Methods on a control from another thread and I can't really see how it
applies here. I would have assumed that MS would manage the cross thread
marshalling of Delegates automatically, but who knows!

Add to that, that I have got a functionally very similar custom event being raised alread which does not seem to cause any problems.

I don't know if this makes any sense, but I'd appreciate any further
feedback!

Iain
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:u5*************@TK2MSFTNGP11.phx.gbl...
Iain,

Are the business objects running in a thread other than the UI
thread?
If this is the case, then you will want to call Invoke (passing in a
delegate and any required parameters) to make sure that the code that
modifies the UI (or any code that will ultimately modify the UI) will be
called on the UI thread.

Hope this helps.


Nov 15 '05 #4
Thankyou. That has fixed the problem.

I guess I should apply this to my other events which affect the UI, even though they *appear* to work.

And whilst I would have preffered a little more specific error message from our Friends in Seattle on this one (that is - 'please use the right thread', rather than *CRASH*!), it is easier that message passing to a hidden window and a damn sight les daunting than the Com marshalling alternatives (CoMarshalInterThreadInterfaceInStream if I remember correctly!).

Thank you again. This would have taken forever.

Iain
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:ua*************@tk2msftngp13.phx.gbl...
Iain,

You are incorrect in assuming that MS would handle any cross-thread
delegate issues. To be honest, I am glad they did it the way they did, as
making assumptions like that can be very, very bad.


Nov 15 '05 #5
Iain,

It should be noted that it doesn't pass the message to a hidden window.
Rather, when you call Invoke, the message handler for the control/form you
call it on is the one receiving the message and processing it. If that
window is hidden, then yes, it is the case, but otherwise, no.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Iain" <id********@dircon.co.uk> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
Thankyou. That has fixed the problem.

I guess I should apply this to my other events which affect the UI, even
though they *appear* to work.

And whilst I would have preffered a little more specific error message from
our Friends in Seattle on this one (that is - 'please use the right thread',
rather than *CRASH*!), it is easier that message passing to a hidden window
and a damn sight les daunting than the Com marshalling alternatives
(CoMarshalInterThreadInterfaceInStream if I remember correctly!).

Thank you again. This would have taken forever.

Iain
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:ua*************@tk2msftngp13.phx.gbl...
Iain,

You are incorrect in assuming that MS would handle any cross-thread
delegate issues. To be honest, I am glad they did it the way they did, as
making assumptions like that can be very, very bad.

Nov 15 '05 #6

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

Similar topics

2
by: TomS | last post by:
I have WinXP Home Edition and VC++ 2002. I have execute command: Debug->Start without debbuging or Debug->Start and VC++ send messages: Access is denied. Verify taht you are administrator or a...
5
by: Kenneth | last post by:
Hi, I've upgraded to .NET 2003 and I opened an .NET 2002 app and let the new IDE convert it to .NET 2003 project. Then I tried to start the application but it keeps on saying "Error while...
16
by: Serdar Kalaycý | last post by:
Hi everybody, My problem seems a bit clichè but I could not work around. Well I read lots of MSDN papers and discussions, but my problem is a bit different from them. When I tried to run the...
5
by: Bruce Schechter | last post by:
I just started to develop an ASP.NET application in vs.net 2003 . But each time I try to execute the application (which is basically empty so far), I get a dialog box titled "Microsoft Development...
3
by: derek | last post by:
When I select "Debug->Start" from the menu to debug my ASP.NET web app, I get the following error message in a dialog box: Error while trying to run project: Unable to start debugging on the web...
2
by: NAGY | last post by:
hello, I created an asp.net web app in C# from a non administrative account in Visual Studio 2003, .net 1.1. when i try to run the application in debug mode from Debug start menu option, i get an...
5
by: SiD` | last post by:
when starting a windows service writte in vb.net, a messagebox appears: cannot start service from the command line or a debugger. A windows service must first be installed using installutil.exe ...
37
by: ales | last post by:
Hello, I have a problem with creation of new thread. The method .Start() of newly created thread delays current thread for 0 - 1 second. Cpu while delay occurs is about 5%. Any idea? Here...
9
by: lostnewmexico | last post by:
This one has me at whit's end. In order for this to make sense, I will have to lay out the "facts" to get a hypothesis. There is no easy way to explain this. Environment: VC2005. MFC...
3
by: Tim Chase | last post by:
I've been trying to come up with a good algorithm for determining the starting and ending dates given the week number (as defined by the strftime("%W") function). My preference would be for a...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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...
0
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...
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,...

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.