线程和多线程问题 [英] Threads and Multi-Threads issue
问题描述
我正在构建一个用于扫描IP地址的程序.我建立了一个检查
端口的类给定的IP地址.我将此类作为线程循环运行.我显然阻止了
的数量 可以并行运行的线程.问题在于线程总是返回退出
代码:259,大约半分钟后,程序挂起.
我试图在google,论坛和代码259的含义中进行搜索.所以我发现了一些
认为它只返回不表示任何含义的代码.在其他论坛上,我看到的是
表示使用大量内存存在问题.所以我尝试在
中仅使用2个线程平行.它仍然无法正常工作.我尝试查看使用线程的另一种方法,所以我读了
关于线程池,我将代码更改为使用线程池,但仍然无法正常工作.的当然要确保在课堂上我不使用全局变量.
代码:
private void Get_Port_With_Status_Thread_Inventer()
{
for (int Thread_Cnt_Get_Port_N_Status = 1; Thread_Cnt_Get_Port_N_Status < IP_Address.Length + 1; Thread_Cnt_Get_Port_N_Status++)
{
if (Open_Threads_Port_N_Status_Counter >= Max_Thread_Allowed)
{
Thread_Cnt_Get_Port_N_Status--;
continue;
}
Thread New_Port_N_Status_Thread = new Thread(new ParameterizedThreadStart(Get_Port_With_Status_Thread));
New_Port_N_Status_Thread.Start(Thread_Cnt_Get_Port_N_Status);
Open_Threads_Port_N_Status_Counter++;
}
End_Get_Port_N_Status_Inventer = true;
}
private void Get_Port_With_Status_Thread(object Index_On_Array_Obj)
{
int Index_On_Array = (int)Index_On_Array_Obj;
string IP_Address_Str = IP_Address[Index_On_Array - 1];
IPEndPoint IP_Address_Connect_Data_Base = new IPEndPoint(IPAddress.Parse(IP_Address_Str), 80);
Socket Socket_IP_Address_Connect_Data_Base = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
string Ports_Rep_Index = 80.ToString();
DateTime Current_New_Now_Seconds = DateTime.Now.Date;
try
{
Socket_IP_Address_Connect_Data_Base.Connect(IP_Address_Connect_Data_Base);
Socket_IP_Address_Connect_Data_Base.Disconnect(true);
IP_Port[Index_On_Array - 1] = "Port 80 - Live";
}
catch (SocketException g)
{
IP_Port[Index_On_Array - 1] = "Port 80 - Dead";
}
Open_Threads_Port_N_Status_Counter--;
}
解决方案很简单.不要使用debugger运行多个线程,所以如果您有Microsoft
Visual Studio,您想运行很多线程,在没有调试器的情况下运行程序!
I'm building a program for scanning IP addresses. I built a class that checks the ports of
a given IP address. I run this class in loop as thread. I obviously blocking the amount of
thread that can be run in parallel. The problem is that always the thread returns an exit
code: 259, and after half a minute or so, the program hangs.
I tried to search in google, forums, and here what the code 259 means. So i found in some
forums that it simply return code that not meaning any thing. In other forums i see that is
means that there a problem with using a lot of memory. So i try to use only 2 threads in
paralel. It still doesnt work. I try to see another methods to work with threads, so i read
about threads pool, i change the code to use threads pool, but it still not work. I of
course made sure that in class i dont use global variableas.
Code :
private void Get_Port_With_Status_Thread_Inventer()
{
for (int Thread_Cnt_Get_Port_N_Status = 1; Thread_Cnt_Get_Port_N_Status < IP_Address.Length + 1; Thread_Cnt_Get_Port_N_Status++)
{
if (Open_Threads_Port_N_Status_Counter >= Max_Thread_Allowed)
{
Thread_Cnt_Get_Port_N_Status--;
continue;
}
Thread New_Port_N_Status_Thread = new Thread(new ParameterizedThreadStart(Get_Port_With_Status_Thread));
New_Port_N_Status_Thread.Start(Thread_Cnt_Get_Port_N_Status);
Open_Threads_Port_N_Status_Counter++;
}
End_Get_Port_N_Status_Inventer = true;
}
private void Get_Port_With_Status_Thread(object Index_On_Array_Obj)
{
int Index_On_Array = (int)Index_On_Array_Obj;
string IP_Address_Str = IP_Address[Index_On_Array - 1];
IPEndPoint IP_Address_Connect_Data_Base = new IPEndPoint(IPAddress.Parse(IP_Address_Str), 80);
Socket Socket_IP_Address_Connect_Data_Base = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
string Ports_Rep_Index = 80.ToString();
DateTime Current_New_Now_Seconds = DateTime.Now.Date;
try
{
Socket_IP_Address_Connect_Data_Base.Connect(IP_Address_Connect_Data_Base);
Socket_IP_Address_Connect_Data_Base.Disconnect(true);
IP_Port[Index_On_Array - 1] = "Port 80 - Live";
}
catch (SocketException g)
{
IP_Port[Index_On_Array - 1] = "Port 80 - Dead";
}
Open_Threads_Port_N_Status_Counter--;
}
The solution was simply . Dont run multiple threads with debugger , so if you have Microsoft
Visual Studio and you want run a lot of threads , run the program without the debugger !
这篇关于线程和多线程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!