我想依次執行幾個exe文件。我需要執行第一個進程,並且只將另一個進程(第二個進程)加載到RAM中。當第一個過程完成時,第二個過程開始。由於第二個進程已經被加載,我們沒有延遲在它們之間切換。如何只在RAM中加載進程但不能運行(啓動)? (c#)
回答
首先想到的是對Win32 API使用較低級別的調用。您可以創建一個暫停的進程,然後恢復調用ResumeThread的主線程。
也許下面的代碼可以幫到你嗎? 編輯:增加了一些意見可能有助於更好地理解代碼:
// Structures needed by CreateProcess API
public struct PROCESS_INFORMATION
{
public IntPtr hProcess;
public IntPtr hThread;
public uint dwProcessId;
public uint dwThreadId;
}
public struct STARTUPINFO
{
public uint cb;
public string lpReserved;
public string lpDesktop;
public string lpTitle;
public uint dwX;
public uint dwY;
public uint dwXSize;
public uint dwYSize;
public uint dwXCountChars;
public uint dwYCountChars;
public uint dwFillAttribute;
public uint dwFlags;
public short wShowWindow;
public short cbReserved2;
public IntPtr lpReserved2;
public IntPtr hStdInput;
public IntPtr hStdOutput;
public IntPtr hStdError;
}
public struct SECURITY_ATTRIBUTES
{
public int length;
public IntPtr lpSecurityDescriptor;
public bool bInheritHandle;
}
public class MyProcess : IDisposable
{
PROCESS_INFORMATION pi = new PROCESS_INFORMATION();
// Look at https://msdn.microsoft.com/en-us/library/windows/desktop/ms684863(v=vs.85).aspx for more information about process creation flags
private const int CREATE_SUSPENDED = 0x00000004;
public bool CreateProcess(string filename)
{
STARTUPINFO si = new STARTUPINFO();
// Create a Win32 process started suspended. pi will hold the handles to the process and main thread
return CreateProcess(filename, null, IntPtr.Zero, IntPtr.Zero, false, CREATE_SUSPENDED, IntPtr.Zero, null, ref si, out pi);
}
public void Start()
{
// Start the process which is currently suspended by resuming the main thread
ResumeThread(pi.hThread);
}
public void Dispose()
{
// Handles to the thread and process must be released when done so no memory leaks occur
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static private extern bool CreateProcess(string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes, IntPtr lpThreadAttributes,
bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment,
string lpCurrentDirectory, ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);
[DllImport("kernel32.dll", SetLastError = true)]
static private extern uint ResumeThread(IntPtr hThread);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool CloseHandle(IntPtr hObject);
}
plz幫助並解釋更多thx – Mahdi
並非所有的東西都可以直接用.net框架完成。幸運的是,.net可以讓您鎖定低級別的原生Win32 API函數。因此,您可以在本機C Win32程序中執行任何操作,理論上可以在您的C#代碼中執行。 – IProgrammer
對不起。我誤按了帖子。在這種情況下,我們使用CreateProcess創建一個進程,但給它一個標誌來告訴Windows以暫停模式創建進程。然後,您可以調用ResumeThread來解凍流程的主線程並讓其啓動 – IProgrammer
- 1. ProcessBulder加載進程但未啓動它
- 2. boost :: thread:如何啓動所有線程,但一次只能運行n個?
- 3. 應用程序加載但不運行
- 4. QProcess可以啓動程序但不能啓動python(命令行)
- 5. 如何在應用程序運行時啓動進程?
- 6. 只有在mysql運行之後,才能在windows啓動時運行java程序?
- 7. AssertionError:只能啓動當前進程創建的進程對象
- 8. Apache啓動失敗,php_imagick擴展,但加載後運行php_info
- 9. xmonad在登錄時自動啓動應用程序,但不能重新加載
- 10. 加載在RAM中的C#類代碼?
- 11. 如果第二次啓動,C#程序只能運行一次+ BringToFront
- 12. 檢查進程是否正在運行,如果不啓動它
- 13. 如何用stdin重定向啓動進程,但不啓用stdout?
- 14. 如何進行一次只能運行一次的活動?
- 15. 如何從休息服務啓動批次。 afterJob運行但EnableBatchProcessing只能在部署後運行
- 16. 加載QPixmap不能在Vista上運行
- 17. Apache無法啓動 - 說httpd模塊已加載,但未運行
- 18. 如何在後臺運行進程? c#
- 19. Tomcat在Eclipse中:它在啓動過程中運行但超時
- 20. 重新啓動進程,如果它在Bash中運行
- 21. 加載DLL進入運行過程
- 22. 如何確保只有單個進程在線程中運行
- 23. .NET Excel加載項將加載,但不會啓動,但只在某些電腦上
- 24. 不能運行在Amazon Linux實例後臺進程,但運行在MAC OS X
- 25. 加載進程以運行webrequest
- 26. 不能夠與更多的RAM啓動Java程序
- 27. 可以在前臺運行`mongod`,但不能作爲守護進程運行
- 28. Docker - 進程不在啓動時啓動
- 29. 谷歌地圖只能通過wifi加載,但不加載3G
- 30. 如果進程沒有運行,啓動Windows命令行
如果你有機會到你想運行的可執行文件的源代碼,你可以修改它來拜訪一個互斥或另一個同步機制執行其邏輯之前。然後,您可以在正確的時間從您的呼叫應用程序發出Mutex信號。 –