|
Eddy,
Thanks for the reply. In my case that won't really work because I need this information in the UnhandledException handler, and by the time I get to this point (the first time I actually have an exception to look at), I no longer have access to the object that threw the exception. I can certainly use this where I can handle an exception more gracefully and still want to log it with more details, but my goal is to get as much info as I can for unhandled exceptions.
Thanks,
DybsThe shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen
|
|
|
|
|
dybs wrote: my goal is to get as much info as I can for unhandled exceptions
Ditto, and you're right - if the information contained helps with the debugging-proces. There's no easy anwer to your question, once one says that it's technically possible one has to answer whether it's worth the effort.
dybs wrote: In my case that won't really work because I need this information in the UnhandledException handler, and by the time I get to this point (the first time I actually have an exception to look at), I no longer have access to the object that threw the exception.
I have created a small console-application to test with;
class Program
{
static Form myForm = new Form();
static Button myButton = new Button();
[STAThread]
public static void Main(string[] args)
{
myButton.Click += delegate
{
new MyTextCache().DoLoadStuff();
};
myForm.Controls.Add(myButton);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(logEx);
Application.Run(myForm);
}
static void logEx(object sender, UnhandledExceptionEventArgs e)
{
Exception ex = (Exception)e.ExceptionObject;
Console.WriteLine("Exception: " + e.ExceptionObject.GetType().ToString());
if (ex.Data.Count > 0)
{
foreach(var item in ex.Data)
{
var entry = (System.Collections.DictionaryEntry)item;
Console.WriteLine("Parameter: " + entry.Value.ToString());
}
}
}
} Rather simple, just creates a form with a button and logs any exceptions on the console. The key here is looping the Data property. We know that we can fill it when we throw an exception ourselves. Now, imagine that there's an unexpected exception thrown by a .NET class in the MyTextCache.DoLoadStuff method. To add the info, we'd need to catch it, attach the desired information, and rethrow it.
The MyTextCache could be adapted like shown below. There's a local exception-handler that doesn't handle the FileNotFound-exception, but instead adds a reference to the object;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;
[Serializable()]
class MyTextCache: ISerializable
{
#region variables that represent Internal State
string _filename;
string _contents;
#endregion
public MyTextCache()
{
_filename = "Q:\\Hello World.txt";
}
public MyTextCache(SerializationInfo info, StreamingContext ctxt)
{
_filename = (String)info.GetValue("filename", typeof(string));
_contents = (String)info.GetValue("contents", typeof(string));
}
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
info.AddValue("filename", _filename);
info.AddValue("contents", _contents);
}
public override string ToString()
{
return string.Format("Filename: {0}\nContents: {1}\n",
_filename,
_contents);
}
internal void DoLoadStuff()
{
try
{
_contents = System.IO.File.ReadAllText(_filename);
}
catch(Exception e)
{
e.Data.Add("the object: ", this);
throw e;
}
}
} That would give you access to the _filename and _contents properties of the MyTextCache class. You should see an invalid filename, and an empty contents-property on the console if you attempt this.
There's a second problem; you can log the contents of the class now (or serialize it), but that doesn't give you access to the local variables from the DoLoadStuff method. This can be solved by modifying the calling method, like thus;
internal void DoLoadStuff()
{
int i = 9;
try
{
_contents = System.IO.File.ReadAllText(_filename);
}
catch(Exception e)
{
e.Data.Add("the object: ", this);
e.Data.Add("local i: ", i);
throw e;
}
} As you can see, it would be a lot of work to add "all" local variables for "each" method in the project. Adding a reference to the originating class might be worth the time to modify some of the key-classes.I are Troll
|
|
|
|
|
Thanks for the detailed response! I'm looking more into the data dictionary now and serializing the objects.
One other question. I notices after you throw add the values to the Data dictionary, you use
throw e; Doesn't this "reset" the stack track to the line where you rethrow the exception? Can you just use
throw; instead? Or would a simple throw not include your changes to the dictionary?
Thanks,
DybsThe shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen
|
|
|
|
|
dybs wrote: Thanks for the detailed response! I'm looking more into the data dictionary now and serializing the objects.
My pleasure. Tracking the items in a dictionary would indeed be a nice alternative
dybs wrote: Doesn't this "reset" the stack track to the line where you rethrow the exception?
It does! That was sloppiness in my example.
dybs wrote: Or would a simple throw not include your changes to the dictionary?
It also includes the change if you omit the exception variable. Just verified it, just to be on the safe side.I are Troll
|
|
|
|
|
Hi Team,
I'm trying to figure out a way to manipulate a bitmap sent to an un-managed cpp DLL by a VB.NET program.
The vb.net loads the image from disk and puts it in memory as a windows.forms.picturebox. I need to send the DLL some characters to tell it what to do, and the image itself. DLL makes the changes to the image, then sends it back to the vb.net to display.
I'm looking at marshaling/interop but its not coming together for me.
Any ideas, previous articles etc are most appreciated!
Thank you in advance for any assistance you could provide.
Musher-AK
|
|
|
|
|
Hi,
you can pass all of the pixels of a Bitmap (not a PictureBox!) to the unmanaged world; all it takes is getting the pointer to it as an IntPtr, and pass that on to a C/C++ char pointer. Look at Bitmap.LockBits and BitmapData.Scan0
Background information on P/Invoke is available here[^] (not completed yet!).
|
|
|
|
|
Thank you very much Luc. This helps.
Can you think of any sample code somewhere that may illustrate this? I'll probably have to alloc/dealloc some memory and other what-nots and not quite sure how to do all this.
The demo app mentioned on the P/Invoke site didn't have a downloadable package.
BTW, the Pinvoke Wiki is awesome.
Again, many thanks for sharing your knowledge.
--Musher rob.
|
|
|
|
|
I don't have an example. I gave you all relevant keywords, so fire up Google and read the documentation on the classes involved. Sorry about the demo app, it isn't ready yet.
|
|
|
|
|
Thank you Luc, I appreciate the feedback! Kudos!
|
|
|
|
|
Hello
Everbody ,i am new in VB.net , i want to serializsed the collection .Please help me to do this
and if it is posible with coding.Hereby i am send the example of coding that i have done but not
working
Public Class colection
Private _objsavecli As New List(Of Cliente)
Public ReadOnly Property objsavecli() As List(Of Cliente)
Get
Return Me._objsavecli
End Get
End Property
Public Sub Addcliente(ByVal Client As Cliente)
Me.objsavecli.Add(Client)
End Sub
End class
<serializable>_
Public Class Cliente
Private _Name As String
Private _surname1 As String
Private _surname2 As Double
Public Property Name() As String
Get
Return Me_Name
End Get
Set(ByVal value As Double)
Me_Name = value
End Set
End Property
Public Property surname1 () As String
Get
Return Me.__surname1
End Get
Set(ByVal value As String)
Me._surname1 = value
End Set
End Property
Public Property surname2() As String
Get
Return Me._surname2
End Get
Set(ByVal value As String)
Me._surname21 = value
End Set
End class cliente
Imports System.Runtime.Serialization.Formatters.Soap
Module Module1
Public save As coleccion
Sub Main()
Dim obj As New SoapFormatter
Dim fs As FileStream
fs = File.Create("D:\all_floders\MCPD\\DatosMultiples")
'obj.Serialize(fs,save.objsavecli )
fs.Close()
End Module1
it could make a file but not write any thing.
|
|
|
|
|
Firstly I recomend you read up on how to Serialize and Deserialize Objects as XML using Generic Types in C# 2.0[^]. This article should answer most of your problems.
Secondly, why repost your original code within three hours of your first post without changing even one line based on the advise you were given? You were already at the top of the forum listing so the only thing you have gained is a reputation for ignoring advice.
Thirdly, Please enclose code snippets in a code block, preferably with the correct language attribute, but anything would be more readable than the sea of blue!
|
|
|
|
|
I send the original messages twice because i thought this problem belongs to VISUAL BASIC not .Net Framwork.Sorry any inconvencies.
|
|
|
|
|
Hello
Everbody ,i am new in VB.net , i want to serializsed the collection .Please help me to do this
and if it is posible with coding.Hereby i am send the example of coding that i have done but not
working
Public Class colection
Private _objsavecli As New List(Of Cliente)
Public ReadOnly Property objsavecli() As List(Of Cliente)
Get
Return Me._objsavecli
End Get
End Property
Public Sub Addcliente(ByVal Client As Cliente)
Me.objsavecli.Add(Client)
End Sub
End class
<serializable()> _
Public Class Cliente
Private _Name As String
Private _surname1 As String
Private _surname2 As Double
Public Property Name() As String
Get
Return Me_Name
End Get
Set(ByVal value As Double)
Me_Name = value
End Set
End Property
Public Property surname1 () As String
Get
Return Me.__surname1
End Get
Set(ByVal value As String)
Me._surname1 = value
End Set
End Property
Public Property surname2() As String
Get
Return Me._surname2
End Get
Set(ByVal value As String)
Me._surname21 = value
End Set
End class cliente
Imports System.Runtime.Serialization.Formatters.Soap
Module Module1
Public save As coleccion
Sub Main()
Dim obj As New SoapFormatter
Dim fs As FileStream
fs = File.Create("D:\all_floders\MCPD\\DatosMultiples")
'obj.Serialize(fs,save.objsavecli )
fs.Close()
End Module1
it could make a file but not write any thing.
|
|
|
|
|
Hi,
you have to mark the class itself as "serializable". So change the first line of your class declaration to this:
<Serializable()> Public Class Cliente
Regards
Sebastian
|
|
|
|
|
yes i have changed it , but working as same.
|
|
|
|
|
|
hello guys....i m a newbie and wanted to ask if there is any difficulty using VS 2008 on Linux. I mean the way we use it on windows, i wanted to use it on Linux, will it make any difference? Any tutorial link is also welcome
|
|
|
|
|
You can't run Visual Studio on Linux.
|
|
|
|
|
Muzammil Saeed wrote: i m a newbie and wanted to ask if there is any difficulty using VS 2008 on Linux. I mean the way we use it on windows, i wanted to use it on Linux, will it make any difference?
You can't run Visual Studio 2008 on Linux, but you can create .NET-compatible applications using Mono[^]. The MonoDevelop-IDE[^] is a nice alternative to VS, albeit not as powerfull.
If you "can't live without" the VS IDE, then install it on Windows-machine. You can even make it look as if it were running locally, by using SeamlessRDP[^].
Enjoy I are Troll
|
|
|
|
|
Hi,
I'm trying to implement KeepAlive method in one connection by using the following code.
void SentMsg(String[] msg, int MsgNum )
{
try
{
String reqURI = "www.xxx.com"
ASCIIEncoding encoding = new ASCIIEncoding();
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(reqURI);
myReq.Method = "POST";
myReq.KeepAlive = true;
foreach (int index in MsgNum)
{
String readContent;
Byte[] byte = encoding.GetBytes(msg[index]);
myReq.ContentLength = byte.Length;
Stream requestStream = myReq.GetRequestStream();
requestStream.Write(byte, 0, byte.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)myReq.GetResponse();
Strean receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
readContent = readStream.ReadToEnd();
Console.Write(readContent);
readStream.Close();
}
}
}
There is no problem on the first request send and gotten response.
When sending the second request, the console shows the following message.
"This property cannot be set after writing has started."
Is my concept wrong when implementing KeepAlive method?modified on Tuesday, March 9, 2010 8:38 PM
|
|
|
|
|
please read and follow the guidelines[^] including #7. You can still edit the existing message.
|
|
|
|
|
I haven't created custom assembly attributes before, so maybe this isn't possible, but what I'd like to do is compile a custom assembly attribute and have it displayed to the user if they get the properties of the DLL.
I'm using C#, VS2008, and .NET 3.5. I've added custom attributes to my DLL (ala http://www.codinghorror.com/blog/2004/11/custom-assemblyinfo-attributes.html[^]). It compiles and I've verified that the custom attributes are in the compiled assembly (with Reflector), but when I select the DLL and view the properties (via Windows Explorer) and then view the 'Details' tab, my custom properties aren't displayed.
This is the first time Ive ventured into this area... is this possible? Or does Windows Server 2008 only display a list of known assembly attributes?
Thanks,
Jeff
|
|
|
|
|
The short answer is that this isn't possible. The explorer process in Windows only displays properties that it "knows" about. The assembly attributes that you are seeing are assembly attributes that the compiler adds to a Win32 resource which is embedded in the assembly. Those attributes map to specific entries in the resource, which the OS knows how to display.
It is possible to do this (for example, Office files like Word), but it isn't simple and can't be done from managed code.Scott Dorman Microsoft® MVP - Visual C# | MCPD
President - Tampa Bay IASA
[ Blog][ Articles][ Forum Guidelines] Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
|
|
|
|
|
Darn. I thought that might be the case. Thanks for the reply and confirmation.
Jeff
|
|
|
|
|
Hello all,
my employer Kryptiva and I have put out a first release of a new plugin for Microsoft Outlook 2003 and 2007 called echotracker.
echotracker is a mail indexer and personnal information aggregator that installs itself as an panel in Microsoft Outlook. The software isn't feature-complete but is already providing useful information about mails in Microsoft Outlook.
This is released under the terms of the LGPL 2.1 license which means it's OPEN-SOURCE! There aren't that many open-source Outlook plugin out there so I think this is interesting stuff for developers.
We need developers and courageous users to build and test first developer preview. We accept bug reports and patch on our mailing lists. I'd be glad to answer any kind of questions on this forum but we would prefer people to use mailing lists.
François-Denis Gonthier
|
|
|
|