Shamil
Posts: 7
Joined: 2010-02-06
|
Hi All --
Could you please advise how to change the folowing code (of ADX for .NET MS Access COM Add-In) to have COM references immediately released on exiting fr om every method?
Please feel free to give just a link to topics/threads here or somewhere on Internet wh ere the subject issue solution is presented.
Thank you.
--
Shamil
P.S. Code sample
...
public void InitDat abase()
{
dao.Database dbs = null;
try
{
dbs = _application.CurrentDb();
if (dbs == null)
throw new ApplicationException(
string.Format("CurrentDatabase is not available, dbs = null")
);
... do some custom actions with dbs here ...
}
catch { }
finally
{
dbs = null;
_database = new AccessDat abase(this);
}
}
public string CurrentDatabaseFullPath
{
get
{
dao.Database dbs = _application.CurrentDb();
string fullPath = dbs.Name;
dbs = null;
return fullPath;
}
}
Note: _application is a reference to COM Add-In's MS Access application inatance set on add-in's initialization phase - no need to release it.
private acc.Application _application;
public void SetHostApplication(object app)
{
_application = app as acc.Application;
}
... |
|
Andrei Smolin
Add-in Express team
Posts: 18791
Joined: 2006-05-11
|
Hi Shamil,
public void InitDatabase()
{
dao.Database dbs = null;
try
{
dbs = _application.CurrentDb();
if (dbs == null)
throw new ApplicationException(
string.Format("CurrentDatabase is not available, dbs = null")
);
... do some custom actions with dbs here ...
}
catch { }
finally
{
if (dbs != null) Marshal.ReleaseComObject(dbs);
dbs = null;
_database = new AccessDatabase(this);
}
}
public string CurrentDatabaseFullPath
{
get
{
dao.Database dbs = _application.CurrentDb();
string fullPath = dbs.Name;
if (dbs != null) Marshal.ReleaseComObject(dbs);
dbs = null;
return fullPath;
}
}
private acc.Application _application;
public void SetHostApplication(object app)
{
_application = app as acc.Application;
// no need to release anything
}
Please see http://www.add-in-express.com/creating-addins-blog/2008/10/30/releasing-office-objects-net/
Andrei Smolin
Add-in Express Team Leader |
|
Shamil
Posts: 7
Joined: 2010-02-06
|
|