• <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 夢在天涯 閱讀(2267) 評論(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

            搜索

            •  

            積分與排名

            • 積分 - 1804430
            • 排名 - 5

            最新評論

            閱讀排行榜

            久久午夜羞羞影院免费观看| 99精品国产99久久久久久97| 嫩草影院久久99| 久久99热这里只有精品国产| 久久精品亚洲精品国产欧美| 精品久久久久久久久久中文字幕 | 久久久久久国产a免费观看不卡 | 99久久精品费精品国产| 久久99精品久久久久久野外| 久久国产免费直播| 久久精品国产精品亚洲艾草网美妙| 思思久久好好热精品国产| 久久99国产综合精品免费| 久久久久久极精品久久久| 国内精品久久人妻互换| 精品久久久久中文字幕一区| 久久人人爽人人爽人人片AV不 | 亚洲国产二区三区久久| 久久久亚洲裙底偷窥综合| 国产成人综合久久精品尤物| 天天躁日日躁狠狠久久| 久久精品免费全国观看国产| 精品久久久久久国产三级| 久久久精品免费国产四虎| 日韩AV无码久久一区二区 | 天天综合久久久网| 久久99精品久久久久婷婷| 精品国产日韩久久亚洲| 综合久久精品色| 色99久久久久高潮综合影院| 99久久精品免费| 国产高潮国产高潮久久久91| 国产午夜久久影院| 91亚洲国产成人久久精品网址| 久久国产精品99国产精| 久久男人Av资源网站无码软件| 亚洲人成网亚洲欧洲无码久久| 久久乐国产综合亚洲精品| 亚洲国产精品嫩草影院久久| 亚洲国产综合久久天堂 | 久久天天躁狠狠躁夜夜2020一 |