C#傳遞參數到線程的辦法匯總。本站提示廣大學習愛好者:(C#傳遞參數到線程的辦法匯總)文章只能為提供參考,不一定能成為您想要的結果。以下是C#傳遞參數到線程的辦法匯總正文
本文匯總整頓了傳遞參數到線程的辦法供年夜家參考,異常適用,詳細內容以下:
起首我們要曉得甚麼是線程,甚麼時刻要用到線程,若何去應用線程,若何更好的應用線程來完成任務。
線程是法式可履行片斷的最小單位,是構成運轉時法式的根本單位,一個過程有至多一個線程構成。普通在並行處置期待事宜的時刻要用到線程,如期待收集呼應,期待I/O通信,後台事務處置等情形。應用線程其實很簡略,在.net框架上面你起首要界說一個函數來完成一些任務,然後實例化一個線程對象Thread thrd = new Thread(new ThreadStart(線程函數)); 個中ThreadStart是一個不帶參數的函數拜托。最初應用thrd.Start()便可以啟動線程了。固然這只是一個很簡略的例子,現實中應用線程會有許多的成績要處理,好比傳遞參數到線程中,期待線程前往,若何同步線程停止統一資本拜訪,若何避免逝世鎖和競爭前提,若何有用的應用線程池,若何供給線程效力等成績。本文在這裡將只對傳遞參數到線程停止商量。
在下面舉例中我們看到線程實例化的參數是一個不帶任何參數的函數拜托,那末就證實了我們弗成能經由過程如許一個拜托傳遞參數到線程外部。那末我們該怎樣做呢?
就我今朝所知有三種辦法可以完成:1. 應用線程完成類,將挪用參數界說成屬性的方法來操作線程參數;2. 應用ParameterizedThreadStart拜托來傳遞輸出參數;3. 應用線程池來完成參數傳入。
上面分離加以論述:
1. 創立線程完成類,這類方法有個最年夜的長處就是可以經由過程線程前往多個前往值
假定存在在一個打印功效的線程,經由過程主函數傳入打印的行,列和字符數據,並前往打印的字符總數。
詳細代碼以下:
class ThreadOutput
{
int _rowCount = 0;
int _colCount = 0;
char _char = '*';
int _ret = 0;
/**//// <summary>
/// 輸出參數
/// </summary>
public int RowCount
{
set { _rowCount = value; }
}
public int ColCount
{
set { _colCount = value; }
}
public char Character
{
set { _char = value; }
}
/**//// <summary>
/// 輸入參數
/// </summary>
public int RetVal
{
get { return _ret; }
}
public void Output()
{
for (int row = 0; row < _rowCount; row++)
{
for (int col = 0; col < _colCount; col++)
{
Console.Write("{0} ", _char);
_ret++;
}
Console.Write("\n");
}
}
ThreadOutput to1 = new ThreadOutput();
to1.RowCount = 10;
to1.ColCount = 20;
Thread thrd = new Thread(new ThreadStart(to1.Output));
// 設置為後台線程,重要是為不影響主線程的停止
thrd.IsBackground = true;
thrd.Start();
最初要留意的是因為線程完成類是經由過程屬性來傳遞數值的,那末在屬性拜訪器中要停止線程同步,不然獲得的值能夠不准確。
2. 應用ParameterizedThreadStart拜托來傳遞輸出參數
ParameterizedThreadStart拜托是.Net2.0中才有的。該拜托供給來在啟動線程時傳遞一個object參數到線程中。這類方法應用起來比擬簡略,然則因為須要對object對象停止類型轉換,所以存在類型紛歧致的隱患。
3. 應用線程池來完成參數傳入
甚麼是線程池,線程池是體系供給一個寄存線程的容器,進入線程池後的線程掌握權由體系控制。應用線程池的利益是我們無需為線程存在年夜量余暇時光而去思慮干點其余甚麼,合適於慣例性的事務處置。在.Net中線程池是一個static類,所以我們須要經由過程ThreadPool來挪用相干的函數。個中向線程池中參加線程的函數就是ThreadPool.QueueUserWorkItem(new WaitCallBack(), object obj)。這個函數有個WaitCallBack的拜托,[ComVisibleAttribute(true)]
public delegate void WaitCallback(Object state)
經由過程這個拜托我們也能夠傳遞參數到線程中。
其實還有一種辦法可以傳遞參數到線程中,那就是經由過程回調函數來完成,只不外這類辦法實質上和第一種經由過程類的數據成員來傳遞參數的機制一樣。所以就不再細說來!
詳細的完成可以參考上面的源代碼:
全體源代碼以下:(該法式在vs 2005編譯經由過程)
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace UsingThread
{
struct RowCol
{
public int row;
public int col;
};
class ThreadOutput
{
// 樹立期待時光
static public ManualResetEvent prompt = new ManualResetEvent(false);
int _rowCount = 0;
int _colCount = 0;
char _char = '*';
int _ret = 0;
/**//// <summary>
/// 輸出參數
/// </summary>
public int RowCount
{
set { _rowCount = value; }
}
public int ColCount
{
set { _colCount = value; }
}
public char Character
{
set { _char = value; }
}
/**//// <summary>
/// 輸入參數
/// </summary>
public int RetVal
{
get { return _ret; }
}
public void Output()
{
for (int row = 0; row < _rowCount; row++)
{
for (int col = 0; col < _colCount; col++)
{
Console.Write("{0} ", _char);
_ret++;
}
Console.Write("\n");
}
// 激活事宜
prompt.Set();
}
public void Output(Object rc)
{
RowCol rowCol = (RowCol)rc;
for (int i = 0; i < rowCol.row; i++)
{
for (int j = 0; j < rowCol.col; j++)
Console.Write("{0} ", _char);
Console.Write("\n");
}
}
}
class Program
{
static void Main(string[] args)
{
ThreadOutput to1 = new ThreadOutput();
to1.RowCount = 10;
to1.ColCount = 20;
Thread thrd = new Thread(new ThreadStart(to1.Output));
// 設置為後台線程,重要是為不影響主線程的停止
thrd.IsBackground = true;
thrd.Start();
// 樹立事宜期待
ThreadOutput.prompt.WaitOne();
Console.WriteLine("{0}", to1.RetVal);
ThreadOutput to2 = new ThreadOutput();
to2.RowCount = 5;
to2.ColCount = 18;
to2.Character = '^';
Thread thrds = new Thread(new ThreadStart(to2.Output));
thrds.IsBackground = true;
thrds.Start();
thrds.Join();
Console.WriteLine("{0}", to2.RetVal);
// 傳遞參數給線程的另外一種完成
RowCol rc = new RowCol();
rc.row = 12;
rc.col = 13;
to1.Character = '@';
if (ThreadPool.QueueUserWorkItem(new WaitCallback(to1.Output), rc))
Console.WriteLine("Insert Pool is success!");
else
Console.WriteLine("Insert Pool is failed!");
Thread.Sleep(1000);
// 應用新的ThreadStart拜托來傳遞參數
rc.col = 19;
to2.Character = '#';
Thread thrdt = new Thread(new ParameterizedThreadStart(to2.Output));
thrdt.Start(rc);
thrdt.Join();
}
}
}