WinForm LED迴圈顯示資訊,使用定時器Threading.Timer

來源:互聯網
上載者:User

標籤:winform   style   blog   class   code   java   

原文:WinForm LED迴圈顯示資訊,使用定時器Threading.Timer

這裡用一個樣本來示範timer如何使用。
樣本:LED螢幕顯示
描述:這個樣本其實很簡單,LED螢幕上顯示3個資訊:
        1:排隊叫號
        2:催繳費
        3:等待列表。因為LED螢幕大小的關係,列表需要分頁顯示。

        正常情況下,這3個資訊都需要從伺服器上去獲得,這裡的樣本只做簡單的類比,

        介面很簡單,,這裡我就不美化了。        

 

Timer建構函式參數說明: Callback:一個 TimerCallback 委託,表示要執行的方法。 State:一個包含回調方法要使用的資訊的對象,或者為空白引用(Visual Basic 中為 Nothing)。 dueTime:調用 callback 之前延遲的時間量(以毫秒為單位)。指定 Timeout.Infinite 以防止計時器開始計時。指定零 (0) 以立即啟動計時器。 Period:調用 callback 的時間間隔(以毫秒為單位)。指定 Timeout.Infinite 可以禁用定期終止。 

程式碼如下:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsThreading
{
public partial class FormTimer : Form
{

//線程暫停時間 單位:毫秒
static int _timespan = 2000;

//每頁顯示行數 範圍1-5
static int _pagerowcount = 4;

//每行固定顯示5個
static int _pagerownumber = 5;

//總頁數
static int _pagetotal = 0;

//當前頁
static int _pageindex = 1;

static int _rowindex = 0;

//擷取正在處理列表
private static IList<string> _namelist;

//聲明定時器
private static System.Threading.Timer _timer;

//定義委託
private delegate void SetTLPNameListDelegate();

//聲明委託變數
private SetTLPNameListDelegate _settlpnamelist;

public FormTimer()
{
InitializeComponent();
_settlpnamelist = LoadLEDForm;//設定委託變數的方法
BeginTimer();
}

private void BeginTimer()
{
//TimerCallback 委託,指定timer定時器需要執行的方法 ThreadMethod
TimerCallback tmrcallback = new TimerCallback(ThreadMethod);
//樣本化定時器
_timer = new System.Threading.Timer(tmrcallback, null, 0, _timespan);
}

private void ThreadMethod(object state)
{
if (this.InvokeRequired)
this.Invoke(_settlpnamelist);
}

/// <summary>
/// 設定正在處理列表
/// </summary>
private void LoadLEDForm()
{
SetQueueOrder();
SetName();
SetNameList();
}
/// <summary>
/// 設定等待列表
/// </summary>
private void SetNameList()
{
if (_pageindex == 1)
{
GetNameList();
_pagetotal = (int)Math.Ceiling((float)_namelist.Count / (_pagerowcount * _pagerownumber));

if (_pagetotal < 1) { _pagetotal = 1; }
}
if (_namelist != null && _namelist.Count > 0)
{
int column = 0;
tlpaNameList.Controls.Clear();
_rowindex = 0;
int i = 0;
int startIndex = (_pageindex - 1) * (_pagerowcount * _pagerownumber);
int endIndex = _pageindex * (_pagerowcount * _pagerownumber);
for (; i < _namelist.Count; i++)
{
if (i >= startIndex && i < endIndex)
{
if (_rowindex > _pagerowcount - 1)
break;
if (column > _pagerownumber - 1)
{
column = 0;
_rowindex++;
}
Addlabel(_namelist[i], column);
column++;
}
}
lblPageNumber.Text = string.Format("頁碼 {0}/{1}", _pageindex, _pagetotal);
_pageindex++;
if (_pageindex > _pagetotal)
{
_pageindex = 1;
}
}
}

/// <summary>
/// 設定排隊叫號
/// </summary>
private void SetQueueOrder()
{
lblOrder.Text = _pageindex + " 號";
}

/// <summary>
/// 設定催繳費
/// </summary>
private void SetName()
{
lblName.Text = "唐鑫瑞" + _pageindex;
}

private void Addlabel(String text, int columnindex)
{
Label label = new Label();
label.Dock = DockStyle.Top;
label.ForeColor = Color.Yellow;
label.BackColor = Color.Black;
label.Font = new System.Drawing.Font("宋體", 12F);
label.Text = text;
label.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
tlpaNameList.Controls.Add(label, columnindex, _rowindex);
}

/// <summary>
/// 類比一個列表(列表中有30個對象),每個列表顯示完成後,自動新增一個對象,用來類比等待列表的人數在增加
/// </summary>
public void GetNameList()
{
if (_namelist == null || _namelist.Count < 1)
{
_namelist = new List<string>();
for (int i = 1; i < 31; i++)
{
_namelist.Add("唐鑫瑞" + i);
}
}
else
{
if (_pageindex == 1)
_namelist.Add("唐鑫瑞" + (_namelist.Count + 1));
}
}

private void FormTimer_Load(object sender, EventArgs e)
{

}
}
}


顯示結果如:

因為我這裡類比等待列表的人數在增加,所以 在第4次重新整理顯示的時候會增加一人,(初始30人)

這樣一直重新整理顯示下去,人數會越來越多,頁碼就會超過2頁,如

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.