【问题标题】:problem in asp.net application_start event...its very very slow...what could be the reason?asp.net application_start 事件中的问题...非常非常慢...可能是什么原因?
【发布时间】:2011-01-31 09:43:20
【问题描述】:
 void Application_Start(object sender, EventArgs e)
{
    Thread thread = new Thread(new ThreadStart(createIndex));
    thread.Start();
}
void createIndex()
{
    string constring = "server=localhost;port=3306;database=hr;uid=root;password=root" ;   
    String luceneIndexDirectory = @"C:\Index";

    try 
    {
       SimpleAnalyzer analyzer = new SimpleAnalyzer();
       Directory directory = new SimpleFSDirectory(new File(luceneIndexDirectory));
       IndexWriter iwriter = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED);

       MySqlConnection con = new MySqlConnection(constring);
       MySqlCommand com = new MySqlCommand("select emp_id , emp_resume from emp;", con);
       con.Open();
       MySqlDataReader dr = com.ExecuteReader();
       object objPath = null;
       object missing = System.Reflection.Missing.Value;
       object objTrue = true;
       object objFalse = false;
       Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
       while (dr.Read())
       {
           objPath = Server.MapPath("~") + @"\Cnd_Resume\" + dr.GetString(1);
           if(System.IO.File.Exists(Convert.ToString(objPath)))
           {
               Microsoft.Office.Interop.Word.Document d = wordApp.Documents.Open(ref objPath, ref objFalse, ref objTrue, 
                   ref objFalse, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                   ref objFalse, ref missing, ref missing, ref missing, ref missing);

               Document doc = new Document(); 

               doc.add(new Field("id", dr.GetString(0) , Field.Store.YES , Field.Index.NOT_ANALYZED ));

               doc.add(new Field("name", dr.GetString(1), Field.Store.YES, Field.Index.NOT_ANALYZED));

               doc.add(new Field("contents", d.Content.Text, Field.Store.NO, Field.Index.ANALYZED));

               d.Close(ref missing, ref missing, ref missing);

               iwriter.addDocument(doc); 
           }
       }

       iwriter.optimize(); 
       iwriter.close(); 
    } 
    catch (Exception ex){}

}

【问题讨论】:

  • 您在应用程序启动时到底在做什么?那里真的有必要吗?您是否尝试过使用秒表查看您的任务需要多长时间? msdn.microsoft.com/en-us/library/…
  • 你说的慢是什么意思..程序甚至没有响应或者需要很多时间..
  • 好的,请问你建议我把这段代码也移到哪里????我正在尝试从 ms word docs 创建一个 lucene 索引...

标签: c# .net asp.net lucene


【解决方案1】:

我觉得你可能有两个问题:

  1. Word 不支持线程。简单来说,即使你产生了多个线程,word 本身一次只处理一个文档,有点像单例。一个适当的单例实现确实在多线程环境中实现了这一点。此外,考虑到这一点 - 您在任何给定时间只能编辑一个文档。当然,您可以有多个 Word UI 窗口,但引擎本身(以及您作为用户)一次只能编辑一个文档。

  2. 您的 MySQL 查询执行时间过长(不太可能)。

【讨论】:

    猜你喜欢
    • 2018-05-08
    • 1970-01-01
    • 2010-09-22
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    • 2011-01-20
    • 1970-01-01
    相关资源
    最近更新 更多