请问我怎么样才能不卡UI
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;
using System.Runtime.InteropServices;
namespace ARP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Thread[] thread = new Thread[255];
ThreadStart threadMethod;
for (int i = 0; i < 255; i++)
{
threadMethod = new ThreadStart(LanSearch.LanSearchThreadMethod);
thread[i] = new Thread(threadMethod);
thread[i].Name = i.ToString();
thread[i].Start();
if (!thread[i].Join(100))
{
thread[i].Abort();
}
}
}
}
}
//说明:这是一段很简短的代码,LanSearch类的LanSearchThreadMethod()方法用于向局域网的发送数据包,得到响应的主机,
但在我在点击button1以后,界面就会卡死,所以我想问一下怎么样处理这种多线程问题。 --------------------编程问答-------------------- 255个线程,如果你那个线程函数还处理很多事情,当然会卡死! --------------------编程问答--------------------
回sdl2005lyx,线程函数处理的事情很少,我是想知道怎么样才能不卡UI呢? --------------------编程问答-------------------- 你的UI卡在thread[i].Join(100)调用上, --------------------编程问答-------------------- 其实我觉得你的功能不需要调用thread[i].Join(100),放开让它们跑就可以了, --------------------编程问答-------------------- 或者你加入另一个button,单击的时候在逐一调用thread[i].Join(100),
因为thread[i].Join(100)会阻塞当前线程,等待thread[i]结束,你把这句话放在循环里,相当于没有多线程,只是一个线程完成后,再开下一个线程,这样当然卡了,而且也没有并发, --------------------编程问答--------------------
回stonespace:我试过把Join()注释掉执行,结果还是一样,我怎么样才能让每个线程都异步执行呢?
附:LanSearch类代码,是从网上抄来的.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading;
using System.Runtime.InteropServices;
namespace ARP
{
class LanSearch
{
/// <summary>
/// 取MAC地址
/// </summary>
/// <param name="DestIP">目标IP</param>
/// <param name="SrcIP">源IP</param>
/// <param name="pMacAddr">MAC地址</param>
/// <param name="PhyAddrLen">MAC地址的长度</param>
/// <returns></returns>
[DllImport("iphlpapi.dll", ExactSpelling = true)]
private static unsafe extern int SendARP(int DestIP, int SrcIP, [Out] byte[] pMacAddr, ref int PhyAddrLen);
/// <summary>
/// 在线程中扫描
/// </summary>
public static void LanSearchThreadMethod()
{
int i = Convert.ToUInt16(Thread.CurrentThread.Name);
//Console.Write(".");
string strIP = "192.168.1." + i.ToString();
//IPHostEntry ip = null;
IPAddress ip = null;
try
{
//ip = Dns.GetHostEntry(strIP);
ip = IPAddress.Parse(strIP);
}
catch
{
//Console.WriteLine("请勿输入非法IP地址");
return;
}
byte[] b = new byte[6]; //字节数组用来存放MAC物理地址
int len = b.Length;
//int r = SendARP(BitConverter.ToInt32(ip.AddressList[0].GetAddressBytes(), 0), 0, b, ref len);
int r = SendARP(BitConverter.ToInt32(ip.GetAddressBytes(), 0), 0, b, ref len); //B返回调用。
int num = BitConverter.ToInt32(b, 0);
string mac = BitConverter.ToString(b, 0, 6);
if (num != 0)
{//有效MAC
//Console.WriteLine("\r\n{0}--{1}--{2}", ip.AddressList[0].ToString(), ip.HostName, mac);
//Console.WriteLine("\r\n{0}--{1}", ip.ToString(), mac);
}
}
}
}
--------------------编程问答-------------------- 你把这三行全注释掉还卡吗?
if (!thread[i].Join(100))
{
thread[i].Abort();
}
--------------------编程问答--------------------
回stonespace :是的,依然会卡 --------------------编程问答-------------------- 求帮助。。 --------------------编程问答-------------------- 你可以将停止线程的循环,改为间接改标志数组=FALSE ,这样很快。
而线程中循环,发现此条件,则自己安全停止结束自己的运行,
若要强制则,用ABORT ,但你的线程中,最好加try执行,
且CATCH段,代码要最短,才不耽误时间,切记。
.JION 会阻塞到时间,所以批量停止,耽误时间,卡,不现实的 --------------------编程问答--------------------
楼上这么多高手出招 如果还卡
那还有一个办法
换电脑
补充:.NET技术 , C#