asp.net application_start事件中的问题......它非常慢......可能是什么原因?

时间:2011-01-31 09:43:21

标签: c# .net asp.net lucene

 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){}

}

1 个答案:

答案 0 :(得分:0)

我认为你可能有两个问题:

  1. Word不是线程感知的。简单来说,即使您生成多个线程,单词本身也只能一次处理一个文档,有点像单例。适当的单例实现确实在多线程环境中实现了这一点。而且,当您考虑它时 - 您只能在任何给定时间编辑一个文档。当然,您可以拥有多个Word UI窗口,但引擎本身(以及您作为用户)一次只能编辑一个文档。

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