日期:2014-05-16 浏览次数:20629 次
笔者以SQLServer为例,写了个分析数据库的工具,下边的是主要代码:
Form1.cs
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;
using System.Collections;
using System.Threading;
using System.Data.SqlClient;
namespace Database_tracking
{
public partial class Form1 : Form
{
private int ThreadState = 1;//Thread运行状态,0为运行,1停止
private string configPath = Application.StartupPath + "\\config.ini";
/****操作INI文件开始***********/
[System.Runtime.InteropServices.DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
// 声明INI文件的读操作函数 GetPrivateProfileString()
[System.Runtime.InteropServices.DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath);
public string ReadValue(string section, string key)
{
// 每次从ini中读取多少字节
System.Text.StringBuilder temp = new System.Text.StringBuilder(255);
// section=配置节,key=键名,temp=上面,path=路径
GetPrivateProfileString(section, key, "", temp, 255, configPath);
return temp.ToString();
}
public void Writue(string section, string key, string value)
{
// section=配置节,key=键名,value=键值,path=路径
WritePrivateProfileString(section, key, value, configPath);
}
/*****操作INI文件结束**********/
public Form1()
{
InitializeComponent();
this.FormClosing += new FormClosingEventHandler(Form1_Closing);
}
private void Form1_Closing(object sender, FormClosingEventArgs e)
{
if (this.ThreadState != 1)
{
MessageBox.Show("Please stop all the thread first.");
e.Cancel = true;
}
else
{
for (int i = 0; i < mThread.Length; i++)
{
if (mThread[i] != null)
{
if (mThread[i].IsAlive==true)
{
mThread[i].Abort();
}
}
}
this.Dispose();
}
}
//private ArrayList mThread=new ArrayList();
private Thread[] mThread;
private void button2_Click(object sender, EventArgs e)
{
//开始执行
if (button2.Text == "Start")
{
SetText("Starting...");
ThreadState = 0;
listView1.Items.Clear();
cDataIndex = 0;
lCount = 0;
//启动线程
SetObjectStatus(false, button1);
SetObjectStatus(false, button3);
if (mDataBase.Length <= int.Parse(MyModel.ThreadCount))
{
MyModel.ThreadCount = mDataBase.Length.ToString();
}
ChangeValue("Stop");
for (int i = 0; i < int.Parse(MyModel.ThreadCount); i++)
{
mThread[i] = new Thread(ThreadWork);
mThread[i].IsBackground = true;
mThread[i].Name = i.ToString();
mThread[i].Start();
}
}
else
{
ThreadState = 1;
ChangeValue("Start");
}
}
private void button1_Click(object sender, EventArgs e)
{
Config s = new Config();
s.ShowDialog();
}
pri