CEF Sharp causing an Object Disposed Exception - c#

While I understand what an object disposed exception is, I don't quite know why it is occurring immediately after the object is instantiated. Below is my code:
var cookiemanager = Cef.GetGlobalCookieManager();
cookiemanager.SetCookieAsync(Domain, Cookie);
The error is occurring on the second line when I attempt to call the SetCookieAsync function stating:
An exception of type 'System.ObjectDisposedException' occurred in CefSharp.Core.dll but was not handled in user code
Any help on why the object was disposed or what I can do to remedy this error would be greatly appreciated!

After some more tinkering I decided to look at the provided Cef examples where I found the following snippet:
Cef.OnContextInitialized = delegate
{
var cookiemanager = Cef.GetGlobalCookieManager();
cookiemanager.SetCookieAsync(Domain, Cookie);
};
Making this changed seemed to work!

Related

Can I get the method that was first called in an error routine?

I am trying to get the method that called a method in an assembly that caused an error.
I have an error routine that logs errors. I would like to get the calling assembly and method along with the assembly and method where the error occurred. I got 3 out of 4, I just cant figure out how to get the calling method.
Say I have a form and a button is clicked that calls a method in another asssembly that errors. I want the assembly and method where the error occurs along with the form and click event. The click event is what I can't find.
VB
Dim Trace As System.Diagnostics.StackTrace
Trace = New System.Diagnostics.StackTrace(ex, True)
'Returns the assembly where the error occurred
Trace.GetFrame(Trace.FrameCount - 1).GetMethod.ReflectedType.FullName
'Returns the calling assembly
Assembly.GetCallingAssembly.FullName.Split(","c)(0)'Would be nice to get the class too but I can live without it.
'Method where the error occurs
ex.TargetSite.Name
'Initial calling method
???
C#
System.Diagnostics.StackTrace Trace;
Trace = New System.Diagnostics.StackTrace(ex, True);
//Returns the assembly where the error occurred
Trace.GetFrame(Trace.FrameCount - 1).GetMethod().ReflectedType.FullName;
//Returns the calling assembly
Assembly.GetCallingAssembly().FullName.Split(',')[0];//Would be nice to get the class too but I can live without it.
//Method where the error occurs
ex.TargetSite.Name;
//Initial calling method
???
I can see the source in the stack trace (Main.btnCancel_CLick but can not get it. My stacktrace frame count is 1 and I have one method in there, the one with the try catch block. Unless there's some other way to get to it that I can't figure out. Anyone know?

'System.Reflection.TargetInvocationException' when calling Activator.CreateInstance()

I am receiving this error:
A first chance exception of type 'System.Reflection.TargetInvocationException'
occurred in mscorlib.dll
and also:
Exception has been thrown by the target of an invocation.
when running this method, and the error is occurring on the CreateInstance(Type.GetType(instantiationString), parameters) call:
public static Report instantiateReport(ReportInfo reportInfo)
{
string instantiationString;
Report reportToReturn;
ReportInfo[] parameters = new ReportInfo[1];
parameters[0] = reportInfo;
instantiationString = reportInfo.Report.InstantiationPath;
reportToReturn = (Report)Activator.CreateInstance(Type.GetType(instantiationString), parameters);
return reportToReturn;
}
ReportInfo is just a custom class that contains variables that help me build the report that I am trying to instantiate and when I investigate it, everything is completely fine. The instantiationString just helps me understand which report is trying to be created at the call and that way I can use the Type.GetType() method. I really have no idea what kind of error this is or what could be going wrong. I researched it a bit but didn't receive any helpful answer so any help is appreciated.
EDIT After looking at the inner exception I am getting Object reference not set to an instance of an object. message. This is strange because neither the ReportInfo or the GetType() methods have a null reference. Say one of the variables within ReportInfo was null, could that cause an issue? It shouldn't right?
FIXED After delving into the stack trace of the inner exception I found that there was some conflicting code causing issues elsewhere. Problem solved.

'System.Threading.Tasks.TaskCanceledException' occurred in WindowsBase.dll when closing application

I have this property in my viewmodel.
public bool IsKWH
{
get { return _isKwh; }
set
{
if (value.Equals(_isKwh)) return;
_isKwh = value;
NotifyOfPropertyChange(() => IsKWH);
}
}
Sometimes (~1 in 10 times) when I close down my application I get the following error in NotifyOfPropertyChange:
An exception of type 'System.Threading.Tasks.TaskCanceledException' occurred in WindowsBase.dll but was not handled in user code
Additional information: A task was canceled.
I have a System.Threading.Timer in my view model that is making a webservice call to update this and many other properties.
I am using Caliburn.Micro and it seems to have started happening when I updated from 1.5 to 2.0.
Is there anyway to prevent this error from occurring?
It is possible that intermittently your application fails to dispose of any secondary threads that it is using before the application closes. This can often cause an error message such as the one you posted. Could I suggest trying the following:
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
// close all active threads
Environment.Exit(0);
}
This should force the application to terminate all active threads before it closes. I recall having a similar problem and that particular little fix solved it. Might be worth giving it a try, let me know if it doesn't help and we can see what other solutions there might be. Hope this works for you.
FWIW, that fix didn't help me. the problem was coming from a 3rd party DLL that's dynamically loaded. I was unable to stop the exception from getting thrown, however I ignore the exception in the app exception handler:
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += new UnhandledExceptionEventHandler(
(sender2, args) =>
{
Exception ex = (Exception)args.ExceptionObject;
// unloading dragon medical one
if (ex is TaskCanceledException)
return; // ignore

a method call and avoid the dll not found error all the time if exists

This method below is called when you click save all button.
I want to ask you is there any way to skip the error under the code shown below.
Why I ask this: Some times the pDenemeProxy.dll does not exist in the folder of the code.
Morever it is a windows form application. Has the pDenemeProxy.dll in the references. And the fDenemeProxy facade of pDenemeProxy.dll is only initialized if the mDesTemp not null.
Thank you!
private bool SaveAll()
{
...
..
..
if (this.mDesTemp != null)
{
fDenemeProxy dnm = new fDenemeProxy();
dnm.SaveThisCustomer(1234,"D",true);
}
...
..
return;
}
Error: System.IO.FileNotFoundException: 'pDenemeProxy, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'
note: .net 2.0 and c#
note: Some people advice to put try catch block but it did not work. I have seen during the debug sessions on VS 2008 that when mDesTemp is null we see again the error declared above.
In some line in your code you are using a method which throws an Exception of type System.IO.FileNotFoundException if the conditions for this erroneous situation are true (i.e. you are trying to access a file which is not possible for some reason).
That's the intended and correct behavior what you are experiencing. Whenever you are getting this error message the error has already been happened and now it's up to you to deal with this new situation. That is what Exception-Handling is all about.
To deal with an error that was caused by an exception (informally speaking) you would have to catch a exception that has been thrown before (formally speaking).
To do that you have to enclose the portion of code (the actual method call that inheres the throwing of the exception) with a so-called try-catch block like this:
private bool SaveAll()
{
...
..
..
if (this.mDesTemp != null)
{
try {
fDenemeProxy dnm = new fDenemeProxy();
dnm.SaveThisCustomer(1234,"D",true);
} catch (FileNotFoundException e) {
// deal with the new situation !
}
}
...
..
return;
}
The meaning of that is very simple and intuitive:
Inside the try-block you are 'securing' a piece of code that is capable of throwing an exception for the case it is doing so. This try - block is then followed by an arbitrary number of catch-block - one for each exception that could be thrown by the secured code.
If you have set up this try-catch block correctly you have achieved that whenever your (secured) code throws an exception the execution flow of your program doesn't end (i.e. you program doesn't crash) but it goes to the aproprirate catch-block where you can do anything to deal with error you have just experienced.
Furthermore if you would look on the internet you will find lots of information on that since exception-handling is a very important concept of programming but what I've tried to explain here is the basic concept which you should try to understand first - it won't get more difficult ;)

'System.Threading.ThreadAbortException' in ASP.net page

I have the below code in my ASP.net page:
Response.StatusCode = 404
Response.Write(strResult)
Response.End()
The code above throws "An exception of type 'System.Threading.ThreadAbortException' occurred and was caught."
Can anyone tell me the reason why? And do I solve this.
thanks
Calling Response.End() from any child object of the page will most likely cause the 'System.Threading.ThreadAbortException' exception.
Please read this for a full explaination of why this is happening and methods to avoid/deal with the exception.

Categories

Resources