object dispose

L

laputa

Dear all :

I create a simple code (window application) like following
Private Sub Button1_Click()
Dim Obj As Object
Dim asm As Assembly = Assembly.LoadFrom("TestApp.dll")
Dim ty As Type = asm.GetType("TestApp.appServ")
Obj = Activator.CreateInstance(ty)
Dim Istring As String = Obj.funct()
MessageBox.Show(Istring)
Obj.Dispose()
GC.SuppressFinalize(Obj)

Obj = Nothing
end sub

while TestApp.dll code follow that
using System;
using System.Collections.Generic;
using System.Text;

namespace TestApp
{
public class appServ : IDisposable
{
private bool disposed = false;
public string funct()
{
return "aad";
}
~appServ()
{

// call Dispose with false. Since we're in the
// destructor call, the managed resources will be
// disposed of anyways.
Dispose(false);
}

protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
// dispose-only, i.e. non-finalizable logic
}

// shared cleanup logic
disposed = true;
}
}



public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
GC.Collect();
}

}
}



Before the button click the "TestApp.dll" allow be overwrite
but after button click , the file is locked until the main program exit.
Can anyone tell me how to solve it ?

Thanks

Michael
 
M

miher

Hi,

As I know this happens because You load the assembly and after You done
that, the assembly file cannot be deleted, it has nothing to do with the
object being disposed of or not. Appdomains not allow unloading assemblies,
only a full appdomain can be unloaded. So if You wish to unload the
assembly, You might want to create a new appdomain for it and load the
assembly into that.

Hope You find this useful.
-Zsolt
 
R

Rohit

Hi,

As I know this happens because You load the assembly and after You done
that, the assembly file cannot be deleted, it has nothing to do with the
object being disposed of or not. Appdomains not allow unloading assemblies,
only a full appdomain can be unloaded. So if You wish to unload the
assembly, You might want to create a new appdomain for it and load the
assembly into that.

Hope You find this useful.
-Zsolt
















- Show quoted text -

Another solution would be

First create a shadow copy of the dll and load that dll so that your
main dll can be overwritten and later u can refersh your shadow copy
if your main copy is changes.

it would same like caching the assembly.

Regards
Rohit
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top