Thread, thread count control!

Source: Internet
Author: User
Here is an introduction to multithreading and thread pool.
Http://edobnet.cnblogs.com/archive/2005/11/29/287094.html

// Private int threadnum = 0;
// Private autoresetevent myresetevent = new autoresetevent (false );
/**/ /// <Summary>
///Thread Recorder
/// </Summary>
Private   Int Number =   0 ;

/**/ /// <Summary>
///Thread pool execution
/// </Summary>
Public   Void Execute ()
{
Int Maxcount =   Int . Parse (configurationsettings. receivettings [ " Count " ]);
Int Time =   Int . Parse (configurationsettings. receivettings [ " Time " ]);
Foreach (Tg_evalutebasedatasset Service In   This )
{


// System. Threading. threadstart Ts = new system. Threading. threadstart (service. getxmlresult );
// System. Threading. Thread th = new system. Threading. Thread (TS );
// Th. Start ();
Waitcallback async =   New Waitcallback (fexeute );

While ( True )
{

Monitor. Enter (number );
If (Number > Maxcount)
Thread. Sleep (time );
Else
Break ;
Monitor. Exit (number );
}
If (Threadpool. queueuserworkitem (async, Service ))
Interlocked. increment ( Ref Number );
}
While ( True )
{
// Myresetevent. Set ();
Monitor. Enter (number );
If (Number <=   0 )
Break ;
Else
Thread. Sleep (time );
Monitor. Exit (number );
}
Ontgfinishreport ( New Eventargs ());

}
/**/ ///   <Summary>
/// The thread executes a computing task.
///   </Summary>
///   <Param name = "Param"> Execution object <See CREF = "glenet. HG. htresolver. tg_evalutebasedatasset"> Tg_evalutebasedatasset </See> </param>
Public Void Fexeute ( Object Param)
{
// Myresetevent. waitone ();
Using (Tg_evalutebasedatasset Service = (Tg_evalutebasedatasset) PARAM)
{< br> service. doresult ();
writefile (path, service);
ontgreport ( New reportevent (service. te_pass_range, service. te_year, service. te_month, int . parse (service. te_ I _e_flag), int . parse (service. te_entry_type), number);
}
Interlocked. decrement ( Ref Number );
Console. writeline ( " Left: " + Number );

}

ByProgramControls the number of threads and is protected by monitor!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.