---
title: "windows mobile 5.0 进程管理、窗体管理、重启和关闭操作系统"
author: "cjc"
date: "2008-02-08T09:52:00Z"
tags: []
slug: "windows-mobile-5-0"
draft: false
lastmod: "2008-02-08T09:52:00Z"
id: 402720
url: "https://cjc.iteye.com/blog/402720"
---

&lt;script&gt;function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}&lt;/script&gt;

1、[进程管理](http://www.watch-life.net/windows-mobile/process-window-form-system-manager.html)：在NET Compact Framework里进程管理的函数相对要比net Framework里要简化，不过仍然可以比较好的控制程序进程。

A.启动进程:在启动进程后返回进程的id

```


<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->/// <summary></summary>
        /// 启动进程
        /// 
        /// <param name="fileName">启动文件的目录路径        
        /// <returns></returns>返回启动进程的进程ID
        public static int StartProcess(string fileName)
        {
            int progressID = 0;

            try
            {
                //这个目录是动态的
                progressID = System.Diagnostics.Process.Start(fileName).Id;
                return progressID;
            }
            catch //(Exception ex)
            {
                //throw ex;
                return 0;
            }
        }
```

B.获取当前进程的ID

```


<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->/// <summary></summary>
        /// 获取当前进程ID
        /// 
        public static int GetCurrentProcessID()
        {
             Process currentProcess = Process.GetCurrentProcess();
             int CurrentProcessID = currentProcess.Id;
             return CurrentProcessID;
        }
```

C.终止当前进程

```


<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->/// <summary></summary>
        /// 终止当前进程
        /// 
        public static void KillCurrentProcess()
        {
            System.Diagnostics.Process.GetCurrentProcess().Kill();
        }
```

D.通过进程ID来终止进程

```


<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->/// <summary></summary>
        /// 终止进程
        /// 
        /// <param name="progressID">进程ID
        /// <returns></returns>true 表示终止成功，反之表示失败
        public static bool KillProcess(int progressID)
        {
            try
            {
                System.Diagnostics.Process.GetProcessById(progressID).Kill();
                return true;
            }
            catch //(Exception ex)
            {
                //throw ex;
                return false;
            }
        }
```

2、窗体管理。在NET Compact Framework（2.0）的窗体类（System.Windows.Forms.Form)）虽然提供WindowsState属性，这个属性是 FormWindowState 枚举类型，在枚举中只有Normal 和 Maximized，不过不能通过Maximized来控制窗体最小，同时也无法控制窗体的关闭。要是实现关闭和最小化只能通过调用api来实现。下面是有关代码：

```


<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->[DllImport("coredll.dll")]
        private extern static bool ShowWindow(IntPtr hWnd, int nCmdShow);
        [DllImport("coredll.dll")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        ///最小化窗体
public static void WindowsMin(string frmTextName)
        {

            IntPtr hwnd = FindWindow(null, frmTextName);
            ShowWindow(hwnd, 6);
        }
        ///隐藏窗体
        public static void WindowHide(string frmTextName)
        {
            IntPtr hwnd = FindWindow(null, frmTextName);
            ShowWindow(hwnd, 0);
        }
```

3、重启和关闭操作系统。同样是调用api来完成。

```


<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
        [DllImport("Aygshell.dll")]
        static extern bool ExitWindowsEx(uint uFlags, int dwReserved);
        private const uint EWX_REBOOT = 2;
        private const uint EWX_POWEROFF = 8;


        ///重启操作系统
        public static void RootWindows()
        {
            ExitWindowsEx(EWX_REBOOT, 0);
        }

        ///关闭操作系统
        public static void ShutDownWindows()
        {
            ExitWindowsEx(EWX_POWEROFF, 0);
        }
```

以上代码的运行环境和开发环境：windows mobile5.0 +ppc sdk +vs2005

本文首发地址：[http://www.watch-life.net/windows-mobile/process-window-form-system-manager.html](http://www.watch-life.net/windows-mobile/process-window-form-system-manager.html)

\-------------------------------------------

更多文章见：[守望轩](http://www.watch-life.net)\[[http://www.watch-life.net](http://www.watch-life.net)]