User Tips: Using Return Values from a SQL Server Stored Procedure to Customize Error Messages_QQGB.c
2007-11-11 10:42:12 来源:WEB开发网 闂傚倸鍊搁崐鎼佸磹閹间礁纾归柟闂寸绾惧綊鏌熼梻瀵割槮缁炬儳缍婇弻鐔兼⒒鐎靛壊妲紒鎯у⒔閹虫捇鈥旈崘顏佸亾閿濆簼绨绘い鎺嬪灪閵囧嫰骞囬姣挎捇鏌熸笟鍨妞ゎ偅绮撳畷鍗炍旈埀顒勭嵁婵犲嫮纾介柛灞捐壘閳ь剛鎳撻~婵嬪Ω閳轰胶鐤呯紓浣割儐椤戞瑩宕ョ€n喗鐓曟い鎰靛亝缁舵氨绱撻崘鈺傜婵﹤顭峰畷鎺戔枎閹搭厽袦婵犵數濮崑鎾绘⒑椤掆偓缁夌敻骞嗛悙鍝勭婵烇綆鍓欐俊鑲╃磼閹邦収娈滈柡灞糕偓鎰佸悑閹肩补鈧尙鏁栧┑鐐村灦閹稿摜绮旈悽绋课﹂柛鏇ㄥ灠閸愨偓濡炪倖鍔﹀鈧繛宀婁邯濮婅櫣绱掑Ο璇茶敿闂佺ǹ娴烽弫璇差嚕婵犳碍鏅插璺猴工瀹撳棝姊虹紒妯哄缂佷焦鎸冲畷鎴﹀箻鐠囧弶宓嶅銈嗘尰缁嬫垶绂嶉悙顒佸弿婵☆垳鍘ф禍楣冩倵濮樼偓瀚�

