c# 如何释放非托管外部dll资源 我的程序中使用计时器不停的调用外部dll文件进行计算,有的时候运行时间比较长后,cpu负荷会升高,我怀疑是外部非托管资源没有释放,请问如何释放? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1、定义一个实现了IDispose的类class A:IDisposable { #region IDisposable 成员 void IDisposable.Dispose() { Dispose(true); GC.SuppressFinalize(this); } #endregion protected virtual void Dispose(bool disposing) { if (disposing) { // 做一些释放资源的操作,比如关闭文件连接,或者非托管资源的连接 } } // 这种方式为可终结类型释放资源 ~A() { Dispose(false); } }2、调用A的示例: A a = new A(); if (a is IDisposable) { ((IDisposable)a).Dispose(); } GC.SuppressFinalize(this); 微软不建议用户直接调用GC.*方法。也不要用析构函数了,直接dispose()方法应该可行了吧 //Dll操作类-将要执行的函数转换为委托/* * //下面代码进行调用 * public delegate int Compile(String command, StringBuilder inf);//编译 * My_DllInvoke dll = new My_DllInvoke(Server.MapPath(@"~/Bin/dll.dll")); * Compile compile = (Compile)dll.Invoke("Compile", typeof(Compile)); * StringBuilder inf; * compile(@“gcc a.c -o a.exe“,inf); //这里就是调用我的DLL里定义的Compile函数 */public class My_DllInvoke{ [DllImport("kernel32.dll")] private extern static IntPtr LoadLibrary(String path); [DllImport("kernel32.dll")] private extern static IntPtr GetProcAddress(IntPtr lib, String funcName); [DllImport("kernel32.dll")] private extern static bool FreeLibrary(IntPtr lib); private IntPtr hLib; public My_DllInvoke(String DLLPath) { hLib = LoadLibrary(DLLPath); } ~My_DllInvoke() { FreeLibrary(hLib); } //将要执行的函数转换为委托 public Delegate Invoke(String APIName,Type t) { IntPtr api = GetProcAddress(hLib, APIName); return (Delegate)Marshal.GetDelegateForFunctionPointer(api,t); }} 关键是在dispose中怎么写释放代码,尤其是DLL 怎样用正则表达式匹配到没有指定单词的行 奇怪的问题:获取硬盘ID,GetInstances()报错 请教高人,关于c#打开pdf文件??? 谢谢先 服务端与客户端的数据双向同步设计问题 我在C#下用到数据库时要比较两个时间日期类型字段的大小,请问怎么比较啊? C#程序操作大量数据Form UI像死机一样,想用委托怎样解决? 【请教】TCP连接出现“通常每个套接字地址(协议/网络地址/端口)只允许使用一次”错误 C#如何获取intptr所指对象的内容? listbox问题。 hongliangy(豆豆)给分,请进! 大家觉得linq怎么样,有人说linq已死 C#label怎么竖着显示
class A:IDisposable
{
#region IDisposable 成员 void IDisposable.Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
} #endregion protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// 做一些释放资源的操作,比如关闭文件连接,或者非托管资源的连接
}
}
// 这种方式为可终结类型释放资源
~A()
{
Dispose(false);
}
}2、调用A的示例:
A a = new A();
if (a is IDisposable)
{
((IDisposable)a).Dispose();
}
微软不建议用户直接调用GC.*方法。
也不要用析构函数了,直接dispose()方法应该可行了吧
/*
* //下面代码进行调用
* public delegate int Compile(String command, StringBuilder inf);//编译
* My_DllInvoke dll = new My_DllInvoke(Server.MapPath(@"~/Bin/dll.dll"));
* Compile compile = (Compile)dll.Invoke("Compile", typeof(Compile));
* StringBuilder inf;
* compile(@“gcc a.c -o a.exe“,inf); //这里就是调用我的DLL里定义的Compile函数
*/
public class My_DllInvoke
{
[DllImport("kernel32.dll")]
private extern static IntPtr LoadLibrary(String path);
[DllImport("kernel32.dll")]
private extern static IntPtr GetProcAddress(IntPtr lib, String funcName);
[DllImport("kernel32.dll")]
private extern static bool FreeLibrary(IntPtr lib);
private IntPtr hLib;
public My_DllInvoke(String DLLPath)
{
hLib = LoadLibrary(DLLPath);
}
~My_DllInvoke()
{
FreeLibrary(hLib);
}
//将要执行的函数转换为委托
public Delegate Invoke(String APIName,Type t)
{
IntPtr api = GetProcAddress(hLib, APIName);
return (Delegate)Marshal.GetDelegateForFunctionPointer(api,t);
}
}