简体   繁体   中英

problem in asp.net application_start event…its very very slow…what could be the reason?

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

}

I think you may have two problems:

  1. Word is not thread aware. In simple terms, even though you spawn multiple threads, word itself only processes one document at a time, kind of like a singleton. A proper singleton implementation does achieve this in a multi-threaded environment. Moreover, when you think about it - you can only edit one document at any given time. Sure, you can have multiple Word UI windows, but the engine itself (and you as a user) can only edit one document at a time.

  2. Your MySQL query is taking too long to execute (unlikely).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM