2009-07-12 67 views
2

有任何命令行或.NET方法在後臺運行一個進程,隱藏它試圖打開的任何窗口?有什麼辦法可以在後臺運行進程?

已經嘗試過:

var process = new Process() 
{ 
     StartInfo = new ProcessStartInfo() 
     { 
      CreateNoWindow = true, 
      WindowStyle = ProcessWindowStyle.Hidden, 
      FileName = TheRealExecutableFileNameHere 
     } 
} 
process.Start(); 

沒有成功,到目前爲止

+0

可能會幫助告訴我們您嘗試運行的過程。 – Noldorin 2009-07-12 01:19:11

+0

什麼,什麼都不開始或窗口顯示?如果沒有開始,那是因爲你忘了process.Start()... – 2009-07-12 01:20:52

+0

是的,你有一個process.Start()調用是否屬於這個?我沒有看到你分配進程和命令行參數的地方。 – jjxtra 2009-07-12 01:23:16

回答

7

查看Matlab Engine

CodeProject上甚至有一個有趣的article,如果這種方法適合您的需求。

1

這取決於你是否要啓動的應用程序最小化,但允許在需要它來與用戶進行交互,或者如果你想無論發生什麼情況,都禁止與用戶進行所有訪問。

如果是後者,您可以在當前用戶的不同桌面環境下運行該進程。

使用不同的桌面環境,例如,通過登錄對話框和Vista UAC--在一個桌面環境中發生的任何事情都與其他環境無關。

雖然可能是您的問題的大錘方法。

0

我假設你想要一個在用戶運行時不可見的進程。

您可以嘗試以下操作以查看這是否是您想要的。

  1. 創建一個簡單的控制檯應用程序,保持運行(並啓動它來測試)。
  2. 右鍵單擊項目 - >屬性 - >應用程序選項卡 - >輸出類型 - >將其從「控制檯應用程序」更改爲Windows應用程序。
  3. 再次啓動相同的應用程序(以查看這是否是您想要的)。
  4. 通過Windows任務管理器關閉應用:)

看來這個過程中會出現在任務管理器,然而,是不是在任務欄是可見的。 Alt + Tab無法啓動這個過程。

我只是希望你沒有做任何惡意的應用程序,雖然:)

14

我回顧我的代碼,它看起來幾乎相同,你的:

ProcessStartInfo psi = new ProcessStartInfo(fileName, arguments) 
{ 
    CreateNoWindow = true, 
    WindowStyle = ProcessWindowStyle.Hidden, 
    UseShellExecute = false, 
    RedirectStandardOutput = true            
}; 

Process process = Process.Start(psi); 

唯一的顯着性差異(比其他格式以及我們選擇的PSI構造函數)是我使用UseShellExecute和RedirectStandardOutput來根據需要讀取運行過程的結果。

我發現上面的代碼一直在XP和Vista上運行一個隱藏進程。然而,我也發現,你可能會遇到同樣的情況,一個隱藏的進程可能會啓動另一個默認不隱藏的進程。換句話說,如果開始隱藏進程A和進程A,進而啓動進程B,則無法控制進程B的顯示方式。可能會顯示無法控制的Windows。

我希望這會有所幫助。祝你好運。

3

沒有 我不知道純粹的.Net方式來實現這一點。

然後我想到內核Job Objects,但在UI restrictions中沒有找到類似的選項。

所以,下(但未經驗證)想法是創建比create a windows hook掛起的進程,然後,這將監測CallWndProc並濾除WM_SHOW消息。 (然後,當然,恢復這個過程,在一個單獨的線程中等待,直到它終止,刪除鉤子)

2

如果你還沒有,你可能想在.Net框架中嘗試BackgroundWorker Class。它用於在單獨的線程上執行長時間運行的進程,以防止它們阻止UI。看一看。

1

我注意到,如果CreateNoWindow = FALSE確實當文件名指向一個Windows可執行文件,如果你有機會到winform應用程序的源代碼,絕對沒有,那麼你或許可以提供一個命令

