I have added scripting ability in my application using sharpdevelop.and my gaol is lettign user write a project and then build it and run/debug it.Run is called by methodeinfo.invoke in the output assembly of the project.All of this is fine , my problem is that when the user script contains an unhandled exception. I have added an event handler to unhandledexceptions and an other for application exception.I can now get a custom message when an exception is thrown by the user code. but when I edit the code and try to rebuild I have an error MSB 3021 because the .pdb file is locked. Is there any solution for this??? the .pdb is locked by my app because it is invoked by the code .how can I kill the thread invoked or can I release the pdb file???
3 Answers
The PDB file is used by two chunks of code, both well out of your control. The debugger uses it to help you debug your code. And most significantly for this particular mishap, the CLR uses it when it generates the stack trace for the exception. Which is how it can display the source code + line number info in the trace.
Exactly how long that PDB file is going to be locked is very hard to see, the CLR uses a COM interface to read the file content. There are certainly non-zero odds that the lock stays in effect as long as the exception hasn't been caught by a catch
block. Possibly longer than that, not terribly likely.
Not a wholeheckofalot you can do about this. In a scenario like this, you typically have much bigger problems with the lock on the assembly that the CLR creates when it loads the assembly. A scripting runtime needs to deal with this by using an AppDomain so the scripting code can be unloaded again. It is not clear whether you are using an AppDomain and that you won't still have a problem with the assembly if you solve the PDB lock.
A quick way to find out is to simply not generate a PDB file, omit the /pdb option when you compile the scripting code. If you now get complaints about the DLL then you've got a significant chunk of work to do.

- 922,412
- 146
- 1,693
- 2,536
-
thanks for your replay.I have got the solution it is by using delegates: http://stackoverflow.com/questions/4117228/reflection-methodinfo-invoke-catch-exceptions-from-inside-the-method just I have added catch exception and not only targetinvocationexception it works well now. – Salihovic Nov 19 '13 at 15:33
You can start your thread using a CancellationToken.
For example :
var cancelToken = new CancellationTokenSource();
Task.Factory.StartNew(() => yourMethod.Invoke(), cancelToken.Token);
This will stop the Task:
// false indicates that no exceptions will be thrown.
cancelToken.Cancel(false);
This way, you can cancel your thread when the exceptions are thrown.

- 16,728
- 6
- 57
- 64

- 1
- 1
-
I have tryed it but is not working.when the exception is thrown it is catched in the methode invoking but the pdb file is locked.May be the task cancellation is not enough to stop the running thread. – Salihovic Nov 19 '13 at 10:36
I have resolved my problem using delegate approach in here: Reflection MethodInfo.Invoke() catch exceptions from inside the method whith this method I have no problem now with unhandled exception and the .pdb file is not used by my app process. so I can change it .