在C#里关于定时器类有3个:System.Windows.Forms.Timer类、System.Threading.Timer类和System.Timers.Timer类。 System.Windows.Forms.Timer是应用于WinForm中的,它是通过Windows消息机制实现的,类似于VB或Delphi中的Timer控件,内部使用API SetTimer实现的。它的主要缺点是计时不精确,而且必须有消息循环,Console Application(控制台应用程序)无法使用。 System.Timers.Timer和System.Threading.Timer非常类似,它们是通过.NET Thread Pool实现轻量、精确的计时,对应用程序、消息没有特别的要求。System.Timers.Timer还可以应用于WinForm,完全取代上面的Timer控件。它们的缺点是不支持直接的拖放,需要手工编码。 public int wrong = 0; System.Timers.Timer time = new System.Timers.Timer(); private void begin_Click(object sender, EventArgs e) { if (action.Text == "启动监测") { action.Text = "停止监测"; label2.Text = "已启动"; if (time.Interval.ToString() == "100") // The default value of interval is 100s. { time.Elapsed += new ElapsedEventHandler(TimeEvent); time.Interval = 1000; } time.Enabled = true; } else { action.Text = "启动监测"; label2.Text = "已停止"; time.Enabled = false; } } private static void TimeEvent(object source, ElapsedEventArgs e) { int tsec = e.SignalTime.Second; int isec = 10; if (tsec == isec) //it will be activated at 10s of every minutes. { if (!Check("http://www.test.com")) { string smtp_server="192.168.8.1"; int port = 25; string mail_from = "test_from@163.com"; string sender="test"; string mail_to = "test_to@163.com"; string receiver="adminer"; string subject = "The site is run out exception on " + DateTime.Now.ToString("yyyyMMddhhmmss"); string body = "The site can not open on " + DateTime.Now.ToString() + ",please check it !"; try { SendEmail(smtp_server, port, mail_from, sender, mail_to, receiver, subject, body); } catch(Exception ex) { MessageBox.Show(ex.Message); } } } } private static bool Check(string urlStr) { HttpWebRequest myWebRequest = null; try { myWebRequest = (HttpWebRequest)WebRequest.Create(urlStr); HttpWebResponse res = (HttpWebResponse)myWebRequest.GetResponse(); if (res.StatusCode == HttpStatusCode.OK) { res.Close(); return true; } else { res.Close(); return false; } } catch (Exception) { return false; } } public static void SendEmail(string smtp_server, int port, string mail_from, string sender, string mail_to, string receiver, string subject, string body) { MailAddress from = new MailAddress(mail_from, sender); MailAddress to = new MailAddress(mail_to, receiver); MailMessage message = new MailMessage(from, to); message.BodyEncoding = Encoding.UTF8; message.IsBodyHtml = true; message.Subject = subject; message.Body = body; SmtpClient client = new SmtpClient(smtp_server, port); //SmtpClient client = new SmtpClient(smtp_server); […]
View Details阅读目录 开始 Lucene简介 效果图 Demo文件说明 简单使用 重点类的说明 存在问题 调整后 Lucene.Net博文与资源下载 做过站内搜索的朋友应该对Lucene.Net不陌生,没做过的也许会问:就不是个查询嘛!为什么不能使用Like模糊查找呢? 原因很简单--模糊查询的契合度太低,匹配关键字之间不能含有其他内容。最重要的是它会造成数据库全表扫描,效率底下,即使使用视图,也会造成数据库服务器"亚历山大",那LuceneNet又是一个神马东西?如何使用?以下给出详细的介绍包括Demo 回到顶部 Lucene简介 首先说明的是--Lucene.Net只是一个全文检索开发包,不是一个成型的搜索引擎, 它的功能就是负责将文本数据按照某种分词算法进行切词,分词后的结果存储在索引库中,从索引库检索数据的速度灰常快. 对以上加粗的词汇稍作下阐述: 文本数据:Lucene.Net只能对文本信息进行检索,所以非文本信息要么转换成为文本信息,要么你就死了这条心吧! 分词算法:将一句完整的话分解成若干词汇的算法 常见的一元分词(Lucene.Net内置就是一元分词,效率高,契合度低),二元分词,基于词库的分词算法(契合度高,效率低)… 切词:将一句完整的话,按分词算法切成若干词语 比如:"不是所有痞子都叫一毛" 这句话,如果根据一元分词算法则被切成: 不 是 所 有 痞 子 都 叫 一 毛 如果二元分词算法则切成: 不是 是所 所有 有痞 痞子 子都 都叫 叫一 一毛 如果基于词库的算法有可能:不是 所有 痞子 都叫 一毛 具体看词库 索引库:简单的理解成一个提供了全文检索功能的数据库 如果文字难以理解 见Demo文件说明中的右侧图吧 回到顶部 效果图 首先展示效果图,避免各位观众不知偶所云. 这里有三张图: 图1 简单使用页面效果图 图2 对数据库新增数据后 索引库更新效果图 图3 将图2中的新增数据修改后 索引库更新效果图 回到顶部 Demo文件说明 回到顶部 简单使用 图1中的BookList.aspx 页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="BookList.aspx.cs" Inherits="Web.LuceneNet.BookList" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" method="get" action="BookList.aspx"> <div> 请输入搜索关键字:<input type="text" name="SearchKey" value="" /> <input type="submit" name="btnSearch" value="一哈哈" /> <input type="submit" name="btnCreate" value="创建索引" /> <br /> <ul> <asp:Repeater ID="Repeater1" runat="server"> <ItemTemplate> <li><a href='#'> <%# Eval("Title") %></a></li> <li><span> <%# Eval("ContentDescription") %></span></li> </ItemTemplate> </asp:Repeater> </ul> </div> </form> </body> </html> |
BookList.aspx.cs 后台的处理操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
using System; using System.Collections.Generic; using System.IO; using Lucene.Net.Analysis.PanGu; using Lucene.Net.Documents; using Lucene.Net.Index; using Lucene.Net.Search; using Lucene.Net.Store; using PZYM.Shop.BLL; namespace Web.LuceneNet { public partial class BookList : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string btnCreate = Request.QueryString["btnCreate"]; string btnSearch = Request.QueryString["btnSearch"]; if(!string.IsNullOrEmpty(btnCreate)) { //创建索引库 CreateIndexByData(); } if(!string.IsNullOrEmpty(btnSearch)) { //搜索 SearchFromIndexData(); } } /// <summary> /// 创建索引 /// </summary> private void CreateIndexByData() { string indexPath = Context.Server.MapPath("~/IndexData");//索引文档保存位置 FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NativeFSLockFactory()); //IndexReader:对索引库进行读取的类 bool isExist = IndexReader.IndexExists(directory); //是否存在索引库文件夹以及索引库特征文件 if(isExist) { //如果索引目录被锁定(比如索引过程中程序异常退出或另一进程在操作索引库),则解锁 <strong>//Q:存在问题 如果一个用户正在对索引库写操作 此时是上锁的 而另一个用户过来操作时 将锁解开了 于是产生冲突 --解决方法后续</strong> if(IndexWriter.IsLocked(directory)) { IndexWriter.Unlock(directory); } } //创建向索引库写操作对象 IndexWriter(索引目录,指定使用盘古分词进行切词,最大写入长度限制) //补充:使用IndexWriter打开directory时会自动对索引库文件上锁 IndexWriter writer = new IndexWriter(directory, new PanGuAnalyzer(), !isExist, IndexWriter.MaxFieldLength.UNLIMITED); BooksManager bookManager = new BooksManager(); List<PZYM.Shop.Model.Books> bookList = bookManager.GetModelList(""); //--------------------------------遍历数据源 将数据转换成为文档对象 存入索引库 foreach(var book in bookList) { Document document = new Document(); //new一篇文档对象 --一条记录对应索引库中的一个文档 //向文档中添加字段 Add(字段,值,是否保存字段原始值,是否针对该列创建索引) document.Add(new Field("id", book.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));//--所有字段的值都将以字符串类型保存 因为索引库只存储字符串类型数据 //Field.Store:表示是否保存字段原值。指定Field.Store.YES的字段在检索时才能用document.Get取出原值 //Field.Index.NOT_ANALYZED:指定不按照分词后的结果保存--是否按分词后结果保存取决于是否对该列内容进行模糊查询 document.Add(new Field("title", book.Title, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS)); //Field.Index.ANALYZED:指定文章内容按照分词后结果保存 否则无法实现后续的模糊查询 //WITH_POSITIONS_OFFSETS:指示不仅保存分割后的词 还保存词之间的距离 document.Add(new Field("content", book.ContentDescription, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS)); writer.AddDocument(document); //文档写入索引库 } writer.Close();//会自动解锁 directory.Close(); //不要忘了Close,否则索引结果搜不到 } /// <summary> /// 从索引库中检索关键字 /// </summary> private void SearchFromIndexData() { string indexPath = Context.Server.MapPath("~/IndexData"); FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NoLockFactory()); IndexReader reader = IndexReader.Open(directory, true); IndexSearcher searcher = new IndexSearcher(reader); //搜索条件 PhraseQuery query = new PhraseQuery(); //把用户输入的关键字进行分词 foreach(string word in Common.SplitContent.SplitWords(Request.QueryString["SearchKey"])) { query.Add(new Term("content", word)); } //query.Add(new Term("content", "C#"));//多个查询条件时 为且的关系 query.SetSlop(100); //指定关键词相隔最大距离 //TopScoreDocCollector盛放查询结果的容器 TopScoreDocCollector collector = TopScoreDocCollector.create(1000, true); searcher.Search(query, null, collector);//根据query查询条件进行查询,查询结果放入collector容器 //TopDocs 指定0到GetTotalHits() 即所有查询结果中的文档 如果TopDocs(20,10)则意味着获取第20-30之间文档内容 达到分页的效果 ScoreDoc[] docs = collector.TopDocs(0, collector.GetTotalHits()).scoreDocs; //展示数据实体对象集合 List<PZYM.Shop.Model.Books> bookResult = new List<PZYM.Shop.Model.Books>(); for(int i = 0; i < docs.Length; i++) { int docId = docs[i].doc;//得到查询结果文档的id(Lucene内部分配的id) Document doc = searcher.Doc(docId);//根据文档id来获得文档对象Document PZYM.Shop.Model.Books book = new PZYM.Shop.Model.Books(); book.Title = doc.Get("title"); //book.ContentDescription = doc.Get("content");//未使用高亮 //搜索关键字高亮显示 使用盘古提供高亮插件 book.ContentDescription = Common.SplitContent.HightLight(Request.QueryString["SearchKey"], doc.Get("content")); book.Id = Convert.ToInt32(doc.Get("id")); bookResult.Add(book); } Repeater1.DataSource = bookResult; Repeater1.DataBind(); } } } |
使用的分词方法与关键字变红 SplitContent.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
using System.Collections.Generic; using System.IO; using Lucene.Net.Analysis; using Lucene.Net.Analysis.PanGu; using PanGu; namespace Web.Common { public class SplitContent { public static string[] SplitWords(string content) { List<string> strList = new List<string>(); Analyzer analyzer = new PanGuAnalyzer();//指定使用盘古 PanGuAnalyzer 分词算法 TokenStream tokenStream = analyzer.TokenStream("", new StringReader(content)); Lucene.Net.Analysis.Token token = null; while((token = tokenStream.Next()) != null) { //Next继续分词 直至返回null strList.Add(token.TermText()); //得到分词后结果 } return strList.ToArray(); } //需要添加PanGu.HighLight.dll的引用 /// <summary> /// 搜索结果高亮显示 /// </summary> /// <param name="keyword"> 关键字 </param> /// <param name="content"> 搜索结果 </param> /// <returns> 高亮后结果 </returns> public static string HightLight(string keyword, string content) { //创建HTMLFormatter,参数为高亮单词的前后缀 PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font style=\"font-style:normal;color:#cc0000;\"><b>", "</b></font>"); //创建 Highlighter ,输入HTMLFormatter 和 盘古分词对象Semgent PanGu.HighLight.Highlighter highlighter = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new Segment()); //设置每个摘要段的字符数 highlighter.FragmentSize = 1000; //获取最匹配的摘要段 return highlighter.GetBestFragment(keyword, content); } } } |
回到顶部 重点类的说明 Analyzer类:LuceneNet中分词算法的基类 任何自定义算法都需继承它 FSDirectory类: 指定索引库文件存放文件位置 是Directory的子类(它有两个子类 还有一个RAMDirecory,它用来指定将索引库文件存放在内存中) IndexReader:对索引进行读取的类 静态方法bool IndexExists(Directory […]
View Details刚安装完VS2010,不仅启动速度慢,编写代码什么的都慢,卡死!!! 1.禁止VS2010显示启动动画 右键VS2010快捷方式,选择【属性】,在【目标】的后面加上:/nosplash(在exe后面加一个空格再写/nosplash),截图如下: 2.让VS2010启动后,显示空环境。 在VS2010中,打开【工具】—-〉〉〉【选项】—-〉〉〉【环境】—-〉〉〉【启动】,在【启动时】选择【显示空环境】,截图如下: 3.禁止VS2010在启动时,启动外部程序。 在VS2010中,选择【工具】—-〉〉〉【外接程序管理器】,截图如下: 在外接程序管理器中,去掉所有外接程序前面的钩,然后确定即可。截图如下: 4、去掉VS2010视觉体验 老样子,在VS2010中,选择【工具】—-〉〉〉【选项】—-〉〉〉【环境】—-〉〉〉【常规】,在右边找到【视觉体验】, 把视觉体验下面所有的钩给去掉即可。截图如下: 5.禁用【IntelliTrace】 在VS2010中,选择【工具】—-〉〉〉【选项】—-〉〉〉【IntelliTrace】,在右边找到【启用IntelliTrace】,然后去掉其前面的钩 即可。截图如下: 6.在vs2010命令提示符下,执行devenv.exe /resetuserdata //这个方法我没有试过 参考文章:http://blog.csdn.net/kupepoem/article/details/6584667 7.禁用VS2010智能提示,即代码提示功能了,如果你安装了VA助手,这个智能提示是可以关闭的。 如果你没有安装VA,建议还是保留该功能吧。 在VS2010中,选择【工具】—-〉〉〉【选项】—-〉〉〉【文本编辑器】—-〉〉〉【C/C++】—-〉〉〉【高级】,在右边找到 【禁用IntelliSense】,选择True即可。截图如下: from:http://blog.163.com/pinbo_jiankun/blog/static/1335464882013102105055396/
View Details1. SB Admin 2 Details & Download 2. Admin Lite Details & Download 3. Director Responsive Admin Template Free Details & Download 4. Free Bootstrap Admin Template Dream Details & Download 5. Dashgum – Free Dashboard Details & Download 6. Free Responsive Admin Template – Zontal Admin Details & Download 7. Free Download Bootstrap Admin Template Details & Download 8. Janux – Free Responsive Admin Dashboard Template Details & Download 9. Joli – Free Responsive Bootstrap Admin Dashboard Template Details & Download 10. KAdmin – Free Responsive Admin […]
View Details