static void Main(string[] args) 
    { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     Form1 form1 = new Form1(); 

     form1.Load += new EventHandler((s,o) => 
      { 
       //check if the form should be shown based on command line arg 
       if (args.Contains("dontShowWindow")) 
       { 
        //hide it 
        form1.ShowInTaskbar = false; 
        form1.Visible = form1.ShowInTaskbar = false; 
       } 
      } 
     ); 
     Application.Run(form1); 
    } 

在你調用代碼,您現在可以指定「dontShowWindow」作爲一個過程參數:

控制形式的默認知名度,做這樣的事情在winform應用程序的啓動代碼行參數
ProcessStartInfo info = new ProcessStartInfo 
     { 
      CreateNoWindow = false, 
      WindowStyle = ProcessWindowStyle.Hidden, 
      UseShellExecute = false, 
      FileName = @"C:\temp\testWinForm.exe", 
      Arguments = "dontShowWindow" 
     }; 
     Process.Start(info); 

希望這有助於

1

一個非常簡單的方式實現這一目標是創建一個服務帳戶,並通過Windows任務計劃程序服務帳戶的用戶的上下文中運行可執行文件。用C#很容易

http://www.codeproject.com/KB/cs/tsnewlib.aspx

你可以在域或本地計算機編程方式創建服務帳戶:

您可以使用此CodeProject上設置計劃任務。

http://www.codeproject.com/KB/system/OSUserMangement.aspx http://support.microsoft.com/kb/306273

運行過程中的其他用戶,除非該用戶登錄不交互出現的背景下計劃任務;因此使用服務帳戶。

2

我意識到這個問題已經得到解答,但是您可以強制隱藏一個窗口,對FindWindow和ShowWindow進行非託管調用。

[DllImport("user32.dll")] 
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 

[DllImport("user32.dll")] 
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); 

psi = new ProcessStartInfo(); // etc.. 
some_process = Process.Start(psi); 
System.Threading.Thread.Sleep(50); // need give the window a chance to be created 
IntPtr hWnd = FindWindow(null, "name of the window"); 
if (hWnd != IntPtr.Zero) ShowWindow(hWnd, 0); // 0 = SW_HIDE 

相反kludgy。

0

您可以製作Windows服務。任何服務試圖打開的窗口只會在會話0中運行,這意味着沒有用戶可以看到它。儘管如此,所有的窗戶都可以,所以也許這不是你要找的。

1
public Form1() 
    { 
     InitializeComponent(); 
     Form1 fm = new Form1(); 
     fm.ShowInTaskbar = false; 
     fm.Visible = fm.ShowInTaskbar = false; 
    } 

很好用!

0

下面的代碼爲我工作: 早些時候進程(記事本)用於刷新桌面屏幕上要打印的記事本,但它使用戶分心,所以下面的代碼隱藏在後臺的記事本程序。

   System.Windows.Forms.PrintDialog printDialog = new PrintDialog(); 
       System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(Application.StartupPath + "/PrintingDocketFile/PrintCustomerOrder.txt"); 
       psi.Verb = "PRINT"; 

       //Process.Start(psi); 
       //psi.CreateNoWindow = true; 
       psi.UseShellExecute = false; 
       //--------------------------------------------------- 

       psi.CreateNoWindow = false; 
       psi.UseShellExecute = true; 
       psi.FileName = Application.StartupPath + "/PrintingDocketFile/PrintCustomerOrder.txt"; 
       psi.WindowStyle = ProcessWindowStyle.Hidden; 
       psi.Arguments = @"%windir%\system32\notepad.exe"; 
       //Process.Start(psi); 
       //------------------------------------------------------ 

       /////////////////////---------------------------------- 

       printProcess.StartInfo = psi; 
       /////psi.CreateNoWindow = true; 

       //psi.FileName = "Application.StartupPath" + "/PrintingDocketFile/PrintCustomerOrder.txt"; 
       //p.StartInfo.FileName = "Notepad.EXE"; 
       //p.StartInfo.Arguments = "/i /q \"" + installationPackages[i] + "\""; 

       printProcess.Start(); 

       /////////////////////////------------------------------- 
       while (!printProcess.HasExited) ; 


       if (!printProcess.HasExited) 
       { 
        printProcess.Close(); 
       } 
相關問題