private static Results MatchMultiProcessor(string userAgent, Handler handler) { // Provide an object to signal when the request has completed. AutoResetEvent waiter = new AutoResetEvent(false); // Create the request. Request request = new Request(userAgent, handler, waiter); if (request.Count > 0) { // For each thread add this to the queue. for (int i = 0; i < Environment.ProcessorCount; i++) ThreadPool.QueueUserWorkItem(ServiceRequest, request); // Wait until a signal is received. Keeping coming back to // this thread so that a request to close the request // can be processed. while (waiter.WaitOne(1, false) == false) { // Do nothing } ; } // Return the results. return request.Results; }
private static void ServiceRequest(Request request) { string userAgent = request.UserAgent; DeviceInfo current = request.Next(); while (current != null) { // Perform the edit distance check. int distance = Algorithms.EditDistance(current.UserAgent, userAgent, request.Results.MinDistance); if (distance <= request.Results.MinDistance) { lock (request.Results) { if (distance < request.Results.MinDistance) { request.Results.MinDistance = distance; request.Results.Clear(); request.Results.Add(current); } else if (distance == request.Results.MinDistance) { request.Results.Add(current); } } } current = request.Next(); request.Complete(); } }
private static Results MatchSingleProcessor(string userAgent, Handler handler) { // Create a single matcher and start it. Request request = new Request(userAgent, handler); // Process the request. ServiceRequest(request); // Return the results. return request.Results; }