C# 中怎么使用带参数的多线程呢
我想写一个线程privatevoidform2_load...{Threadthread=newThread(newThreadstart(rec()))}private...
我想写一个线程
private void form2_load...
{
Thread thread=new Thread(new Threadstart(rec()))
}
private bool rec(string Ip,int Port)
{
。。。。
}
可是却通不过编译,说要输入方法名,请问,带参数的线程该怎么写呢 展开
private void form2_load...
{
Thread thread=new Thread(new Threadstart(rec()))
}
private bool rec(string Ip,int Port)
{
。。。。
}
可是却通不过编译,说要输入方法名,请问,带参数的线程该怎么写呢 展开
展开全部
寒。。。楼上怎么就直接抄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));
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));
展开全部
线程池: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();
}
}
}
下面的代码示例说明如何使用线程池异步创建和写入一组文件。每个写入操作都作为一个工作项排队,并在完成后发出信号。主线程等待所有项发出信号后退出。
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();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询