static WaitHandle ProcessJob(SnippetGatherState state)
 {
     try
     {
         maxConcurrentJobControl.WaitOne();
         IAsyncResult result = s.BeginSearch(state.request, state.internalCallback, state);
         return result.AsyncWaitHandle;
     }
     catch (Exception e)
     {
         //TraceEventType severity = (e is WebException) ? TraceEventType.Warning : TraceEventType.Error;
         //state.error = new KspApiException(ErrorCode.Network.NetworkError, e.Message, e, severity);
         //state.userCallback(state);
         //maxConcurrentJobControl.Release();
         //return null;
         throw;
     }
 }
 static void AbortJob(SnippetGatherState state)
 {
     if (state.request != null)
     {
         lock (state)
         {
             if (state.request != null)
             {
                 s.CancelAsync(state.UserState);
                 state.request = null;
             }
         }
     }
 }
        public void AsyncSearch(SearchRequest request, WaitCallback callback, object userState)
        {
            SearchResponse cacheRespoinse = GetFromCache(request.Query);
            if (cacheRespoinse != null)
            {
                SnippetGatherState sgs = new SnippetGatherState();
                sgs.result = cacheRespoinse;
                sgs.userState = userState;

                ThreadPool.QueueUserWorkItem(callback, sgs);

                return;
            }

            request.AppId = appID;

            SnippetGatherState state = new SnippetGatherState();
            state.request = request;
            state.userState = userState;
            state.userCallback = callback;
            state.internalCallback = SnippetGatherCallback;

            jobQueue.EnqueueJob(state);
        }