C# 在C语言中使用参数化函数#
嗨,这是我的多线程代码,工作正常C# 在C语言中使用参数化函数#,c#,multithreading,.net-3.5,parameter-passing,C#,Multithreading,.net 3.5,Parameter Passing,嗨,这是我的多线程代码,工作正常 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; using System.Threading; namesp
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Threading;
namespace searchIpAdresses
{
public partial class frmSearchIpRange : Form
{
public frmSearchIpRange()
{
InitializeComponent();
}
int intStartIp, intEndIp;
private void frmSearchIpRange_Load(object sender, EventArgs e)
{
startIp.Text = "0.0.0.0";
endIp.Text = "0.1.0.0";
nudThreads.Value = 1;
}
private bool calcIpAddressRange()
{
intStartIp = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(IPAddress.Parse(startIp.Text).GetAddressBytes(), 0));
intEndIp = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(IPAddress.Parse(endIp.Text).GetAddressBytes(), 0));
if (intStartIp>intEndIp)
{
MessageBox.Show("End Ip must be bigger than Begin Ip!");
return false;
}
else
{
return true;
}
}
private void btnShow_Click(object sender, EventArgs e)
{
if (calcIpAddressRange())
{
int threadCount = Convert.ToInt32(nudThreads.Value);
Thread[] threads = new Thread[threadCount];
for (int i = 0; i < threadCount; i++)
{
threads[i] = new Thread(new ThreadStart(getPerformance));
threads[i].Name = string.Format(i.ToString());
}
foreach (Thread t in threads)
{
t.Start();
}
//for (int i = 0; i < nudThreads.Value; i++)
//{
// if (threads[i].IsAlive)
// threads[i].Abort();
//}
}
}
private void getPerformance()//int sampleStartIp, int sampleEndIp
{
if (calcIpAddressRange())
{
lbAddress.Items.Clear();
progressBar1.Maximum = intEndIp - intStartIp;
int i;
for (i = intStartIp; i < intEndIp; i++)
{
string ipAddress = new IPAddress(BitConverter.GetBytes(IPAddress.NetworkToHostOrder(i))).ToString();
progressBar1.Value++;
lbAddress.Items.Add(ipAddress);
}
MessageBox.Show(i.ToString() + " addresses added", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
progressBar1.Value = 0;
}
}
}
}
1) 您可能应该使用线程池或任务(Fx4)
2) 当您可以使用代理lamba'a(.NET 3及更高版本)时
对于旧版本,可以使用和辅助对象来保持开始和结束
编辑以调整循环:
for (int i = 0; i < threadCount; i++)
{
// some code to update intStartIp, intEndIp
//threads[i] = new Thread(new ThreadStart(getPerformance));
threads[i] = new Thread(() => getPerformance(intStartIp, intEndIp));
threads[i].Name = string.Format(i.ToString());
}
for(int i=0;igetPerformance(intStartIp,intEndIp));
线程[i].Name=string.Format(i.ToString());
}
1)您可能应该使用线程池或任务(Fx4)
2) 当您可以使用代理lamba'a(.NET 3及更高版本)时
对于旧版本,可以使用和辅助对象来保持开始和结束
编辑以调整循环:
for (int i = 0; i < threadCount; i++)
{
// some code to update intStartIp, intEndIp
//threads[i] = new Thread(new ThreadStart(getPerformance));
threads[i] = new Thread(() => getPerformance(intStartIp, intEndIp));
threads[i].Name = string.Format(i.ToString());
}
for(int i=0;igetPerformance(intStartIp,intEndIp));
线程[i].Name=string.Format(i.ToString());
}
您可以用来将对象传递到被调用的方法中您可以用来将对象传递到被调用的方法中平台、语言?它看起来像是C#,但提到它只是一个小小的努力。很抱歉我忘了它,现在已编辑..getPerformance将在非ui线程上执行,因此对progressBar1的更改将不起作用。此外,多个线程可能同时更改属性,使粘贴的代码容易出现线程问题。平台、语言?它看起来像是C#,但提到它只是一个小小的努力。很抱歉我忘了它,现在已编辑..getPerformance将在非ui线程上执行,因此对progressBar1的更改将不起作用。此外,多个线程可能同时更改属性,使粘贴的代码容易出现线程问题。我可以将其用于多线程吗?因为这个“t”变量不是数组类型,我可以将它用于多线程吗?因为这个“t”变量不是数组类型
for (int i = 0; i < threadCount; i++)
{
// some code to update intStartIp, intEndIp
//threads[i] = new Thread(new ThreadStart(getPerformance));
threads[i] = new Thread(() => getPerformance(intStartIp, intEndIp));
threads[i].Name = string.Format(i.ToString());
}