核心提示: ·一个简单的CustomItem使用案例·CustomizeGoogle:让Firefox搜索更上·用CustomDataSource向iReport中传递数·User Tips: Using Return Values from·Custom C
- ·一个简单的CustomItem使用案例
·CustomizeGoogle:让Firefox搜索更上
·用CustomDataSource向iReport中传递数
·User Tips: Using Return Values from
·Custom Channel Sinks被我征服了
·Cisco Unified Customer Voice Portal
·使用CustomValidator模仿show出一个co
·Efficient service of Microsoft‘s C
·Creating Custom Web Controls in C#
Custom Thread Pooling Template
Now that we have covered the basics of Visual Studio .NET project types and templates, let us look at how we can write our own custom project template that will generate a thread pool that can be used by the developer without any modifications at all. Before we start discussing the custom template, let us focus on writing a thread pool class that will be a part of this template.
A thread pool usually consists of two classes: one class that represents a thread, and another class that represents the object that manages a pool of these thread objects. The thread class is fairly simple: it is simply an encapsulation of the built in System.Threading.Thread class, but the thread pool class is slightly more involved. This thread pool class has to create all the threads in the pool, assign them work, and keep track of idle threads so that further work can be assigned to them. Further, the threads in a thread pool are created once, and if a thread is idle, the thread goes into an efficient wait mode (like a sleep call). When there is work to be processed, the thread pool is responsible for bringing an idle thread back into active mode and assigning it work.
Let us start by discussing the thread class first.
public class ThreadPoolThread
{
public ThreadPoolThread
(ThreadPool iThreadPool, ThreadPool.DoWork iWorkerMethod)
{
mWorkerMethod += iWorkerMethod;
mThreadPool = iThreadPool;
mThread = new Thread (new ThreadStart (ThreadFunc));
mEvent = new AutoResetEvent (false);
mThread.Start ();
}
public void ProcessRequest ()
{
mEvent.Set ();
}
public void Stop ()
{
if (mThread != null)
mThread.Abort ();
}
private void ThreadFunc ()
{
WaitHandle[] lWaitHandle = new WaitHandle[1];
lWaitHandle[0] = mEvent;
while (true){
AutoResetEvent.WaitAll (lWaitHandle);
mWorkerMethod ();
mThreadPool.ReturnToPool (this);
}
}
private ThreadPool.DoWork mWorkerMethod;
private Thread mThread;
private AutoResetEvent mEvent;
private ThreadPool mThreadPool;
}
This class has four methods, a constructor that sets up the objects required by the class, a method to wake up the thread, a method to kill the thread, and the worker method where the thread will spend its entire life. The ThreadPoolThread object consists of one Thread object and one AutoResetEvent object, which is used to signal an idle thread. The constructor takes in a reference to the thread pool that this thread belongs to, and it also takes in a delegate which is the user defined method that this thread will invoke as part of its processing. The user of this class will pass this delegate, and it typically will be a method that will accept a request (from a socket or from a web service, etc) and process it. Once this delegate method returns, the thread will go into an efficient wait, blocking the event object. This is an efficient wait state that consumes minimal resources. The thread can be brought out of this wait state by the thread pool, by calling its ProcessRequest method. This method simply calls the Set method of the event object, which causes the event object to go into a signaled state. This causes the AutoResetEvent.WaitAll method to come out of a blocking state, and invoke the delegate method. This continues in an infinite loop, until the thread is terminated by the thread pool calling the Stop method.
Let us now look at the thread pool class. Note that this is a custom implementation of the thread pool, which is different from the System.Threading.ThreadPool class. We will start off by discussing the Start and the Stop methods.
public delegate void DoWork ();
public ThreadPool (int iNumThreads)
{
mNumThreads = iNumThreads;
}
public void Start (DoWork iWorkerMethod)
{
mFreeList = new ThreadPoolThread [mNumThreads];
mThreads = new ThreadPoolThread [mNumThreads];
mFreeThreads = mNumThreads;
ThreadPoolThread lThread;
for (int i = 0; i < mNumThreads; i++){
lThread = new ThreadPoolThread (this, iWorkerMethod);
mFreeList[i] = mThreads[i] = lThread;
}
}
public void Stop ()
{
for (int i = 0; i < mNumThreads; i++)
mThreads[i].Stop ();
}
private int mNumThreads;
private ThreadPoolThread[] mFreeList;
private ThreadPoolThread[] mThreads;
private int mFreeThreads;
The first thing that the ThreadPool class defines is a delegate that is used by the thread class to call the method that will perform the actual processing, as explained above. The constructor of the thread pool takes in a single parameter which specifies the number of threads that will be created in this pool. The thread pool is started by calling the Start method, which creates a pool of ThreadPoolThread objects, and stores a pointer reference to these objects in the mThreads and mFreeList arrays (we use arrays for simplicity in this sample: a more efficient alternative would be the use of lists). The mThreads array represents all the threads in this pool, and the mFreeList array represents all the threads that are currently free to service requests. All threads start off belonging to the free list. The Stop method simply calls the Stop method on the ThreadPoolThread class that aborts the thread.
Let us now look at the ProcessRequest method that is called by the client when it needs a thread to process a request. Calling this method causes the delegate that was passed to the Start method to be called.
public bool ProcessRequest ()
{
ThreadPoolThread lThread = null;
if (mFreeThreads == 0)
return false;
lock(this)
{
mFreeThreads--;
for (int i = 0; i < mNumThreads; i++)
if (mFreeList[i] != null){
lThread = mFreeList[i];
mFreeList[i] = null;
break;
}
}
lThread.ProcessRequest ();
return true;
·一个简单的CustomItem使用案例
·CustomizeGoogle:让Firefox搜索更上
·用CustomDataSource向iReport中传递数
·User Tips: Using Return Values from
·Custom Channel Sinks被我征服了
·Cisco Unified Customer Voice Portal
·使用CustomValidator模仿show出一个co
·Custom Thread Pooling Template
·Creating Custom Web Controls in C#
前天发现自己2000系统升级不了,提了个question到微软的Support,结果效率还真不差。 贴上解决方案。提问时我就写了个can not update. 看看他们解决流程:接到提问->转到support->给出solution.
Problem title: | can not update. | |
Product: | Microsoft Windows Update (All Languages) | |
Sent on: | 3/20/2006 11:47:36 AM | |
Last modified: | 3/22/2006 11:56:54 AM | |
Description: | Problem Description: can not update. Operating System:Windows2000 |
| |||||
|
更多精彩
赞助商链接