MARK:待解决。
ArcEngine自定义方法添加图层时,内存无法释放,而且关闭主控程序后,内存管理器中一直驻留EXE。
1. 命令方式:
ControlsAddDataCommandClass command = new ControlsAddDataCommandClass();
command.OnCreate(m_mapControl.Object);
command.OnClick();
2. 手工方式:
/加载图层并设置图层样式
IWorkspaceFactory pWSFactory = new AccessWorkspaceFactoryClass();
IWorkspace pWorkspace = pWSFactory.OpenFromFile(AppProperty.gcMDBPath, 0);
IFeatureWorkspace pFWS = pWorkspace as IFeatureWorkspace;
IFeatureClass pFeatureClass = pFWS.OpenFeatureClass("工程");
IFeatureLayer pFeatureLayer = new FeatureLayerClass();
pFeatureLayer.Name = "工程";
pFeatureLayer.FeatureClass = pFeatureClass;
IGeoFeatureLayer pGeoFeatureLayer = (IGeoFeatureLayer)pFeatureLayer;
ISimpleFillSymbol simpleFillSymbol = new SimpleFillSymbolClass();
simpleFillSymbol.Style = esriSimpleFillStyle.esriSFSCross;
ILineSymbol outLineSymbol = new SimpleLineSymbolClass();
outLineSymbol.Width = 1;
outLineSymbol.Color = GetColor(140, 140, 140, 100);
simpleFillSymbol.Outline = outLineSymbol;
simpleFillSymbol.Color = GetColor(140, 140, 140, 100);
ISimpleRenderer simpleRender = new SimpleRendererClass();
simpleRender.Symbol = simpleFillSymbol as ISymbol;
pGeoFeatureLayer.Renderer = simpleRender as IFeatureRenderer;
_mapControl.Map.AddLayer(pGeoFeatureLayer);
彻底的释放也不管用。
System.Runtime.InteropServices.Marshal.ReleaseComObject(pFWS);
System.Runtime.InteropServices.Marshal.ReleaseComObject(pWorkspace);
System.Runtime.InteropServices.Marshal.ReleaseComObject(pWSFactory);
IMapControl3 _mapControl = (IMapControl3)axMapControlGHT.Object;
for (int i = 0; i < _mapControl.Map.LayerCount; i++)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(_mapControl.Map.get_Layer(i));
}
******************************************************
无奈采用强制关闭EXE的方法释放内存
原文:http://kenter1643.javaeye.com/blog/451973
主程序Program.cs代码
- static class Program
- {
- /// <summary>
- /// 应用程序的主入口点。
- /// </summary>
- [STAThread]
- static void Main()
- {
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- Application.Run(new MainForm());
- //强制关闭进程
- string exeName = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
- string[] exeArray = exeName.Split('//');
- FunctionClass.RunCmd("taskkill /im " + exeArray[exeArray.Length-1] + " /f ");
- }
- }
强制关闭方法:
- /// <summary>
- /// 运行DOS命令
- /// DOS关闭进程命令(ntsd -c q -p PID )PID为进程的ID
- /// </summary>
- /// <param name="command"></param>
- /// <returns></returns>
- public static string RunCmd(string command)
- {
- //實例一個Process類,啟動一個獨立進程
- System.Diagnostics.Process p = new System.Diagnostics.Process();
- //Process類有一個StartInfo屬性,這個是ProcessStartInfo類,包括了一些屬性和方法,下面我們用到了他的幾個屬性:
- p.StartInfo.FileName = "cmd.exe"; //設定程序名
- p.StartInfo.Arguments = "/c " + command; //設定程式執行參數
- p.StartInfo.UseShellExecute = false; //關閉Shell的使用
- p.StartInfo.RedirectStandardInput = true; //重定向標準輸入
- p.StartInfo.RedirectStandardOutput = true; //重定向標準輸出
- p.StartInfo.RedirectStandardError = true; //重定向錯誤輸出
- p.StartInfo.CreateNoWindow = true; //設置不顯示窗口
- p.Start(); //啟動
- //p.StandardInput.WriteLine(command); //也可以用這種方式輸入要執行的命令
- //p.StandardInput.WriteLine("exit"); //不過要記得加上Exit要不然下一行程式執行的時候會當機
- return p.StandardOutput.ReadToEnd(); //從輸出流取得命令執行結果
- }