Winform 进程、线程、treeview

时间:2022-06-10 11:56:08

进程:
一个程序就是一个进程,但是也有一个程序需要多个进程来支持的情况

进程要使用的类是:Process
它在命名空间:System.Diagnostics;

静态方法Start();
也可以实例化对象,来调用Start()普通方法,但调用普通方法之前需要给
StartInfo属性设置一个对象,来通知它要打开的是哪个进程

 private void button1_Click(object sender, EventArgs e)
{
Process p = new Process();
ProcessStartInfo psi = new ProcessStartInfo(textBox1.Text.Trim()); p.StartInfo = psi; p.Start(); } private void button2_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "应用程序|*.exe"; DialogResult dr = openFileDialog1.ShowDialog(); if (dr == DialogResult.OK)
{
textBox1.Text = openFileDialog1.FileName;
} }

线程:
一个进程就像是一个公司,默认只有一个老板
老板叫做 主线程
主线程一旦让它去执行一段需要时间的代码,那么就会出现
程序假死,失去控制

原因是:线程同一时间只能做一件事,而主线程是操作窗体控制的,一旦被占用,那么窗体就没人控制了。

解决办法:
招一个员工帮老板干这个活

开启线程:
线程基本使用:
//创建一个线程对象,告诉它要执行的是哪一个方法
Thread th = new Thread(aaa);

//准备好了,开始执行,不是立即开始,CPU决定什么时候开始
th.Start();
---------------------------------------------------------------
需要跨线程访问对象:
线程需要执行一个方法,而这个方法允许有一个参数,并且这个参数必须是object类型的

程序默认不允许跨线程访问对象,因为监控开着
需要关闭监控
在构造函数的构造方法下面写上:
Control.CheckForIllegalCrossThreadCalls = false;
---------------------------------------------------------------
程序退出立刻关闭所有线程:
线程默认开启为前台线程,程序必须将所有的前台线程执行完毕后才会真正退出程序

解决办法:将线程变为后台线程
//设置后台线程
th.IsBackground = true;
----------------------------------------------------------------

 public partial class Form2 : Form
{
public Form2()
{
InitializeComponent(); Control.CheckForIllegalCrossThreadCalls = false; } private void button1_Click(object sender, EventArgs e)
{
//创建一个线程对象,告诉它要执行的是哪一个方法
Thread th = new Thread(aaa); //设置后台线程
th.IsBackground = true; //准备好了,开始执行,不是立即开始,CPU决定什么时候开始
th.Start(sender);
(sender as Button).Enabled = false; } private static void aaa(object bbb)
{
for (int i = ; i < ; i++)
{
Console.WriteLine(i);
Thread.Sleep();
}
(bbb as Button).Enabled = true; }

TreeView 控件

TreeView 控件显示Node 对象的分层列表,每个Node 对象均由一个标签和一个可选的位图组成。

利用treeview 显示省区县(递归)

FORM代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
List<China> AllList = new List<China>(); public Form1()
{
InitializeComponent(); AllList = new ChinaData().Select(); TreeNode tn1 = new TreeNode("中国");
tn1.Tag = ""; NodesBind(tn1); treeView1.Nodes.Add(tn1); } public void NodesBind(TreeNode tn)
{
//lambda 表达式
List<China> clist = AllList.Where(r => r.ParentAreaCode == tn.Tag.ToString()).ToList(); foreach (China c in clist)
{
TreeNode tnn = new TreeNode(c.AreaName);
tnn.Tag = c.AreaCode; NodesBind(tnn); tn.Nodes.Add(tnn);
}
} }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace WindowsFormsApplication2
{
public class China
{
public string AreaCode { get; set; }
public string AreaName { get; set; }
public string ParentAreaCode { get; set; } }
}
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text; namespace WindowsFormsApplication2
{
public class ChinaData
{
SqlConnection conn = null;
SqlCommand cmd = null; public ChinaData()
{
conn = new SqlConnection("server=.;database=mydb;user=sa;pwd=123");
cmd = conn.CreateCommand();
} public List<China> Select(string pcode)
{
List<China> clist = new List<China>(); cmd.CommandText = "select *from ChinaStates where ParentAreaCode = '" + pcode + "'"; conn.Open();
SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows)
{
while (dr.Read())
{
China c = new China();
c.AreaCode = dr[].ToString();
c.AreaName = dr[].ToString();
c.ParentAreaCode = dr[].ToString(); clist.Add(c);
}
} conn.Close();
return clist;
} public List<China> Select()
{
List<China> clist = new List<China>(); cmd.CommandText = "select *from ChinaStates"; conn.Open();
SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows)
{
while (dr.Read())
{
China c = new China();
c.AreaCode = dr[].ToString();
c.AreaName = dr[].ToString();
c.ParentAreaCode = dr[].ToString(); clist.Add(c);
}
} conn.Close();
return clist;
} }
}