C# 掛起恢複運行線程代碼

來源:互聯網
上載者:User
//添加引用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(), "異常");                    }                }            }        }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.