Public class ThreadMulti
{
# Region variable
Public delegate void DelegateComplete ();
Public delegate void DelegateWork (int taskindex, int threadindex );
Public DelegateComplete CompleteEvent;
Public DelegateWork WorkMethod;
Private Thread [] _ threads;
Private bool [] _ threadState;
Private int _ taskCount = 0;
Private int _ taskindex = 0;
Private int _ threadCount = 20; // defines the thread
<Span id = "more-787"> </span>
# Endregion
Public ThreadMulti (int taskcount)
{
_ TaskCount = taskcount;
}
Public ThreadMulti (int taskcount, int threadCount)
{
_ TaskCount = taskcount;
_ ThreadCount = threadCount;
}
# Region get task reference old Yu http://www.cnblogs.com/michael-zhangyu/archive/2009/07/16/1524737.html blog
Private int GetTask ()
{
Lock (this)
{
If (_ taskindex <_ taskCount)
{
_ Taskindex ++;
Return _ taskindex;
}
Else
{
Return 0;
}
}
}
# Endregion
# Region Start
/// <Summary>
/// Start
/// </Summary>
Public void Start ()
{
// Using Hawker (http://www.cnblogs.com/tietaren/) recommendations, streamlined a lot
_ Taskindex = 0;
Int num = _ taskCount <_ threadCount? _ TaskCount: _ threadCount;
_ ThreadState = new bool [num];
_ Threads = new Thread [num];
For (int n = 0; n <num; n ++)
{
_ ThreadState [n] = false;
_ Threads [n] = new Thread (new ParameterizedThreadStart (Work ));
_ Threads [n]. Start (n );
}
}
/// <Summary>
/// End thread
/// </Summary>
Public void Stop ()
{
For (int I = 0; I <_ threads. Length; I ++)
{
_ Threads [I]. Abort ();
}
// String s = "";
// For (int j = 0; j <_ threads. Length; j ++)
//{
// S + = _ threads [j]. ThreadState. ToString () + "\ r \ n ";
//}
// MessageBox. Show (s );
}
# Endregion
# Region Work
Public void Work (object arg)
{
// Extract and execute the task
Int threadindex = int. Parse (arg. ToString ());
Int taskindex = GetTask ();
While (taskindex! = 0 & WorkMethod! = Null)
{
WorkMethod (taskindex, threadindex + 1 );
Taskindex = GetTask ();
}
// All tasks are completed
_ ThreadState [threadindex] = true;
// Process concurrency. If two threads are completed simultaneously, only one complete event can be triggered.
Lock (this)
{
For (int I = 0; I <_ threadState. Length; I ++)
{
If (_ threadState [I] = false)
{
Return;
}
}
// If all are completed
If (CompleteEvent! = Null)
{
CompleteEvent ();
}
// Reset the thread status after the complete event is triggered
// The above judgment cannot be passed for the next concurrent thread
For (int j = 0; j <_ threadState. Length; j ++)
{
_ ThreadState [j] = false;
}
}
}
# Endregion
}
Private void button6_Click (object sender, EventArgs e)
{
ThreadPool. QueueUserWorkItem (new WaitCallback (ThreadStartMethod2 ));
}
ThreadMulti thread;
Public void ThreadStartMethod2 (object arg)
{
Int workcount = listView1.Items. Count;
/// _ Count = workcount * 100;
Thread = new ThreadMulti (workcount );
Thread. WorkMethod = new ThreadMulti. DelegateWork (DoWork2 );
Thread. CompleteEvent = new ThreadMulti. DelegateComplete (WorkComplete2 );
Thread. Start ();
}
Public void DoWork2 (int index, int threadindex)
{
HttpWebRequest hwr = (HttpWebRequest) WebRequest. Create (listView1.Items [index-1]. SubItems [1]. Text );
MessageBox. Show (Convert. ToString (index ));
Hwr. AllowAutoRedirect = false; // redirection not allowed
Hwr. Timeout = Convert. ToInt32 (numericUpDown3.Value) * 1000; // set the connection Timeout value.
Hwr. Method = "GET"; // Protocol: GET, HEAD, POST, PUT, DELETE, TRACE, or OPTIONS.
Try
{
HttpWebResponse hwrs = (HttpWebResponse) hwr. GetResponse ();
Button1.Text = (int) hwrs. StatusCode). ToString ();
}
Catch (Exception ex)
{
// MessageBox. Show (ex. ToString ());
}
For (int I = 0; I <100; I ++)
{
Thread. Sleep (threadindex * 5 );
// DisplayProgress2 (threadindex, index, I );
}
}
// End
Public void WorkComplete2 ()
{
MessageBox. Show ("detected ");
}
Private void button7_Click (object sender, EventArgs e)
{
Thread. Stop ();
}
}
From http://www.xssxss.com/fuck/787.xss