使用P/Invoke 實作CreateJobObject 和SetInformationJobObject 的範例
此範例示範如何建立作業物件(CreateJob資訊(SetInformationJobObject),以確保在作業執行時與作業關聯的程序終止
using System; using System.Diagnostics; using System.Runtime.InteropServices; namespace JobObjectPInvoke { class Program { const int JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE = 0x2000; [DllImport("kernel32.dll", CharSet = CharSet.Unicode)] static extern IntPtr CreateJobObject(IntPtr a, string lpName); [DllImport("kernel32.dll")] static extern bool SetInformationJobObject(IntPtr hJob, JobObjectInfoType infoType, IntPtr lpJobObjectInfo, UInt32 cbJobObjectInfoLength); [DllImport("kernel32.dll", SetLastError = true)] static extern bool CloseHandle(IntPtr hObject); static void Main() { // Get a handle to the current process. IntPtr currentProcessHandle = Process.GetCurrentProcess().Handle; // Create a job object. IntPtr jobObjectHandle = CreateJobObject(IntPtr.Zero, null); if (jobObjectHandle == IntPtr.Zero) { throw new Exception($"Failed to create job object: {Marshal.GetLastWin32Error()}"); } // Set the limit information for the job object. JOBOBJECT_BASIC_LIMIT_INFORMATION jobLimitInfo = new JOBOBJECT_BASIC_LIMIT_INFORMATION { LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE }; int sizeOfJobLimitInfo = Marshal.SizeOf(jobLimitInfo); IntPtr ptrJobLimitInfo = Marshal.AllocHGlobal(sizeOfJobLimitInfo); Marshal.StructureToPtr(jobLimitInfo, ptrJobLimitInfo, false); if (!SetInformationJobObject(jobObjectHandle, JobObjectInfoType.BasicLimitInformation, ptrJobLimitInfo, (uint)sizeOfJobLimitInfo)) { throw new Exception($"Failed to set job limit information: {Marshal.GetLastWin32Error()}"); } // Add the current process to the job object. if (!AssignProcessToJobObject(jobObjectHandle, currentProcessHandle)) { throw new Exception($"Failed to add process to job object: {Marshal.GetLastWin32Error()}"); } // Sleep for 10 seconds. System.Threading.Thread.Sleep(10000); // Close the job object. CloseHandle(jobObjectHandle); // The current process will terminate when the job object is closed. You can use this example to create a sandbox environment for your processes. } #region Helper Classes [StructLayout(LayoutKind.Sequential)] struct JOBOBJECT_BASIC_LIMIT_INFORMATION { public Int64 PerProcessUserTimeLimit; public Int64 PerJobUserTimeLimit; public UInt32 LimitFlags; public UIntPtr MinimumWorkingSetSize; public UIntPtr MaximumWorkingSetSize; public UInt32 ActiveProcessLimit; public UIntPtr Affinity; public UInt32 PriorityClass; public UInt32 SchedulingClass; } public enum JobObjectInfoType { AssociateCompletionPortInformation = 7, BasicLimitInformation = 2, BasicUIRestrictions = 4, EndOfJobTimeInformation = 6, ExtendedLimitInformation = 9, SecurityLimitInformation = 5 } #endregion } }
這個擴充範例說明了建立作業物件、設定其基本限制資訊以及向作業物件新增流程的完整實作。 JOBOBJECT_LIMIT_KILL_ON_JOB_CLOSE 標誌確保當作業物件關閉時,與作業關聯的任何進程都會被終止,從而確保進程使用的所有資源都被正確清理。
以上是如何在C#中使用P/Invoke建立和管理作業物件以確保進程終止?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本文解釋了C標準模板庫(STL),重點關注其核心組件:容器,迭代器,算法和函子。 它詳細介紹了這些如何交互以啟用通用編程,提高代碼效率和可讀性t

本文詳細介紹了c中有效的STL算法用法。 它強調了數據結構選擇(向量與列表),算法複雜性分析(例如,std :: sort vs. std vs. std :: partial_sort),迭代器用法和並行執行。 常見的陷阱

本文詳細介紹了C中的有效異常處理,涵蓋了嘗試,捕捉和投擲機制。 它強調了諸如RAII之類的最佳實踐,避免了不必要的捕獲塊,並為強大的代碼登錄例外。 該文章還解決了Perf

本文討論了使用C中的移動語義來通過避免不必要的複制來提高性能。它涵蓋了使用std :: Move的實施移動構造函數和任務運算符,並確定了關鍵方案和陷阱以有效

C 20範圍通過表現力,合成性和效率增強數據操作。它們簡化了複雜的轉換並集成到現有代碼庫中,以提高性能和可維護性。

本文討論了C中的動態調度,其性能成本和優化策略。它突出了動態調度會影響性能並將其與靜態調度進行比較的場景,強調性能和之間的權衡

文章討論了在C中有效使用RVALUE參考,以進行移動語義,完美的轉發和資源管理,重點介紹最佳實踐和性能改進。(159個字符)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

Dreamweaver CS6
視覺化網頁開發工具

Dreamweaver Mac版
視覺化網頁開發工具

記事本++7.3.1
好用且免費的程式碼編輯器

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。