• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C++ Programmer's Cookbook

            {C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

            進程 Process的使用

            1 Process?
            ? 簡單實例:

            // ?Execute?notepad.exe?with?no?command-line?arguments.
            Process.Start( " notepad.exe " );

            // ?Execute?notepad.exe?passing?the?name?of?the?file?to?open?as?a?
            // ?command-line?argument.
            Process.Start( " notepad.exe " ,? " SomeFile.txt " );

            Properties of the ProcessStartInfo Class

            Property

            Description

            Arguments

            The command-line arguments to pass to the new process.

            ErrorDialog

            If Process.Start can't start the specified process, it will throw a System.ComponentModel.Win32Exception. If ErrorDialog is true, Start displays an error dialog to the user before throwing the exception.

            FileName

            The name of the application to start. You can also specify any type of file for which you have configured an application association. For example, you could specify a file with a .doc or .xls extension, which would cause Microsoft Word or Microsoft Excel to run.

            WindowStyle

            A member of the System.Diagnostics.ProcessWindowStyle enumeration, which controls how the window is displayed. Valid values include Hidden, Maximized, Minimized, and Normal.

            WorkingDirectory

            The fully qualified name of the initial directory for the new process.


            進程的啟動:

            The following example uses Process to execute Notepad in a maximized window and open a file named C:\Temp\file.txt. After creation, the example calls the Process.WaitForExit method, which blocks the calling thread until a process terminates or a specified time-out expires.


            using?System;
            using?System.Diagnostics;

            public?class?StartProcessExample?{

            ????
            public?static?void?Main?()?{

            ????????
            //?Create?a?ProcessStartInfo?object?and?configure?it?with?the?
            ????????
            //?information?required?to?run?the?new?process.
            ????????ProcessStartInfo?startInfo?=?new?ProcessStartInfo();

            ????????startInfo.FileName?
            =?"notepad.exe";
            ????????startInfo.Arguments?
            =?"file.txt";
            ????????startInfo.WorkingDirectory?
            =?@"C:\Temp";
            ????????startInfo.WindowStyle?
            =?ProcessWindowStyle.Maximized;
            ????????startInfo.ErrorDialog?
            =?true;

            ????????
            //?Create?a?new?Process?object.
            ????????using?(Process?process?=?new?Process())?{

            ????????????
            //?Assign?the?ProcessStartInfo?to?the?Process.
            ????????????process.StartInfo?=?startInfo;

            ????????????
            try?{

            ????????????????
            //?Start?the?new?process.
            ????????????????process.Start();

            ????????????????
            //?Wait?for?the?new?process?to?terminate?before?exiting.
            ????????????????Console.WriteLine("Waiting?30?seconds?for?process?to"?+
            ????????????????????
            "?finish.");
            ????????????????process.WaitForExit(
            30000);

            ????????????}
            ?catch?(Exception?ex)?{

            ????????????????Console.WriteLine(
            "Could?not?start?process.");
            ????????????????Console.WriteLine(ex);
            ????????????}

            ????????}


            ????????
            //?Wait?to?continue.
            ????????Console.WriteLine("Main?method?complete.?Press?Enter.");
            ????????Console.ReadLine();
            ????}

            }


            進程終止:
            Methods for Obtaining Process References

            Method

            Description

            GetCurrentProcess

            Returns a Process object representing the currently active process.

            GetProcessById

            Returns a Process object representing the process with the specified ID.

            GetProcesses

            Returns an array of Process objects representing all currently active processes.

            GetProcessesByName

            Returns an array of Process objects representing all currently active processes with a specified friendly name. The friendly name is the name of the executable excluding file extension or path; for example, notepad or calc.

            The following example starts a new instance of Notepad, waits five seconds, and then terminates the Notepad process. The example first tries to terminate the process using CloseMainWindow. If CloseMainWindow returns false, or the Notepad process is still running after CloseMainWindow is called, the example calls Kill and forces the Notepad process to terminate; you can force CloseMainWindow to return false by leaving the File Open dialog box open.

            using?System;
            using?System.Threading;
            using?System.Diagnostics;

            public?class?TerminateProcessExample?{

            ????
            public?static?void?Main?()?{

            ????????
            //?Create?a?new?Process?and?run?notepad.exe.
            ????????using?(Process?process?=?Process.Start("notepad.exe"))?{

            ????????????
            //?Wait?for?5?seconds?and?terminate?the?notepad?process.
            ????????????Console.WriteLine("Waiting?5?seconds?before?terminating"?+
            ????????????????
            "?notepad.exe.");
            ????????????Thread.Sleep(
            5000);

            ????????????
            //?Terminate?notepad?process.
            ????????????Console.WriteLine("Terminating?Notepad?with?CloseMainWindow.");

            ????????????
            //?Try?to?send?a?close?message?to?the?main?window.
            ????????????if?(!process.CloseMainWindow())?{

            ????????????????
            //?Close?message?did?not?get?sent?-?Kill?Notepad.
            ????????????????Console.WriteLine("CloseMainWindow?returned?false?-?"?+
            ????????????????????
            "?terminating?Notepad?with?Kill.");
            ????????????????process.Kill();

            ????????????}
            ?else?{

            ????????????????
            //?Close?message?sent?successfully;?wait?for?2?seconds
            ????????????????
            //?for?termination?confirmation?before?resorting?to?Kill.
            ????????????????if?(!process.WaitForExit(2000))?{

            ????????????????????Console.WriteLine(
            "CloseMainWindow?failed?to"?+
            ????????????????????????
            "?terminate?-?terminating?Notepad?with?Kill.");
            ????????????????????process.Kill();
            ????????????????}

            ????????????}

            ????????}


            ????????
            //?Wait?to?continue.
            ????????Console.WriteLine("Main?method?complete.?Press?Enter.");
            ????????Console.ReadLine();
            ????}

            }

            posted on 2006-04-20 19:11 夢在天涯 閱讀(2273) 評論(0)  編輯 收藏 引用 所屬分類: C#/.NET

            公告

            EMail:itech001#126.com

            導航

            統計

            • 隨筆 - 461
            • 文章 - 4
            • 評論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1807503
            • 排名 - 5

            最新評論

            閱讀排行榜

            久久久久国产亚洲AV麻豆| 久久精品国产亚洲沈樵| 久久久噜噜噜久久中文字幕色伊伊| 久久国产午夜精品一区二区三区| 无码乱码观看精品久久| 少妇高潮惨叫久久久久久| 99久久精品国产一区二区蜜芽| 久久免费视频6| 狠狠色丁香久久婷婷综合五月| 国产精品毛片久久久久久久| 青青草国产97免久久费观看| 久久国产高潮流白浆免费观看| 久久综合久久伊人| 亚洲天堂久久精品| 亚洲精品无码久久一线| 合区精品久久久中文字幕一区| 精品久久久久久亚洲| 亚洲国产精品无码久久| 国产巨作麻豆欧美亚洲综合久久 | 亚洲精品乱码久久久久久| 91亚洲国产成人久久精品| 亚洲αv久久久噜噜噜噜噜| 欧美久久久久久精选9999| 久久久精品国产sm调教网站| 国内精品久久国产| 四虎影视久久久免费| 精品国产91久久久久久久a| 久久婷婷国产麻豆91天堂| 7777久久久国产精品消防器材| 久久精品国产国产精品四凭 | 91久久成人免费| 91精品国产9l久久久久| 久久精品无码一区二区无码| 国内精品久久久久影院薰衣草| 欧美久久一区二区三区| 久久综合给合综合久久| 久久强奷乱码老熟女网站| 欧洲性大片xxxxx久久久| 亚洲国产小视频精品久久久三级| 久久综合成人网| 综合久久国产九一剧情麻豆|