一個學習進程與線程的例子

學校畢業了,在外面找了一份技術支持的工做。是的,這份工做 很閒。忙的 就是就是那個點,其餘的時間 都是很是的閒的。閒着無聊 ,我想學習    c#。就是感受學的好玩!c#

第一天 我想說說個人學習ide

這是一個關於學習進程與線程的例子  灰常簡單吧  就是咱們常常 進程管理器  好了 廢話不說了 開始講述個人學習 這裏例子的理解吧? let's  go!學習

首先 是那個 引用空間 一個是 using system.diagnostic  這個單詞不認識 我百度了一下 說是叫診斷的意思線程

和  using system.collection.objectmodel   這個我也不知道 什麼用法 xml

而後咱們來定義一個結構,一個集合類,和該集合類的兩個屬性進程

public struct Fargsget

{string

public string pid{get;set;}it

public string proname{get;set;}io

}

public class allprocess:obserablecollection<Fargs>{}

allprocess ss=new allprocess();

allprocess ss1=new allprocess();

好了 ,準備開始了,下面是前臺的代碼 注意的就是那個字段的綁定 其它的 我感受還行 

<Window x:Class="Wpf8.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBlock Height="18" HorizontalAlignment="Left" Margin="12,12,0,0" Name="textBlock1"  VerticalAlignment="Top" Width="160" Text="當前全部進程信息"/>
        <ListView Name="listView1" Margin="12,36,12,0" Height="118" VerticalAlignment="Top" SelectionMode="Single">
            <ListView.View>
                <GridView AllowsColumnReorder="true" >
                    <GridViewColumn  Header="進程ID" Width="100" 
DisplayMemberBinding="{Binding Path=pid}" />
                    <GridViewColumn  Header="進程名稱" Width="100" 
DisplayMemberBinding="{Binding Path=processname}" />
                </GridView>
            </ListView.View>
        </ListView>
        <TextBlock HorizontalAlignment="Left" Margin="15,160,0,158" Name="textBlock2" Width="184" Text="所選進程中的全部線程"/>
        <ListView Height="112" Margin="15,0,12,40" Name="listView2" VerticalAlignment="Bottom" >
            <ListView.View >
                <GridView AllowsColumnReorder="true">
                    <GridViewColumn  Header="線程ID" Width="100" 
DisplayMemberBinding="{Binding Path=pid}" />
                    <GridViewColumn  Header="線程開始時間" Width="200" 
DisplayMemberBinding="{Binding Path=processname}" />
                </GridView>
            </ListView.View>
        </ListView>
        <Button Height="22" HorizontalAlignment="Left" Margin="15,0,0,12" Name="button1" VerticalAlignment="Bottom" Width="123" Click="button1_Click">列出當前進程
        </Button>
        <Button Height="24" Margin="144,0,0,10" Name="button2" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="119" Click="button2_Click">結束所選進程</Button>
        <Button Height="24" HorizontalAlignment="Right" Margin="0,0,12,10" Name="button3" VerticalAlignment="Bottom" Width="121" Click="button3_Click">列出所選進程中線程
        </Button>

    </Grid>
</Window>

好了 往事具有 只欠東風  下面就來實現 下面的功能 

1、實現獲取本計算機的全部運行的進程 注意是進程  並顯示 進程的ID 和 進程的名字

 Process [] myProcess=Process.GetProcesses(".");
          ss.Clear();
          Fargs fr = new Fargs();
            foreach(Process ar in myProcess)
            {
                fr.pid = ar.Id.ToString();
                fr.processname = ar.ProcessName;
                ss.Add(fr);
            }

            listView1.ItemsSource = ss;

2、如何將所選擇的的進程 進行關閉

 Fargs fr = new Fargs();
            if(listView1.SelectedItem!=null)
            {
                fr = (Fargs)listView1.SelectedItem;
                int i = Int32.Parse(fr.pid);
                try
                {
                    Process a = Process.GetProcessById(i);
                    a.Kill();

                    listView1.Items.Remove(listView1.SelectedItem);
                }
                catch
                { 

                }
            }

3、經過所選擇的的進程  將該進程的全部線程顯示出來

Fargs fr = new Fargs();
            if (listView1.SelectedItem != null)
            {
                fr = (Fargs)listView1.SelectedItem;
                int i = Int32.Parse(fr.pid);
                try
                {
                    Process a = Process.GetProcessById(i);
                    ProcessThreadCollection mythread = a.Threads;
                    ss1.Clear();
                    Fargs fg = new Fargs();
                    foreach (ProcessThread ar in mythread)
                    {
                        fg.pid = ar.Id.ToString();
                        fg.processname = ar.StartTime.ToString();
                        ss1.Add(fg);
                    }

                    listView2.ItemsSource = ss1;


                }
                catch
                {

                }

好了 我要的功能 基本上都實現了 其它的功能 能夠加以研究 !

相關文章
相關標籤/搜索