//添加引用using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;using System.Threading;using System.Collections; //定義變數private Thread convertThread;private Mutex mutex = new Mutex();Queue myThreadQueue=new Queue();//按鈕事件private void btnConstraintToBig5_Click(object sender, EventArgs e) { this.btnConstraintToBig5.Enabled = false; //測試資料庫連接 this.richTextBox1.Text = ""; CurrentServerName = this.txtServerName.Text.Trim(); CurrentDatabaseName = this.txt_DatabaseName.Text.Trim(); CurrentUserName = this.txt_UserName.Text.Trim(); CurrentUserPassword = this.txt_Password.Text.Trim(); CurrentConnectionString = "Data Source=" + CurrentServerName + ";Initial Catalog=Master;User ID=" + CurrentUserName + ";password=" + CurrentUserPassword; DataTable myDataTable = new DataTable(); try { SqlConnection thisConnection = new SqlConnection(CurrentConnectionString); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = new SqlCommand("select * from sysservers", thisConnection); DataSet ds = new DataSet(); da.Fill(ds, "Temp"); myDataTable = ds.Tables["Temp"]; thisConnection.Close(); if (myDataTable.Rows.Count > 0) { this.richTextBox1.Text += "/r/n建立到資料庫連接成功"; CurrentConnectionString = "Data Source=" + CurrentServerName + ";Initial Catalog=" + CurrentDatabaseName + ";User ID=" + CurrentUserName + ";password=" + CurrentUserPassword; convertThread = new Thread(new ThreadStart(StartConvertConstraintToBig5)); convertThread.IsBackground = true; convertThread.Start(); } else { this.richTextBox1.Text += "/r/n建立到資料庫連接失敗"; } } catch (Exception ex) { this.richTextBox1.Text += "/r/n建立到資料庫連接出錯" + ex.Message; } }//線程調用函數/// <summary> /// 線程調用的監聽轉換資料資料表條件約束到簡體版函數 /// </summary> private void StartConvertConstraintToBig5() { lock (myThreadQueue) { mutex.WaitOne(); myDataBaseConverter.BeginConvertingMessage += new DataBaseEventHandler(OnBeginMessageReceived); myDataBaseConverter.EndConvertingMessage += new DataBaseEventHandler(OnEndMessageReceived); myDataBaseConverter.CurrentConvertingMessage += new DataBaseEventHandler(OnCurrentMessageReceived); myDataBaseConverter.ConvertingErrorMessage += new DataBaseEventHandler(OnErrorMessageReceived); myDataBaseConverter.ConvertTableConstraintToBig5(CurrentConnectionString); mutex.ReleaseMutex(); } } //暫停恢複線程運行private void btnPauseConverting_Click(object sender, EventArgs e) { if (convertThread != null) { Console.WriteLine("當前線程狀態名稱:" + convertThread.ThreadState.ToString()); Console.WriteLine("當前線程狀態編號:" + Convert.ToInt32(convertThread.ThreadState).ToString()); if (this.btnPauseConverting.Text == "繼續轉換" && Convert.ToInt32(convertThread.ThreadState) == 68) { try { this.btnPauseConverting.Text = "暫停轉換"; convertThread.Resume(); } catch (Exception msg) { MessageBox.Show(msg.ToString(), "異常"); } } else if (this.btnPauseConverting.Text == "暫停轉換" && Convert.ToInt32(convertThread.ThreadState) == 36) { try { this.btnPauseConverting.Text = "繼續轉換"; convertThread.Suspend(); } catch (Exception msg) { MessageBox.Show(msg.ToString(), "異常"); } } } }