parallelSearch.cs
资源名称:08.zip [点击查看]
上传用户:ynjin1970
上传日期:2014-10-13
资源大小:6438k
文件大小:4k
源码类别:
中间件编程
开发平台:
Visual C++
- using System;
- using System.Threading ;
- using System.Collections;
- namespace WebApplication1
- {
- /// <summary>
- /// 并行搜索的类
- /// </summary>
- public class parallelSearch
- {
- private ArrayList _results= null;
- private string _keyword;
- private DateTime _started;
- private DateTime _end;
- public parallelSearch()
- {
- //
- // TODO: 在此处添加构造函数逻辑
- //
- }
- private void addResult(result thisResult)
- {
- lock(_results)
- {_results.Add(thisResult);
- }
- }
- public TimeSpan totalTime
- {
- get { return _end.Subtract(_started); }
- }
- public ArrayList search(string keyword )
- {
- if (keyword==null && keyword=="")
- return null;
- _started=DateTime.Now;
- _keyword=keyword;
- _results=new ArrayList();
- Thread ps1=new Thread (new ThreadStart (searchWebSites));
- Thread ps2=new Thread (new ThreadStart (searchDatabases));
- Thread ps3=new Thread (new ThreadStart (searchDirectoryServers));
- ps1.Start ();
- ps2.Start ();
- ps3.Start ();
- ps1.Join ();
- ps2.Join ();
- ps3.Join ();
- _end=DateTime.Now;
- return _results;
- }
- private void searchDatabases()
- {
- result r = new result("符合查询关键字的记录:""+_keyword+""来自数据库服务器中");
- try
- {
- Thread.Sleep(1000);
- r.links.Add( new link("书名- 《你今天心情不好吗?》","HHFDESKTOP"));
- Thread.Sleep(1000);
- r.links.Add( new link("书名 - 《中国机械设计大典》","HHFDESKTOP"));
- Thread.Sleep(1000);
- r.links.Add( new link("书名 - 《甲骨文献集成》","HHFDESKTOP"));
- r.message="查询结果:" + r.links.Count+ "项";
- r.isCompleted =true;
- }
- catch(ThreadAbortException tae)
- {
- r.isCompleted =false;
- r.message ="查询结果:" + r.links.Count+ "项"+"在操作取消之前";
- }
- catch(Exception e)
- {
- r.isCompleted=false;
- r.message ="查询结果:" + r.links.Count+ "项"+"在操作出错之前";
- }
- finally
- {
- addResult(r);
- }
- }
- private void searchDirectoryServers()
- {
- result r = new result("符合查询关键字的记录:""+_keyword+""来自目录服务器中");
- try
- {
- Thread.Sleep(1000);
- r.links.Add( new link("员工 -书名 张大宝","/"));
- r.links.Add( new link("员工 - 书名 刘爱山","/"));
- r.message="查询结果::" + r.links.Count+ "项";
- r.isCompleted =true;
- }
- catch(ThreadAbortException tae)
- {
- r.isCompleted =false;
- r.message ="查询结果:" + r.links.Count+ "项"+"在操作取消之前";
- }
- catch(Exception e)
- {
- r.isCompleted=false;
- r.message ="查询结果:" + r.links.Count+ "项"+"在操作出错之前";
- }
- finally
- {
- addResult(r);
- }
- }
- private void searchWebSites()
- {
- result r = new result("符合查询关键字的记录:""+_keyword+""来自WEB服务器中");
- try
- {
- Thread.Sleep(2000);
- r.links.Add( new link("简体中文网页-书名 《国王和国后》","HTTP://WWW.163.COM"));
- Thread.Sleep(1000);
- r.links.Add( new link("简体中文网页 -书名 《坐井之小青蛙》","HTTP://WWW.163.COM"));
- Thread.Sleep(1000);
- r.links.Add( new link("简体中文网页 -书名 《激情燃烧的岁月》","HTTP://WWW.163.COM"));
- Thread.Sleep(1000);
- r.links.Add( new link("简体中文网页 -书名《 林海雪原》","HTTP://WWW.163.COM"));
- r.message="查询结果:" + r.links.Count+ "项";
- r.isCompleted =true;
- }
- catch(ThreadAbortException tae)
- {
- r.isCompleted =false;
- r.message ="查询结果:" + r.links.Count+ "项"+"在操作取消之前";
- }
- catch(Exception e)
- {
- r.isCompleted=false;
- r.message ="查询结果:" + r.links.Count+ "项"+"在操作出错之前";
- }
- finally
- {
- addResult(r);
- }
- }
- }
- }