What's the cause of this exception in Windows Phone - windows-phone-7

It's a little strange that my Windows Phone App will exit without any Warning by chance, most of the time it works fine.
Then I trace the Application_UnhandledException, find that the Exception message is:
[ExceptionMessage]:[NullReferenceException]
[StackTrace]:[
at wpapp.MainPage.<DispatcherLoad>b__1(Object sender, EventArgs e)
at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
]
There is no detailed information for me to find out the cause of the exception. Has anybody met this exception before and got a solution for it?
Any suggestions will be appreciated.

From the look of it, it's likely that you have a DispatcherLoad method in your MainPage, and you're calling a lambda function in that method. Something like:
private void DispatcherLoad()
{
this.Dispatcher.BeginInvoke(() => Console.WriteLine("hello world;"));
}
The error is occuring in the lambda (in my sample: the Console.WriteLine("hello world;") part). So now you just have to find the right lambda, and find out why your code crashes.
Given the "object sender, EventArgs e" parameters, it's probably an event handler. Are you assigning a lambda to an event handler somewhere in the DispatcherLoad function? For instance:
private void DispatcherLoad()
{
this.Button.Click += (sender, e) => Console.WriteLine("hello world;");
}
Note: if there's many lambdas in your method and you can't figure which one is crashing, you can try opening your assembly with Reflector (http://www.reflector.net/). It will decompile your dll, and you can then see which lambda is called "<DispatcherLoad>b__1".

Related

How to access HttpContext.Current.Application on OWIN Startup

I need to fill HttpContext.Current.Application["Translations"] on OWIN startup, but HttpContext.Current is null there.
Should I use Application_Start() in Global.asax.cs or it is bad idea?
What alternatives do I have?
HttpContext is not available at start up as it cannot exist without a request. The Startup class only runs once for the application, not for each request.
Try accessing it in the BeginRequest handler in Global.asax.cs.
protected void Application_BeginRequest(object sender, EventArgs e) {
base.Application["Translations"] = "My value here";
}

ICommentEvent runs TWICE in ONE comment event Sitefinity

I have these code, according to document from sitefinity:
protected void Application_Start(object sender, EventArgs e)
{
Bootstrapper.Initialized += new EventHandler<ExecutedEventArgs>(Bootstrapper_Initialized);
}
public void Bootstrapper_Initialized(object sender, ExecutedEventArgs args)
{
if (args.CommandName == "Bootstrapped")
{
EventHub.Subscribe<ICommentEvent>(evt => CommentsEvent.CommentEventHandler(evt));
}
}
And the handler:
public static void CommentEventHandler(ICommentEvent evt)
{
// My code here
}
The problem is this handler always runs twice when a comment event happens (post a comment or approve a comment).
Could you please tell me why this happens and any possible way to avoid this? (I don't believe static boolean is a good idea).
Thanks
ICommentEvent is a base interface that is implemented by multiple events such as ICommentCreatingEvent, ICommentCreatedEvent, ICommentUpdatingEvent, ICommentUpdatedEvent and some others.
In your case it is fired twice due to firing of both ICommentCreatingEvent and ICommentCreatedEvent.
You can subscribe to just one of them and it should fire just once.

MS.Internal.XcpImports.MethodEx(IntPtr ptr, String name, CValue[] cvData

I am getting an error "Value does not fall within the expected range". The stacktrace is given below. Please help...
at MS.Internal.XcpImports.MethodEx(IntPtr ptr, String name, CValue[] cvData)
at MS.Internal.XcpImports.MethodPack(IntPtr objectPtr, String methodName, Object[] rawData)
at MS.Internal.XcpImports.UIElement_TransformToVisual(UIElement element, UIElement visual)
at Microsoft.Phone.Controls.Pivot.ReleaseMouseCaptureAtGestureOrigin()
at Microsoft.Phone.Controls.Pivot.OnManipulationDelta(Object sender, ManipulationDeltaEventArgs args)
at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
Might be you are trying to access main thread object or UI thread object in a asynchronous function.because asynchronous function's thread has lower priority so you cant access the main thread object from here..

Eventhandler is fired more than once

I have a problem with two of my EventHandlers, they work the same, so here is one:
private void Form1_Load(object sender, EventArgs e)
{
webBrowserWebsite.Url = new System.Uri(textBoxURL.Text, System.UriKind.Absolute);
webBrowserWebsite.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowserWebsite_DocumentCompleted);
}
void webBrowserWebsite_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + #"\CurrentData.wfd");
sw.Write(webBrowserWebsite.Document.Body.InnerText);
sw.Close();
}
The problem is, that the EventHandler fires multiple times, it doesn't stop!
Why is it doing this?
Thanks in advance
The code that you have written won't compile (your StreamWriter in your EventHandler isn't assigned to anything) and without more context as to how you are calling this, it is difficult to say for certain.
But the most likely reason is you are calling Form1_Load multiple times, but using the same webBrowserWebsite object. Each time the form loads, you are adding a new Event Handler. And since you aren't showing any code showing where you removing the event handler, I'm guessing it fires once for each time you call Form_Load.
Depending on your design, you are better off adding the event handler in the constructor so it is only added once regardless of the number of times you load the form.
public Form1()
{
webBrowserWebsite.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowserWebsite_DocumentCompleted);
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowserWebsite.Url = new System.Uri(textBoxURL.Text, System.UriKind.Absolute);
}
Or remove the event handler in the event handler:
void webBrowserWebsite_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + #"\CurrentData.wfd");
sw.Write(webBrowserWebsite.Document.Body.InnerText);
sw.Close();
webBrowserWebsite.DocumentCompleted -= webBrowserWebsite_DocumentCompleted;
}
Also, since StreamWriter implements IDisposible, you should be putting it inside of a using block or at least calling sw.Dispose() at the end of the method

How to customize the method templates in Visual Studios

Is there a way to customize/edit the default template of the event handling methods in Visual Studios 2008/2010 such that upon creation, the event handler already has a try/catch block in it?
the default behavior is
private void button1_Click(object sender, EventArgs e)
{
}
I'd like to be able to set the development environment to do
private void button1_Click(object sender, EventArgs e)
{
try
{
///TODO: Add logic here
}
catch
{
throw;
}
finally
{
}
}
I've already edited the templates located in the ItemTemplates folder so that they meet the coding standard where I work, but I'd also like to edit the method templates if possible. I've searched the 'Net for the better part of a week and found nothing. Any assistance would be greatly appreciated.
![Snippet Manager1
Have you considered Editing Event method stub in the Code Snippet manager or adding a new snippet?

Resources