C#往線程里傳遞參數(shù)的方法小結(jié)
傳參方式有兩種:
1、創(chuàng)建帶參構(gòu)造方法類 傳參
2、利用Thread.start(8)直接傳參,該方法會(huì)接收一個(gè)對(duì)象,并將該對(duì)象傳遞給線程,因此在線程中啟動(dòng)的方法
必須接收object類型的單個(gè)參數(shù)。
Thread (ParameterizedThreadStart) 初始化 Thread 類的新實(shí)例,指定允許對(duì)象在線程啟動(dòng)時(shí)傳遞給線程的委托。
Thread (ThreadStart) 初始化 Thread 類的新實(shí)例。
由 .NET Compact Framework 支持。
Thread (ParameterizedThreadStart, Int32) 初始化 Thread 類的新實(shí)例,指定允許對(duì)象在線程啟動(dòng)時(shí)傳遞給線程的委托,并指定線程的最大堆棧大小。
Thread (ThreadStart, Int32) 初始化 Thread 類的新實(shí)例,指定線程的最大堆棧大小。
由 .NET Compact Framework 支持。
我們?nèi)绻x不帶參數(shù)的線程,可以用ThreadStart,帶一個(gè)參數(shù)的用ParameterizedThreadStart。帶多個(gè)參數(shù)的用另外的方法,下面逐一講述。
一、不帶參數(shù)的
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace AAAAAA
{
class AAA
{
public static void Main()
{
Thread t = new Thread(new ThreadStart(A));
t.Start();
Console.Read();
}
private static void A()
{
Console.WriteLine("Method A!");
}
}
}
結(jié)果顯示Method A!
二、帶一個(gè)參數(shù)的
由于ParameterizedThreadStart要求參數(shù)類型必須為object,所以定義的方法B形參類型必須為object。
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace AAAAAA
{
class AAA
{
public static void Main()
{
Thread t = new Thread(new ParameterizedThreadStart(B));
t.Start("B");
Console.Read();
}
private static void B(object obj)
{
Console.WriteLine("Method {0}!",obj.ToString ());
}
}
}
結(jié)果顯示Method B!
三、帶多個(gè)參數(shù)的
由于Thread默認(rèn)只提供了這兩種構(gòu)造函數(shù),如果需要傳遞多個(gè)參數(shù),我們可以自己將參數(shù)作為類的屬性。定義類的對(duì)象時(shí)候?qū)嵗@個(gè)屬性,然后進(jìn)行操作。
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace AAAAAA
{
class AAA
{
public static void Main()
{
My m = new My();
m.x = 2;
m.y = 3;
Thread t = new Thread(new ThreadStart(m.C));
t.Start();
Console.Read();
}
}
class My
{
public int x, y;
public void C()
{
Console.WriteLine("x={0},y={1}", this.x, this.y);
}
}
}
結(jié)果顯示x=2,y=3
四、利用結(jié)構(gòu)體給參數(shù)傳值。
定義公用的public struct,里面可以定義自己需要的參數(shù),然后在需要添加線程的時(shí)候,可以定義結(jié)構(gòu)體的實(shí)例。
//結(jié)構(gòu)體
struct RowCol
{
public int row;
public int col;
};
//定義方法
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");
}
}
以上所述是小編給大家介紹的C#往線程里傳遞參數(shù)的方法小結(jié),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
C#使用SignalR實(shí)現(xiàn)與前端vue實(shí)時(shí)通信的示例代碼
SignalR 是 ASP.NET Core 的一個(gè)庫,它簡化了在應(yīng)用程序中添加實(shí)時(shí)通信的過程,無論是聊天應(yīng)用、實(shí)時(shí)游戲還是協(xié)作工具,SignalR 都能提供高效且易于實(shí)現(xiàn)的解決方案,本文給大家介紹了C#使用SignalR實(shí)現(xiàn)與前端vue實(shí)時(shí)通信的實(shí)現(xiàn),需要的朋友可以參考下2024-10-10
C#使用System.Buffer以字節(jié)數(shù)組Byte[]操作基元類型數(shù)據(jù)
這篇文章介紹了C#使用System.Buffer以字節(jié)數(shù)組Byte[]操作基元類型數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
C#結(jié)束Excel進(jìn)程的步驟教學(xué)
在本篇文章里小編給大家分享了關(guān)于C#結(jié)束Excel進(jìn)程的步驟教學(xué)內(nèi)容,有興趣的朋友們學(xué)習(xí)下。2019-01-01
C# 利用ICSharpCode.SharpZipLib實(shí)現(xiàn)在線壓縮和解壓縮
本文主要主要介紹了利用ICSharpCode.SharpZipLib第三方的DLL庫實(shí)現(xiàn)在線壓縮和解壓縮的功能,并做了相關(guān)的代碼演示。2016-04-04
C#中GraphicsPath的AddString方法用法實(shí)例
這篇文章主要介紹了C#中GraphicsPath的AddString方法用法,實(shí)例分析了AddString方法添加字符串的相關(guān)使用技巧,需要的朋友可以參考下2015-06-06
C# DataTable.Select()根據(jù)條件篩選數(shù)據(jù)問題
這篇文章主要介紹了C# DataTable.Select()根據(jù)條件篩選數(shù)據(jù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01

