<address id="ousso"></address>
<form id="ousso"><track id="ousso"><big id="ousso"></big></track></form>
  1. C語言

    C言語如何利用子線程刷新主線程

    時間:2024-12-26 20:55:38 C語言 我要投稿
    • 相關推薦

    C言語如何利用子線程刷新主線程

      你知道C言語如何利用子線程刷新主線程嗎?使用子線程進行時間操作和加法操作,然后刷新主線程的控件顯示結果。下面是小編為大家帶來的關于C言語如何利用子線程刷新主線程的知識,歡迎閱讀。

      C言語如何利用子線程刷新主線程

      使用線程操作

      1、實時顯示當前時間

      2、輸入加數和被加數,自動出現結果

      分析:兩個問題解決的方式一致,使用子線程進行時間操作和加法操作,然后刷新主線程的控件顯示結果

      using System;

      using System.Threading;

      using System.Windows.Forms;

      namespace WinThread

      {

      public partial class frmMain : Form

      {

      public frmMain()

      {

      InitializeComponent();

      }

      ///

      /// 初始化

      ///

      ///

      ///

      private void frmMain_Load(object sender, EventArgs e)

      {

      // 控件初始化

      this.txtOne.Text = "0";

      this.txtSecond.Text = "0";

      // 顯示時間操作

      Thread showTimeThread = new Thread(new ThreadStart(GetTime));

      showTimeThread.IsBackground = true;

      showTimeThread.Start();

      // 加法操作

      Thread addThread = new Thread(new ThreadStart(Add));

      addThread.IsBackground = true;

      addThread.Start();

      }

      #region 顯示時間操作

      ///

      /// 取得實時時間

      ///

      private void GetTime()

      {

      try

      {

      while (true)

      {

      string currentTime = string.Format("{0}.{1}", DateTime.Now.ToLongTimeString(),

      DateTime.Now.Millisecond);

      ShowTime(currentTime);

      Application.DoEvents();

      }

      }

      catch (Exception ex)

      {

      Console.WriteLine(ex.Message);

      }

      }

      // 定義顯示時間操作委托,用于回調顯示時間方法

      delegate void ShowTimeCallBack(string str);

      ///

      /// 實時顯示時間

      ///

      ///

      private void ShowTime(string str)

      {

      if (this.txtCurrentTime.InvokeRequired)

      {

      ShowTimeCallBack showTimeCallBack = new ShowTimeCallBack(ShowTime);

      this.Invoke(showTimeCallBack, new object[] { str });

      }

      else

      {

      this.txtCurrentTime.Text = str;

      }

      }

      #endregion

      #region 加法操作

      ///

      /// 加法操作

      ///

      private void Add()

      {

      try

      {

      while (true)

      {

      int i = Convert.ToInt32(this.txtOne.Text.Trim());

      int j = Convert.ToInt32(this.txtSecond.Text.Trim());

      ShowResult((i + j).ToString());

      Application.DoEvents();

      }

      }

      catch (Exception ex)

      {

      Console.WriteLine(ex.Message);

      }

      }

      // 定義加法操作委托,用于回調加法操作方法

      delegate void ShowResultCallBack(string result);

      ///

      /// 顯示結果

      ///

      ///

      private void ShowResult(string result)

      {

      if (this.txtResult.InvokeRequired)

      {

      // 寫法1

      //ShowResultCallBack showResultCallBack = new ShowResultCallBack(ShowResult);

      //this.Invoke(showResultCallBack, new object[] { result });

      // 寫法2

      //使用委托來賦值

      this.txtResult.Invoke(

      //委托方法

      new ShowResultCallBack(ShowResult),

      new object[] { result });

      }

      else

      {

      this.txtResult.Text = result;

      }

      }

      #endregion

      }

      }


    【C言語如何利用子線程刷新主線程】相關文章:

    利用Java終止線程的方法04-05

    c語言線程終止練習示例06-03

    java中如何停止線程08-14

    Java多線程的線程守護例子06-08

    如何使用java多線程05-04

    如何創建并運行Java線程01-11

    Java 如何進行線程同步05-19

    java Runnable接口如何創建線程05-18

    淺談如何使用java多線程05-07

    <address id="ousso"></address>
    <form id="ousso"><track id="ousso"><big id="ousso"></big></track></form>
    1. 日日做夜狠狠爱欧美黑人