信赤2015吧 关注:2贴子:772
  • 6回复贴,共1

希望大学之路就此一帆风顺

只看楼主收藏回复

希望大学之路就此一帆风顺


IP属地:上海来自Android客户端1楼2015-09-02 16:08回复
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Threading;
    using System.Threading.Tasks;
    using System.IO;
    using System.Diagnostics;
    using System.Management;
    namespace A._2
    {
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
    int fileIndex = 1;
    string filename = "Notepad";
    List<Data> list=new List<Data>();
    public MainWindow()
    {
    InitializeComponent();
    }
    private void btnStart_Click(object sender, RoutedEventArgs e)
    {
    string arguement = Environment.CurrentDirectory + "myfile" + (fileIndex) + ".txt";
    if (File.Exists(arguement) == false)
    {
    File.CreateText(arguement);
    }
    Process p = new Process();
    p.StartInfo.FileName = filename;
    p.StartInfo.Arguments = arguement;
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
    p.Start();
    p.WaitForInputIdle();
    RefreshProcessInfo();
    }
    private void btnStop_Click(object sender, RoutedEventArgs e)
    {
    this.Cursor=Cursors.Wait;
    Process [] myprocess;
    myprocess =Process.GetProcessesByName(filename);
    foreach( Process p in myprocess){
    using (p){
    p.CloseMainWindow();
    Thread.Sleep(1000);
    p.WaitForExit();
    }
    }
    fileIndex=0;
    RefreshProcessInfo();
    this.Cursor=Cursors.Arrow;
    }
    private void RefreshProcessInfo()
    {
    this.dataGrid1.ItemsSource=null;
    list.Clear();
    Process[] processes = Process.GetProcesses();
    foreach(Process p in processes){
    Data pd = new Data();
    try
    {
    if (p.Id == 0)
    {
    pd.ID = p.Id;
    pd.Name = p.ProcessName;
    pd.StartTime = "";
    pd.Memory = "";
    pd.FilePath = "";
    }
    else
    {
    pd.ID = p.Id;
    pd.Name = p.ProcessName;
    pd.StartTime = p.StartTime.ToString("yyyy-M-d HH:mm:ss");
    pd.Memory = string.Format("{0,10:0} KB", p.WorkingSet64 / 1024d);
    pd.FilePath = p.MainModule.FileName;
    }
    }
    catch
    {
    pd.FilePath = "";
    }
    list.Add(pd);
    }
    this.dataGrid1.ItemsSource=list;
    this.textblock.Text = "当前正在运行的进程数目是:" + list.Count;
    }
    }
    public class Data{
    public int ID {get;set;}
    public string Name { get; set; }
    public string StartTime { get; set; }
    public string Memory { get; set; }
    public string FilePath { get; set; }
    }
    }


    IP属地:上海2楼2017-03-06 11:54
    回复
      <Window x:Class="A._2.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 >
      <Grid.RowDefinitions>
      <RowDefinition Height="85*"></RowDefinition>
      <RowDefinition Height="5*"></RowDefinition>
      <RowDefinition Height="10*"></RowDefinition>
      </Grid.RowDefinitions>
      <DataGrid Name="dataGrid1" Grid.Row="0" AutoGenerateColumns="False">
      <DataGrid.Columns>
      <DataGridTextColumn Header="进程ID" Binding="{Binding ID}"></DataGridTextColumn>
      <DataGridTextColumn Header="进程名称" Binding="{Binding Name}"></DataGridTextColumn>
      <DataGridTextColumn Header="占用内存(MB)" Binding="{Binding Memory}"></DataGridTextColumn>
      <DataGridTextColumn Header="启动时间" Binding="{Binding StartTime}"></DataGridTextColumn>
      <DataGridTextColumn Header="文件路径" Binding="{Binding FilePath}"></DataGridTextColumn>
      </DataGrid.Columns>
      </DataGrid>
      <TextBlock Grid.Row="1" Name="textblock" Foreground="AliceBlue" VerticalAlignment="Center"></TextBlock>
      <StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Center" >
      <Button Name="btnprocess" Width="100" Content="启动进程" Height="40" Click="btnStart_Click" Margin="5" ></Button>
      <Button Name="btnKill" Width="100" Content="停止进程" Height="40" Click="btnStop_Click" Margin="5"></Button>
      </StackPanel>
      </Grid>
      </Window>


      IP属地:上海3楼2017-03-06 11:55
      回复
        using System;
        using System.Collections.Generic;
        using System.Diagnostics;
        using System.Linq;
        using System.Text;
        using System.Threading;
        using System.Threading.Tasks;
        using System.Windows;
        using System.Windows.Controls;
        using System.Windows.Data;
        using System.Windows.Documents;
        using System.Windows.Input;
        using System.Windows.Media;
        using System.Windows.Media.Imaging;
        using System.Windows.Navigation;
        using System.Windows.Shapes;
        namespace WpfApplication1
        {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
        public MainWindow()
        {
        InitializeComponent();
        }
        List<ProcessData> list = new List<ProcessData>();
        private void Button_Click(object sender, RoutedEventArgs e)
        {
        RefreshProcess();
        }
        private void RefreshProcess()
        {
        list.Clear();
        this.datagrid1.ItemsSource = null;
        Process[] myp = Process.GetProcesses();
        foreach (Process p in myp)
        {
        ProcessData pd = new ProcessData();
        try
        {
        pd.ID = p.Id;
        pd.Name = p.ProcessName;
        if (p.Id == 0)//Idle进程
        {
        pd.StartTime = "";
        pd.Memory = "";
        pd.FilePath = "";
        }
        else //非空闲进程
        {
        pd.StartTime = p.StartTime.ToString("yyyy-M-d HH:mm:ss");
        pd.Memory = (p.WorkingSet64 / 1024 / 1024).ToString() + " MB";
        pd.FilePath = p.MainModule.FileName;//maybe exception
        }
        }
        catch
        {
        pd.FilePath = "";
        }
        list.Add(pd);
        }
        this.datagrid1.ItemsSource = list;
        this.textblock.Text = "当前正在运行的进程数目是:" + list.Count;
        }
        private void btnKill_Click(object sender, RoutedEventArgs e)
        {
        ProcessData pd = (ProcessData)datagrid1.SelectedItem;
        if (pd != null)
        {
        Process p=null;
        try
        {
        p = Process.GetProcessById(pd.ID);
        p.Kill();
        Thread.Sleep(1000);
        }
        catch
        {
        MessageBox.Show("进程无法终止!!请确认进程是否在运行或者是否具有终止进程的权限!~");
        }
        finally
        {
        if (p != null)
        {
        if (p.HasExited == true)
        MessageBox.Show("进程已经正常终止!");
        p.Close();
        }
        }
        }
        RefreshProcess();
        }
        }
        class ProcessData
        {
        public int ID {get;set;}
        public string Name { get; set; }
        public string StartTime { get; set; }
        public string Memory { get; set; }
        public string FilePath { get; set; }
        }
        }


        IP属地:上海4楼2017-03-06 11:55
        回复
          参考Window系统自带的任务管理器,实现一个简易的WPF版本的任务管理器。要求实现下列功能:
          u显示本机所有当前正在运行的进程信息;u显示每个正在运行进程的ID、使用内存大小、进程描述等;u能够启动和终止进程;u能够保持进程信息的实时更新;u能够处理程序可能出现的异常信息。
          功能扩展:显示本机运行的服务信息、CPU性能等信息;
          u


          IP属地:上海5楼2017-03-06 11:56
          回复
            package com.example.administrator.a4_1;
            import android.app.Service;
            import android.app.content.Intent;
            import android.app.os.IBinder;
            import android.app.widget.Toast;
            import android.content.Intent;
            import android.os.IBinder;
            import android.view.InflateException;
            import android.widget.Toast;
            /**
            * Created by Administrator on 2017/5/4.
            */
            public abstract class RandomService extends Service {
            private Thread workThread;
            @Override
            public void onStart(Intent intent,int startId){
            super.onStart(intent,startId);
            Toast.makeText(this,"(3)调用()onDestroy",Toast.LENGTH_LONG).show();
            workThread.interrupt();
            }
            @Override
            public void onDestroy(){
            super.onDestroy();
            Toast.makeText(this,"(3)调用()onDestroy",Toast.LENGTH_LONG).show();
            workThread.interrupt();
            }
            @Override
            public void onCreate(){
            super.onCreate();
            Toast.makeText(this,"(1)调用()onCreate",Toast.LENGTH_LONG).show();
            workThread=new Thread(null,backgroundWork,"WorkThread");
            }
            public IBinder OnBind(Intent intent){
            return null;
            }
            private Runnable backgroundWork=new Runnable() {
            @Override
            public void run() {
            try{
            while(!Thread.interrupted()){
            double randomDouble=Math.random();
            MainActivity.UpdateGUI(randomDouble);
            Thread.sleep(1000);
            }
            }catch (InterruptedException e){
            e.printStackTrace();
            }
            }
            };
            }


            IP属地:上海8楼2017-05-04 09:43
            回复
              package com.example.administrator.a4_1;
              import android.content.Intent;
              import android.os.Handler;
              import android.support.v7.app.AppCompatActivity;
              import android.os.Bundle;
              import android.view.View;
              import android.widget.Button;
              import android.widget.TextView;
              public class MainActivity extends AppCompatActivity {
              private static Handler handler=new Handler();
              private static TextView labelView=null;
              private static double randomdouble;
              public static void UpdateGUI(double refreshDouble){
              randomdouble=refreshDouble;
              handler.post(RefreshLable);
              }
              private static Runnable RefreshLable=new Runnable() {
              @Override
              public void run() {
              labelView.setText(String.valueOf(randomdouble));
              }
              };
              @Override
              protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              labelView=(TextView)findViewById(R.id.textView);
              Button startButton=(Button)findViewById(R.id.btn_start);
              Button stopButton=(Button)findViewById(R.id.btn_stop);
              final Intent serviceIntent=new Intent(this,RandomService.class);
              startButton.setOnClickListener(new Button.OnClickListener(){
              @Override
              public void onClick(View v) {
              startService(serviceIntent);
              }
              });
              stopButton.setOnClickListener(new Button.OnClickListener(){
              @Override
              public void onClick(View v) {
              stopService(serviceIntent);
              }
              });
              }
              }


              IP属地:上海9楼2017-05-04 09:43
              回复