110,533
社区成员
发帖
与我相关
我的任务
分享
while( blcontinue )
{
//do some thing
}
//结束时置blcontinue=false就可。
try
{
//方法体
}
catch (ThreadAbortException e)
{
//处理异常
}
namespace SynUsers
{
public partial class Form1 : Form
{
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder val, int size, string filepath);
static int count = 0;
bool minimized = false;
static bool running = false;
SqlConnection msConn;
OdbcConnection myConn;
int reconnectTime, reconnectNum;
FormWindowState currentState = FormWindowState.Normal;
Thread t = null;
private void Form1_Load(object sender, EventArgs e)
{
if (ReadIniFile())
{
this.WindowState = FormWindowState.Minimized;
t = new Thread(Form1.synTwoDatabases);
running = true;
t.Start(this);
}
}
public bool writeToLog(string MSG, bool isWarning)
{
if (count >= 2 && isWarning)
{
StopSys(false);
count = 0;
return false;
}
try
{
ListViewItem listItem = listView1.Items.Add(DateTime.Now.ToString());
listItem.SubItems.Add(MSG);
if ((int)((listView1.Items.Count - 1) / 5) % 2 == 1)
{
listItem.BackColor = Color.Lavender;
}
else
listItem.BackColor = Color.LemonChiffon;
if (isWarning)
{
listItem.ForeColor = Color.Red;
count++;
/*this.Visible = true;
this.WindowState = currentState;
this.Activate();
this.BringToFront();
this.ShowInTaskbar = true;*/
}
else
{
listItem.ForeColor = Color.Black;
}
if (listView1.Items.Count > 100)
{
listView1.Items.RemoveAt(0);
}
listItem.EnsureVisible();
}
catch
{ }
return true;
}
public DataTable doMSQuery(string sql)
{
DataSet ds = new DataSet();
try
{
SqlDataAdapter adp = new SqlDataAdapter(sql, msConn);
adp.Fill(ds);
if (ds.Tables.Count > 0)
return ds.Tables[0];
else
return null;
}
catch
{
writeToLog("无法连接SQL SERVER", true);
return null;
}
}
public DataTable doMYQuery(string sql)
{
DataSet ds = new DataSet();
try
{
OdbcDataAdapter adp = new OdbcDataAdapter(sql, myConn);
adp.Fill(ds);
if (ds.Tables.Count > 0)
return ds.Tables[0];
else
return null;
}
catch
{
writeToLog("无法连接MYSQL", true);
return null;
}
}
public void StopSys(bool isQuit)
{
try
{
running = false;
if (t != null && t.ThreadState != ThreadState.Aborted && t.IsAlive)
{
try
{
t.Abort();
}
catch (Exception e)
{
writeToLog(e.ToString(),false);
}
t.Join();
t = null;
}
if (msConn.State != ConnectionState.Closed)
msConn.Close();
if (myConn.State != ConnectionState.Closed)
myConn.Close();
}
catch
{
writeToLog("系统停止失败,请重启该软件,并确定数据库连接正确!", false);
}
}
public static void synTwoDatabases(object theMe)
{
Form1 me = (Form1)theMe;
while (Form1.running)
{
string sql = "select * from tri_temp order by id";
DataTable msTab = me.doMSQuery(sql);
DataTable myTab = me.doMYQuery(sql);
if (msTab != null && msTab.Rows.Count > 0)
{
try
{
if (me.myConn.State != ConnectionState.Open)
me.myConn.Open();
foreach (DataRow row in msTab.Rows)
{
string sqlPro = "call pro_Ecshop('" + row["actions"].ToString() + "','" + row["username"].ToString() + "','" + row["password"].ToString() + "','" + row["email"].ToString() + "')";
OdbcCommand com = new OdbcCommand(sqlPro, me.myConn);
com.ExecuteNonQuery();
me.doMSQuery("delete from tri_temp where id=" + row[0].ToString());
}
if (me.myConn.State != ConnectionState.Closed)
me.myConn.Close();
}
catch
{
me.writeToLog("连接MYSQL失败!!", true);
}
}
if (myTab != null && myTab.Rows.Count > 0)
{
try
{
if (me.msConn.State != ConnectionState.Open)
me.msConn.Open();
foreach (DataRow row in myTab.Rows)
{
SqlCommand comm = new SqlCommand("pro_Citysoo", me.msConn);
comm.CommandType = CommandType.StoredProcedure;
comm.Parameters.AddWithValue("@action", row["actions"].ToString());
comm.Parameters.AddWithValue("@username", row["username"].ToString());
comm.Parameters.AddWithValue("@password", row["password"].ToString());
comm.Parameters.AddWithValue("@email", row["email"].ToString());
comm.ExecuteNonQuery();
me.doMYQuery("delete from tri_temp where id=" + row[0].ToString());
}
if (me.msConn.State != ConnectionState.Closed)
me.msConn.Close();
}
catch
{
me.writeToLog("连接SQL SERVER失败!!", true);
}
}
Thread.Sleep(3000);
}
}
}
}
namespace killself
{
class Program
{
public Thread t = null;
static void Main(string[] args)
{
Program p = new Program();
p.t = new Thread(new ThreadStart(p.Kill));
p.t.Start();
Console.WriteLine("terminated");
Console.Read();
}
void Kill()
{
if (t != null && t.ThreadState != ThreadState.Aborted && t.IsAlive)
{
t.Abort(); // 《---每次当t来调用此函数,运行到这句就会报错
t.Join();
t = null;
}
}
}
}