C# 中怎么使用带参数的多线程呢
发布网友
发布时间:2022-04-09 16:34
我来回答
共2个回答
热心网友
时间:2022-04-09 18:03
寒。。。楼上怎么就直接抄msdn的代码呢。。。
msdn的代码很不好看的。。。
楼主我帮你写了个例子,你自己看看就明白了。:
struct IpAndPort
{
public string Ip;
public int Port;
}
private void Form1_Load(object sender, EventArgs e)
{
Thread thr = new Thread(a);
IpAndPort aa = new IpAndPort();
aa.Ip = "123.123.123.123";
aa.Port = 123;
thr.Start((object)aa);
}
private void a(object aa)
{
IpAndPort ip = (IpAndPort)aa;
MessageBox.Show(ip.Ip + ":" + ip.Port.ToString());
}
注:参数必须是object型的,并且只能一个。其他类型的可以通过显式转换成object型,然后在线程方法里再转回来。
如果有多个变量则可以自己定义一个struct或者类,然后转换成object型,然后在线程方法里再转回来。
另ThreadStart是没有参数的,要使用带参数的方法这样使用:
Thread thr = new Thread(a);
而不是
Thread thr = new Thread(new ThreadStart(a));
热心网友
时间:2022-04-09 19:21
线程池:msdn例子如下
下面的代码示例说明如何使用线程池异步创建和写入一组文件。每个写入操作都作为一个工作项排队,并在完成后发出信号。主线程等待所有项发出信号后退出。
using System;
using System.IO;
using System.Security.Permissions;
using System.Threading;
// Request permission to create and write files to C:\TestTest.
[assembly: FileIOPermissionAttribute(SecurityAction.RequestMinimum,
All = @"C:\TestTest")]
class Test
{
static void Main()
{
const int numberOfFiles = 5;
string dirName = @"C:\TestTest";
string fileName;
byte[] byteArray;
Random randomGenerator = new Random();
ManualResetEvent[] manualEvents =
new ManualResetEvent[numberOfFiles];
State stateInfo;
if(!Directory.Exists(dirName))
{
Directory.CreateDirectory(dirName);
}
// Queue the work items that create and write to the files.
for(int i = 0; i < numberOfFiles; i++)
{
fileName = string.Concat(
dirName, @"\Test", i.ToString(), ".dat");
// Create random data to write to the file.
byteArray = new byte[1000000];
randomGenerator.NextBytes(byteArray);
manualEvents[i] = new ManualResetEvent(false);
stateInfo =
new State(fileName, byteArray, manualEvents[i]);
ThreadPool.QueueUserWorkItem(new WaitCallback(
Writer.WriteToFile), stateInfo);
}
// Since ThreadPool threads are background threads,
// wait for the work items to signal before exiting.
WaitHandle.WaitAll(manualEvents);
Console.WriteLine("Files written - main exiting.");
}
}
// Maintain state to pass to WriteToFile.
class State
{
public string fileName;
public byte[] byteArray;
public ManualResetEvent manualEvent;
public State(string fileName, byte[] byteArray,
ManualResetEvent manualEvent)
{
this.fileName = fileName;
this.byteArray = byteArray;
this.manualEvent = manualEvent;
}
}
class Writer
{
static int workItemCount = 0;
Writer() {}
public static void WriteToFile(object state)
{
int workItemNumber = workItemCount;
Interlocked.Increment(ref workItemCount);
Console.WriteLine("Starting work item {0}.",
workItemNumber.ToString());
State stateInfo = (State)state;
FileStream fileWriter = null;
// Create and write to the file.
try
{
fileWriter = new FileStream(
stateInfo.fileName, FileMode.Create);
fileWriter.Write(stateInfo.byteArray,
0, stateInfo.byteArray.Length);
}
finally
{
if(fileWriter != null)
{
fileWriter.Close();
}
// Signal Main that the work item has finished.
Console.WriteLine("Ending work item {0}.",
workItemNumber.ToString());
stateInfo.manualEvent.Set();
}
}
}