怎样给 线程 传递参数,new Thread(new ThreadStart(ThreadHandle))好像不行哇。 如题。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 shuker(秋风儿,学习.net) , 好办法,通过传递参数能不能实现? 好像不可以的,ThreadStart她要求的是一个没有参数的接口我都是这么来做的 System.Threading.Thread thScan= new Thread(new ThreadStart(ScanTarget)); thScan.Start();//Thread.Start 方法:啟動線程 我的问题是这样,我要做一个网段扫描,最大同时启动255个线程,每个线程询问一个ip。我想过了,如果把它做成类,当然可以解决存储它负责的ip的问题,但这样使问题的复杂度突然上升。首先是在某一些线程解析ip的过程中我终止了程序,线程管理是一个问题。每一个线程搜索完毕后还要和主form通讯,把扫描结果交给主form。这样又要在每一个对象内保持一个到主form的托管。最后还要把已完成任务的Scaner对象销毁。这些都是使用类方法造成的复杂问题。求解法 你本来想怎么做,现在一样可以这么做的阿本来是255个线程对象现在是255个包装了线程对象的新类阿没什么区别,而且新的类你可以添加上 start , stop等接口的 要我回答的话,我的答案将完全与 shuker(秋风儿,学习.net) 的相同。Wrox P2P 有一本 《VB.NET 线程参考手册》书上对这种问题讲得很好,只能代码所用的语言不同,其原理是一样的,可以去好好看一下,有符合 shuker(秋风儿,学习.net) 所说的好例子。可能最近有了《C# 线程参考手册》了,留意一下。 要想简单,把每个Thread负责的ip地址赋给该Thread的name属性。在Thread运行的函数里通过:Thread.currentThread.name获得该ip地址。如果参数不止一个或者不能简单的赋给name,又不想向大家说的那样,建个HashTable好了,key是Thread,vaule是含有赋给该Thread参数的Class或者Status。 除非是使用线程连接池,但使用线程数又不能控制它的优先级和abort直接用Thread 必须用类传参,参见:http://expert.csdn.net/Expert/TopicView1.asp?id=1345305我以前曾写过一个端口扫描器,代码如下:里面有二个我的公用类:ArLi.CommonPrj.getResPrj 是一个资源文件操作器,在我的http://expert.csdn.net/Expert/TopicView1.asp?id=1705200 里有另一个ArLi.CommonPrj.MsgBox.Warning 就是一个MessageBox 你自己改成MessageBox.Show 就行了 using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Threading;using System.Net;using System.Net.Sockets;namespace PortScanEx{ public class fm_Main : System.Windows.Forms.Form { private System.Windows.Forms.Label label1; protected internal System.Windows.Forms.GroupBox gBox_Target; private System.Windows.Forms.Label label2; private System.Windows.Forms.StatusBar stBar; protected internal System.Windows.Forms.StatusBarPanel stBar_Stats; private System.Windows.Forms.GroupBox gBox_Available; private System.Windows.Forms.Button btn_Start; private System.Windows.Forms.TextBox txt_IP; private System.Windows.Forms.TextBox txt_Port; protected internal System.Windows.Forms.ListBox lstBox_ok; protected internal System.Windows.Forms.ListBox lstBox_Job; private System.Windows.Forms.GroupBox gBox_Job; protected internal System.Windows.Forms.StatusBarPanel stBar_Job; protected internal System.Windows.Forms.StatusBarPanel stBar_Ok; private System.Windows.Forms.ContextMenu cMnu_Ok; private System.Windows.Forms.MenuItem cMnu_Ok_Copy; private System.Windows.Forms.MenuItem cMnu_Ok_Save; private System.Windows.Forms.MenuItem cMnu_Ok_Copy_IP; private System.Windows.Forms.MenuItem cMnu_Ok_Copy_IP_Port; private System.Windows.Forms.PictureBox picBox_Wait; private System.ComponentModel.Container components = null; public fm_Main() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form AutoCode. private void InitializeComponent() { this.txt_IP = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.gBox_Target = new System.Windows.Forms.GroupBox(); this.btn_Start = new System.Windows.Forms.Button(); this.label2 = new System.Windows.Forms.Label(); this.txt_Port = new System.Windows.Forms.TextBox(); this.lstBox_ok = new System.Windows.Forms.ListBox(); this.cMnu_Ok = new System.Windows.Forms.ContextMenu(); this.cMnu_Ok_Copy = new System.Windows.Forms.MenuItem(); this.cMnu_Ok_Copy_IP = new System.Windows.Forms.MenuItem(); this.cMnu_Ok_Copy_IP_Port = new System.Windows.Forms.MenuItem(); this.cMnu_Ok_Save = new System.Windows.Forms.MenuItem(); this.stBar = new System.Windows.Forms.StatusBar(); this.stBar_Stats = new System.Windows.Forms.StatusBarPanel(); this.stBar_Ok = new System.Windows.Forms.StatusBarPanel(); this.stBar_Job = new System.Windows.Forms.StatusBarPanel(); this.lstBox_Job = new System.Windows.Forms.ListBox(); this.gBox_Job = new System.Windows.Forms.GroupBox(); this.gBox_Available = new System.Windows.Forms.GroupBox(); this.picBox_Wait = new System.Windows.Forms.PictureBox(); this.gBox_Target.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.stBar_Stats)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.stBar_Ok)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.stBar_Job)).BeginInit(); this.gBox_Job.SuspendLayout(); this.gBox_Available.SuspendLayout(); this.SuspendLayout(); // // txt_IP // this.txt_IP.Location = new System.Drawing.Point(48, 24); this.txt_IP.Name = "txt_IP"; this.txt_IP.Size = new System.Drawing.Size(144, 21); this.txt_IP.TabIndex = 0; this.txt_IP.Text = "192.168.0.*"; // // label1 // this.label1.Location = new System.Drawing.Point(8, 24); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(24, 21); this.label1.TabIndex = 1; this.label1.Text = "IP"; this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // gBox_Target // this.gBox_Target.Controls.Add(this.btn_Start); this.gBox_Target.Controls.Add(this.label2); this.gBox_Target.Controls.Add(this.txt_IP); this.gBox_Target.Controls.Add(this.label1); this.gBox_Target.Controls.Add(this.txt_Port); this.gBox_Target.Location = new System.Drawing.Point(8, 8); this.gBox_Target.Name = "gBox_Target"; this.gBox_Target.Size = new System.Drawing.Size(200, 104); this.gBox_Target.TabIndex = 2; this.gBox_Target.TabStop = false; this.gBox_Target.Text = "Target"; this.gBox_Target.EnabledChanged += new System.EventHandler(this.gBox_Target_EnabledChanged); // // btn_Start // this.btn_Start.FlatStyle = System.Windows.Forms.FlatStyle.Popup; this.btn_Start.Location = new System.Drawing.Point(72, 72); this.btn_Start.Name = "btn_Start"; this.btn_Start.Size = new System.Drawing.Size(64, 24); this.btn_Start.TabIndex = 4; this.btn_Start.Text = "&Start"; this.btn_Start.Click += new System.EventHandler(this.btn_Start_Click); // // label2 // this.label2.Location = new System.Drawing.Point(8, 48); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(32, 23); this.label2.TabIndex = 2; this.label2.Text = "Port"; this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // txt_Port // this.txt_Port.Location = new System.Drawing.Point(48, 48); this.txt_Port.Name = "txt_Port"; this.txt_Port.Size = new System.Drawing.Size(96, 21); this.txt_Port.TabIndex = 3; this.txt_Port.Text = "139"; // // lstBox_ok // this.lstBox_ok.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.lstBox_ok.ContextMenu = this.cMnu_Ok; this.lstBox_ok.ItemHeight = 12; this.lstBox_ok.Location = new System.Drawing.Point(8, 16); this.lstBox_ok.Name = "lstBox_ok"; this.lstBox_ok.Size = new System.Drawing.Size(184, 220); this.lstBox_ok.TabIndex = 3; this.lstBox_ok.SelectedIndexChanged += new System.EventHandler(this.lstBox_ok_SelectedIndexChanged); // // cMnu_Ok // this.cMnu_Ok.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.cMnu_Ok_Copy, this.cMnu_Ok_Save}); // // cMnu_Ok_Copy // this.cMnu_Ok_Copy.Enabled = false; this.cMnu_Ok_Copy.Index = 0; this.cMnu_Ok_Copy.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.cMnu_Ok_Copy_IP, this.cMnu_Ok_Copy_IP_Port}); this.cMnu_Ok_Copy.Text = "&Copy This.."; // // cMnu_Ok_Copy_IP // this.cMnu_Ok_Copy_IP.Index = 0; this.cMnu_Ok_Copy_IP.Text = "&Copy IP"; this.cMnu_Ok_Copy_IP.Click += new System.EventHandler(this.cMnu_Ok_Copy_IP_Click); // // cMnu_Ok_Copy_IP_Port // this.cMnu_Ok_Copy_IP_Port.Index = 1; this.cMnu_Ok_Copy_IP_Port.Text = "&Copy IP:Port"; this.cMnu_Ok_Copy_IP_Port.Click += new System.EventHandler(this.cMnu_Ok_Copy_IP_Port_Click); // // cMnu_Ok_Save // this.cMnu_Ok_Save.Enabled = false; this.cMnu_Ok_Save.Index = 1; this.cMnu_Ok_Save.Text = "&Save to File..."; // // stBar // this.stBar.Location = new System.Drawing.Point(0, 373); this.stBar.Name = "stBar"; this.stBar.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] { this.stBar_Stats, this.stBar_Ok, this.stBar_Job}); this.stBar.ShowPanels = true; this.stBar.Size = new System.Drawing.Size(528, 16); this.stBar.TabIndex = 4; // // stBar_Stats // this.stBar_Stats.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring; this.stBar_Stats.Width = 492; // // stBar_Ok // this.stBar_Ok.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Contents; this.stBar_Ok.Width = 10; // // stBar_Job // this.stBar_Job.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Contents; this.stBar_Job.Width = 10; // // lstBox_Job // this.lstBox_Job.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.lstBox_Job.ItemHeight = 12; this.lstBox_Job.Location = new System.Drawing.Point(8, 24); this.lstBox_Job.Name = "lstBox_Job"; this.lstBox_Job.Size = new System.Drawing.Size(288, 328); this.lstBox_Job.TabIndex = 5; // // gBox_Job // this.gBox_Job.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.gBox_Job.Controls.Add(this.lstBox_Job); this.gBox_Job.Location = new System.Drawing.Point(216, 8); this.gBox_Job.Name = "gBox_Job"; this.gBox_Job.Size = new System.Drawing.Size(304, 360); this.gBox_Job.TabIndex = 6; this.gBox_Job.TabStop = false; this.gBox_Job.Text = "Job"; // // gBox_Available // this.gBox_Available.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left))); this.gBox_Available.Controls.Add(this.lstBox_ok); this.gBox_Available.Location = new System.Drawing.Point(8, 120); this.gBox_Available.Name = "gBox_Available"; this.gBox_Available.Size = new System.Drawing.Size(200, 248); this.gBox_Available.TabIndex = 7; this.gBox_Available.TabStop = false; this.gBox_Available.Text = "Available"; // // picBox_Wait // this.picBox_Wait.Location = new System.Drawing.Point(155, 60); this.picBox_Wait.Name = "picBox_Wait"; this.picBox_Wait.Size = new System.Drawing.Size(40, 40); this.picBox_Wait.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage; this.picBox_Wait.TabIndex = 9; this.picBox_Wait.TabStop = false; this.picBox_Wait.Visible = false; // // fm_Main // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(528, 389); this.Controls.Add(this.picBox_Wait); this.Controls.Add(this.gBox_Available); this.Controls.Add(this.gBox_Job); this.Controls.Add(this.stBar); this.Controls.Add(this.gBox_Target); this.Name = "fm_Main"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Form1"; this.Closing += new System.ComponentModel.CancelEventHandler(this.fm_Main_Closing); this.Load += new System.EventHandler(this.fm_Main_Load); this.gBox_Target.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.stBar_Stats)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.stBar_Ok)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.stBar_Job)).EndInit(); this.gBox_Job.ResumeLayout(false); this.gBox_Available.ResumeLayout(false); this.ResumeLayout(false); } #endregion [STAThread] static void Main() { Application.Run(new fm_Main()); } private void IPBox(){ ArLi.CommonPrj.MsgBox.Warning(this,"Invalid IP Format\nIP Format:\n\nNumber.Number.Number.*"); } public ArrayList threadList = new ArrayList(); private void GoScan(string ipAddr,int ServerPort) { Poc clsPoc = new Poc(); clsPoc.myIPAddr = ipAddr; clsPoc.ServerPort = ServerPort; clsPoc.sForm = this; ThreadStart tsTmp = new ThreadStart(clsPoc.PocScanOf); Thread tdTmp = new Thread(tsTmp); lock (this.lstBox_Job) { this.lstBox_Job.Items.Add(ipAddr); this.stBar_Job.Text = this.lstBox_Job.Items.Count.ToString(); } lock (this.threadList) { this.threadList.Add(tdTmp); } Application.DoEvents(); tdTmp.Start(); } private void btn_Start_Click(object sender, System.EventArgs e) { this.threadList = new ArrayList(); this.lstBox_Job.Items.Clear(); this.lstBox_ok.Items.Clear(); this.txt_Port.Text = this.txt_Port.Text.Trim(); this.txt_IP.Text = this.txt_IP.Text.Trim(); if (this.txt_IP.Text == "" || this.txt_Port.Text == "") { ArLi.CommonPrj.MsgBox.Warning(this,"IP or Port Empty!"); return; } try { int tmp = Int32.Parse(this.txt_Port.Text); } catch { ArLi.CommonPrj.MsgBox.Warning(this,"Port Must Number"); } string[] myIP = this.txt_IP.Text.Trim().Split('.'); if (myIP.Length<4){ this.IPBox(); return; } try { int tmp; for (int i=0; i<=2; i++) { tmp = Int32.Parse(myIP[i]); if (tmp <0 || tmp >254){ this.IPBox(); return; } } } 255 并不算多,在现在的CPU周期上完全能处理。 .....把线程方法放到一个类中:Thread t=new Thread(net ThreadStart(new IPScaner(ipaddr,..其他想传递的~~).DoJob));t.Start(); to ArLi2003(阿利 无业悠民) && ArLi1993(阿利 无业悠民)多谢你的代码,参考ing...我决定使用ThreadPool。多谢shuker(秋风儿,学习.net) ,AhBian(阿扁),lostinetdotcom(思考=储存+选择+变异) 指教多谢 cqing ,你的方法很特别。^_^ #region 线程代码 //主线控制程对象 Thread controlThread; //线程主要处理的函数 private void ThreadRunMethod() { while (true) { //this.BT_Start.PerformClick(); CursorPosition = Cursor.Position; AutoClick(CursorPosition.X, CursorPosition.Y); Thread.Sleep(1000); } } #endregion #region 点击开始/停止 /// <summary> /// 点击开始/停止 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnAxis_Click(object sender, EventArgs e) { if (this.btnAxis.Text.Equals("点击开始")) { try { controlThread = new Thread(new ThreadStart(ThreadRunMethod)); controlThread.Start(); } catch (Exception) { Application.DoEvents(); } this.btnAxis.Text = "点击停止"; this.timer1.Stop();//暂停时间 } else //否则就是‘点击停止’ { try { if (controlThread != null) controlThread.Abort(); } catch (Exception) { Application.DoEvents(); } this.btnAxis.Text = "点击开始"; this.timer1.Start();//启动时间 } } #endregion C# 读串口数据问题 紧急问题!!!! listview的一个问题??? <C#高级编程>:应用程序只能加载位于主执行文件所在文件夹和其子文件夹中的数据? 如何用這三個api函數 【C#新手求教帖】看书遇到一个关于继承的问题 有谁了解升腾仿真这款软件? 存款计算器没着落..有请各位高手指点.. 菜鸟提问。怎么根据控件的txt值改变就可以得到另外的控件的值 哪个能解释以下 integrated security=SSPI 这个吗? 控制台程序中可否使用App.Config配置文件,如果可以,如何配置?谢谢 求助,关于 C# 怎么用 Cult3D ActiveX Player 的
我都是这么来做的
thScan.Start();//Thread.Start 方法:啟動線程
我要做一个网段扫描,
最大同时启动255个线程,
每个线程询问一个ip。我想过了,如果把它做成类,
当然可以解决存储它负责的ip的问题,
但这样使问题的复杂度突然上升。首先是在某一些线程解析ip的过程中我终止了程序,
线程管理是一个问题。
每一个线程搜索完毕后还要和主form通讯,把扫描结果交给主form。
这样又要在每一个对象内保持一个到主form的托管。
最后还要把已完成任务的Scaner对象销毁。这些都是使用类方法造成的复杂问题。求解法
本来是255个线程对象
现在是255个包装了线程对象的新类阿
没什么区别,而且新的类你可以添加上 start , stop等接口的
ArLi.CommonPrj.getResPrj 是一个资源文件操作器,在我的http://expert.csdn.net/Expert/TopicView1.asp?id=1705200 里有
另一个ArLi.CommonPrj.MsgBox.Warning 就是一个MessageBox 你自己改成MessageBox.Show 就行了
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;using System.Threading;
using System.Net;
using System.Net.Sockets;namespace PortScanEx
{
public class fm_Main : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
protected internal System.Windows.Forms.GroupBox gBox_Target;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.StatusBar stBar;
protected internal System.Windows.Forms.StatusBarPanel stBar_Stats;
private System.Windows.Forms.GroupBox gBox_Available;
private System.Windows.Forms.Button btn_Start;
private System.Windows.Forms.TextBox txt_IP;
private System.Windows.Forms.TextBox txt_Port;
protected internal System.Windows.Forms.ListBox lstBox_ok;
protected internal System.Windows.Forms.ListBox lstBox_Job;
private System.Windows.Forms.GroupBox gBox_Job;
protected internal System.Windows.Forms.StatusBarPanel stBar_Job;
protected internal System.Windows.Forms.StatusBarPanel stBar_Ok;
private System.Windows.Forms.ContextMenu cMnu_Ok;
private System.Windows.Forms.MenuItem cMnu_Ok_Copy;
private System.Windows.Forms.MenuItem cMnu_Ok_Save;
private System.Windows.Forms.MenuItem cMnu_Ok_Copy_IP;
private System.Windows.Forms.MenuItem cMnu_Ok_Copy_IP_Port;
private System.Windows.Forms.PictureBox picBox_Wait;
private System.ComponentModel.Container components = null; public fm_Main()
{
InitializeComponent();
} protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form AutoCode.
private void InitializeComponent()
{
this.txt_IP = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.gBox_Target = new System.Windows.Forms.GroupBox();
this.btn_Start = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.txt_Port = new System.Windows.Forms.TextBox();
this.lstBox_ok = new System.Windows.Forms.ListBox();
this.cMnu_Ok = new System.Windows.Forms.ContextMenu();
this.cMnu_Ok_Copy = new System.Windows.Forms.MenuItem();
this.cMnu_Ok_Copy_IP = new System.Windows.Forms.MenuItem();
this.cMnu_Ok_Copy_IP_Port = new System.Windows.Forms.MenuItem();
this.cMnu_Ok_Save = new System.Windows.Forms.MenuItem();
this.stBar = new System.Windows.Forms.StatusBar();
this.stBar_Stats = new System.Windows.Forms.StatusBarPanel();
this.stBar_Ok = new System.Windows.Forms.StatusBarPanel();
this.stBar_Job = new System.Windows.Forms.StatusBarPanel();
this.lstBox_Job = new System.Windows.Forms.ListBox();
this.gBox_Job = new System.Windows.Forms.GroupBox();
this.gBox_Available = new System.Windows.Forms.GroupBox();
this.picBox_Wait = new System.Windows.Forms.PictureBox();
this.gBox_Target.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.stBar_Stats)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.stBar_Ok)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.stBar_Job)).BeginInit();
this.gBox_Job.SuspendLayout();
this.gBox_Available.SuspendLayout();
this.SuspendLayout();
//
// txt_IP
//
this.txt_IP.Location = new System.Drawing.Point(48, 24);
this.txt_IP.Name = "txt_IP";
this.txt_IP.Size = new System.Drawing.Size(144, 21);
this.txt_IP.TabIndex = 0;
this.txt_IP.Text = "192.168.0.*";
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(24, 21);
this.label1.TabIndex = 1;
this.label1.Text = "IP";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// gBox_Target
//
this.gBox_Target.Controls.Add(this.btn_Start);
this.gBox_Target.Controls.Add(this.label2);
this.gBox_Target.Controls.Add(this.txt_IP);
this.gBox_Target.Controls.Add(this.label1);
this.gBox_Target.Controls.Add(this.txt_Port);
this.gBox_Target.Location = new System.Drawing.Point(8, 8);
this.gBox_Target.Name = "gBox_Target";
this.gBox_Target.Size = new System.Drawing.Size(200, 104);
this.gBox_Target.TabIndex = 2;
this.gBox_Target.TabStop = false;
this.gBox_Target.Text = "Target";
this.gBox_Target.EnabledChanged += new System.EventHandler(this.gBox_Target_EnabledChanged);
//
// btn_Start
//
this.btn_Start.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btn_Start.Location = new System.Drawing.Point(72, 72);
this.btn_Start.Name = "btn_Start";
this.btn_Start.Size = new System.Drawing.Size(64, 24);
this.btn_Start.TabIndex = 4;
this.btn_Start.Text = "&Start";
this.btn_Start.Click += new System.EventHandler(this.btn_Start_Click);
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 48);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(32, 23);
this.label2.TabIndex = 2;
this.label2.Text = "Port";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// txt_Port
//
this.txt_Port.Location = new System.Drawing.Point(48, 48);
this.txt_Port.Name = "txt_Port";
this.txt_Port.Size = new System.Drawing.Size(96, 21);
this.txt_Port.TabIndex = 3;
this.txt_Port.Text = "139";
//
// lstBox_ok
//
this.lstBox_ok.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.lstBox_ok.ContextMenu = this.cMnu_Ok;
this.lstBox_ok.ItemHeight = 12;
this.lstBox_ok.Location = new System.Drawing.Point(8, 16);
this.lstBox_ok.Name = "lstBox_ok";
this.lstBox_ok.Size = new System.Drawing.Size(184, 220);
this.lstBox_ok.TabIndex = 3;
this.lstBox_ok.SelectedIndexChanged += new System.EventHandler(this.lstBox_ok_SelectedIndexChanged);
//
// cMnu_Ok
//
this.cMnu_Ok.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.cMnu_Ok_Copy,
this.cMnu_Ok_Save});
//
// cMnu_Ok_Copy
//
this.cMnu_Ok_Copy.Enabled = false;
this.cMnu_Ok_Copy.Index = 0;
this.cMnu_Ok_Copy.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.cMnu_Ok_Copy_IP,
this.cMnu_Ok_Copy_IP_Port});
this.cMnu_Ok_Copy.Text = "&Copy This..";
//
// cMnu_Ok_Copy_IP
//
this.cMnu_Ok_Copy_IP.Index = 0;
this.cMnu_Ok_Copy_IP.Text = "&Copy IP";
this.cMnu_Ok_Copy_IP.Click += new System.EventHandler(this.cMnu_Ok_Copy_IP_Click);
//
// cMnu_Ok_Copy_IP_Port
//
this.cMnu_Ok_Copy_IP_Port.Index = 1;
this.cMnu_Ok_Copy_IP_Port.Text = "&Copy IP:Port";
this.cMnu_Ok_Copy_IP_Port.Click += new System.EventHandler(this.cMnu_Ok_Copy_IP_Port_Click);
//
// cMnu_Ok_Save
//
this.cMnu_Ok_Save.Enabled = false;
this.cMnu_Ok_Save.Index = 1;
this.cMnu_Ok_Save.Text = "&Save to File...";
//
// stBar
//
this.stBar.Location = new System.Drawing.Point(0, 373);
this.stBar.Name = "stBar";
this.stBar.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
this.stBar_Stats,
this.stBar_Ok,
this.stBar_Job});
this.stBar.ShowPanels = true;
this.stBar.Size = new System.Drawing.Size(528, 16);
this.stBar.TabIndex = 4;
//
// stBar_Stats
//
this.stBar_Stats.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring;
this.stBar_Stats.Width = 492;
// stBar_Ok
//
this.stBar_Ok.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Contents;
this.stBar_Ok.Width = 10;
//
// stBar_Job
//
this.stBar_Job.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Contents;
this.stBar_Job.Width = 10;
//
// lstBox_Job
//
this.lstBox_Job.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.lstBox_Job.ItemHeight = 12;
this.lstBox_Job.Location = new System.Drawing.Point(8, 24);
this.lstBox_Job.Name = "lstBox_Job";
this.lstBox_Job.Size = new System.Drawing.Size(288, 328);
this.lstBox_Job.TabIndex = 5;
//
// gBox_Job
//
this.gBox_Job.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.gBox_Job.Controls.Add(this.lstBox_Job);
this.gBox_Job.Location = new System.Drawing.Point(216, 8);
this.gBox_Job.Name = "gBox_Job";
this.gBox_Job.Size = new System.Drawing.Size(304, 360);
this.gBox_Job.TabIndex = 6;
this.gBox_Job.TabStop = false;
this.gBox_Job.Text = "Job";
//
// gBox_Available
//
this.gBox_Available.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)));
this.gBox_Available.Controls.Add(this.lstBox_ok);
this.gBox_Available.Location = new System.Drawing.Point(8, 120);
this.gBox_Available.Name = "gBox_Available";
this.gBox_Available.Size = new System.Drawing.Size(200, 248);
this.gBox_Available.TabIndex = 7;
this.gBox_Available.TabStop = false;
this.gBox_Available.Text = "Available";
//
// picBox_Wait
//
this.picBox_Wait.Location = new System.Drawing.Point(155, 60);
this.picBox_Wait.Name = "picBox_Wait";
this.picBox_Wait.Size = new System.Drawing.Size(40, 40);
this.picBox_Wait.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
this.picBox_Wait.TabIndex = 9;
this.picBox_Wait.TabStop = false;
this.picBox_Wait.Visible = false;
//
// fm_Main
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(528, 389);
this.Controls.Add(this.picBox_Wait);
this.Controls.Add(this.gBox_Available);
this.Controls.Add(this.gBox_Job);
this.Controls.Add(this.stBar);
this.Controls.Add(this.gBox_Target);
this.Name = "fm_Main";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form1";
this.Closing += new System.ComponentModel.CancelEventHandler(this.fm_Main_Closing);
this.Load += new System.EventHandler(this.fm_Main_Load);
this.gBox_Target.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.stBar_Stats)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.stBar_Ok)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.stBar_Job)).EndInit();
this.gBox_Job.ResumeLayout(false);
this.gBox_Available.ResumeLayout(false);
this.ResumeLayout(false); }
#endregion [STAThread]
static void Main()
{
Application.Run(new fm_Main());
} private void IPBox(){
ArLi.CommonPrj.MsgBox.Warning(this,"Invalid IP Format\nIP Format:\n\nNumber.Number.Number.*");
} public ArrayList threadList = new ArrayList(); private void GoScan(string ipAddr,int ServerPort) {
Poc clsPoc = new Poc();
clsPoc.myIPAddr = ipAddr;
clsPoc.ServerPort = ServerPort;
clsPoc.sForm = this;
ThreadStart tsTmp = new ThreadStart(clsPoc.PocScanOf);
Thread tdTmp = new Thread(tsTmp); lock (this.lstBox_Job) {
this.lstBox_Job.Items.Add(ipAddr);
this.stBar_Job.Text = this.lstBox_Job.Items.Count.ToString();
}
lock (this.threadList) {
this.threadList.Add(tdTmp);
} Application.DoEvents();
tdTmp.Start();
} private void btn_Start_Click(object sender, System.EventArgs e) {
this.threadList = new ArrayList();
this.lstBox_Job.Items.Clear();
this.lstBox_ok.Items.Clear();
this.txt_Port.Text = this.txt_Port.Text.Trim();
this.txt_IP.Text = this.txt_IP.Text.Trim(); if (this.txt_IP.Text == "" || this.txt_Port.Text == "") {
ArLi.CommonPrj.MsgBox.Warning(this,"IP or Port Empty!");
return;
} try {
int tmp = Int32.Parse(this.txt_Port.Text);
}
catch {
ArLi.CommonPrj.MsgBox.Warning(this,"Port Must Number");
} string[] myIP = this.txt_IP.Text.Trim().Split('.'); if (myIP.Length<4){
this.IPBox();
return;
} try {
int tmp;
for (int i=0; i<=2; i++) {
tmp = Int32.Parse(myIP[i]);
if (tmp <0 || tmp >254){
this.IPBox();
return;
}
}
}
把线程方法放到一个类中:Thread t=new Thread(net ThreadStart(new IPScaner(ipaddr,..其他想传递的~~).DoJob));
t.Start();
多谢你的代码,参考ing...
我决定使用ThreadPool。
多谢shuker(秋风儿,学习.net) ,AhBian(阿扁),lostinetdotcom(思考=储存+选择+变异) 指教
多谢 cqing ,你的方法很特别。^_^
//主线控制程对象
Thread controlThread;
//线程主要处理的函数
private void ThreadRunMethod()
{
while (true)
{
//this.BT_Start.PerformClick();
CursorPosition = Cursor.Position;
AutoClick(CursorPosition.X, CursorPosition.Y);
Thread.Sleep(1000);
}
}
#endregion
#region 点击开始/停止
/// <summary>
/// 点击开始/停止
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnAxis_Click(object sender, EventArgs e)
{
if (this.btnAxis.Text.Equals("点击开始"))
{
try
{
controlThread = new Thread(new ThreadStart(ThreadRunMethod));
controlThread.Start();
}
catch (Exception)
{
Application.DoEvents();
} this.btnAxis.Text = "点击停止";
this.timer1.Stop();//暂停时间
}
else //否则就是‘点击停止’
{
try
{
if (controlThread != null)
controlThread.Abort();
}
catch (Exception)
{
Application.DoEvents();
}
this.btnAxis.Text = "点击开始";
this.timer1.Start();//启动时间
}
}
#endregion