• <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精品久久久久久齐齐| 三级片免费观看久久| 久久亚洲精品中文字幕| 国内精品免费久久影院| 国内精品伊人久久久久777| 久久精品草草草| 久久午夜综合久久| 久久天堂AV综合合色蜜桃网| 99久久国产免费福利| 亚洲精品白浆高清久久久久久| 久久亚洲综合色一区二区三区| 欧美精品一区二区久久| 精品久久人妻av中文字幕| 人妻丰满?V无码久久不卡| 精品精品国产自在久久高清| 天堂无码久久综合东京热| 国产精品久久久久久久| 一本色综合网久久| 久久一区二区三区免费| 99久久成人18免费网站| 久久久精品免费国产四虎| 无码人妻久久一区二区三区| 亚洲欧美日韩精品久久亚洲区 | 日日噜噜夜夜狠狠久久丁香五月| 狠狠色丁香婷婷综合久久来来去| 亚洲女久久久噜噜噜熟女| 久久久久国色AV免费看图片| 国产精品久久网| avtt天堂网久久精品| 18岁日韩内射颜射午夜久久成人| 久久一本综合| 亚洲日本va午夜中文字幕久久| 久久综合中文字幕| 大美女久久久久久j久久| 亚洲天堂久久精品| 久久国产福利免费| 久久综合九色综合97_久久久|