先生们,
我找到了一种方法,可以在窗口不可见的情况下运行AutoCAD Core控制台,将secureload设置为0,然后Netload一个dll,运行一个带有参数、GUID字符串的.NET Lisp应用程序。此字符串是进程间通信的管道名称。这允许外部应用程序优雅地处理DWG
与AutoCAD相比,AutoCAD Core控制台本身使用的RAM非常少。当它空闲时,它使用0%的CPU功率。我遇到的问题是,我的应用程序占用了大量CPU资源。我的英特尔I9 CPU有10个内核,20个线程。它100%使用一个线程,占整个CPU的5%。当程序处于空闲状态时,我想将其减少到~0.1%
以下是我在AutoCAD Core控制台中运行的代码
- using AcConsole;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.Runtime;
- using Clifton.Core.Pipes;
- using NetDBX;
- using System.Collections.Generic;
- using System.IO;
- namespace AcConsoleIn
- {
- ///
- /// Provides static methods to run within AutoCAD Core Console
- ///
- public static class Run
- {
- // IPC Pipe
- private static ClientPipe Client { get; set; }
- ///
- /// Provides the Lisp Function to connect this process to an external application.
- ///
- ///
- [LispFunction("PROCESS")]
- public static void Process(ResultBuffer buffer)
- {
- try
- {
- using (buffer)
- {
- // Process input
- List args = AutoLisp.HandleLispArguments(buffer, 1, 1);
- // Create and connect IPC pipe
- string pipeName = AutoLisp.LispToString(args[0]);
- Client = new ClientPipe(".", pipeName, p => p.StartStringReaderAsync());
- Client.DataReceived += Client_DataReceived;
- Client.Connect();
- }
- }
- catch { }
- }
- ///
- /// Event Handler for processing data from server.
- ///
- ///
- ///
- private static void Client_DataReceived(object sender, PipeEventArgs e)
- {
- // Exit if communication pipe is broken
- if (Client == null)
- return;
- try
- {
- // Check for a close application message
- if (e.String == "Close Application")
- {
- // Disconnect Pipe
- Client.DataReceived -= Client_DataReceived;
- Client.Close();
- Client = null;
- // Close AutoCAD
- Autodesk.AutoCAD.ApplicationServices.Core.Application.Quit();
- return;
- }
- // Tell server that this is busy
- Client.WriteString("1");
- // Get database object
- AcConsole.DatabaseServices.Database database;
- database = IO.XmlDeserializeFromString(e.String);
- // Throw error if it received an invalid database object
- if (database == null)
- throw new InvalidDataException("Invalid Database Object");
- // Create new drawing
- using Database drawing = new Database(true, true);
- using (Transaction transaction = drawing.TransactionManager.StartTransaction())
- {
- // Convert AcConsole.DatabaseServices.Database to Autodesk.AutoCAD.DatabaseServices.Database
- database.ToDatabase(drawing, transaction);
- transaction.Commit();
- }
- // Delete file if it exists
- string filename = database.Filename;
- if (File.Exists(filename))
- File.Delete(filename);
- // Save drawing
- drawing.SaveAs(filename, DwgVersion.Current);
- // Tell Server it is not busy.
- Client.WriteString("0");
- }
- catch (System.Exception ex)
- {
- // Tell server that an error occured.
- Client.WriteString(ex.Message);
- }
- }
- }
- }
我想如果我使用事件处理程序,当它空闲时,我不会使用任何CPU电源。有人能帮我吗?
本帖以下内容被隐藏保护;需要你回复后,才能看到! 游客,如果您要查看本帖隐藏内容请 回复 |