一个学习进程与线程的例子

学校毕业了,在外面找了一份技术支持的工做。是的,这份工做 很闲。忙的 就是就是那个点,其余的时间 都是很是的闲的。闲着无聊 ,我想学习    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
                {

                }

好了 我要的功能 基本上都实现了 其它的功能 能够加以研究 !

相关文章
相关标签/搜索