如何提高以下功能的性能? [英] How can I improve the performance speed of below function?

查看:70
本文介绍了如何提高以下功能的性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void btnUpload_Click(object sender, EventArgs e)
       {
           ResetStatus();

           for (int i = 1; i <= 10; i++)
           {
               string file_control = "FileUpload" + i.ToString();
               string link_button_control = "LinkButton" + i.ToString();
               FileUpload fileUpload = (FileUpload)this.Master.FindControl("MainContent").FindControl(file_control);
               LinkButton linkButton = (LinkButton)this.Master.FindControl("MainContent").FindControl(link_button_control);
               string label_file_name_control = "LabelFileName" + i.ToString();
               Label label_file_name = (Label)this.Master.FindControl("MainContent").FindControl(label_file_name_control);

               if (fileUpload.PostedFile.FileName != "")
               {
                   string f_type = Functions.CheckFileType(fileUpload.PostedFile.FileName);
                   string file_name = fileUpload.PostedFile.FileName;
                   if (f_type == "xls" && f_type != "xlsx")
                   {
                       error += "\nFile number " + i.ToString() + " " + file_name + " should be in Excel format!";
                       linkButton.Visible = true;
                       linkButton.Text = "Error found, click to show error";
                   }
                   else
                   {
                       DateTime rightnow = DateTime.Now;
                       string user_name = SharedFunctionV3.UserInfo.GetUserInfofromAD(Session["UserID"].ToString()).DisplayName;
                       string new_file_name = Session["UserID"].ToString() + rightnow.ToString("yyyyMMddhhmmss") + "ForecastDataUpload" + i.ToString() + ".xls";
                       string f_name = Server.MapPath(".") + "\\upload\\" + new_file_name;
                       fileUpload.PostedFile.SaveAs(f_name);
                       label_file_name.Text = f_name;
                       if (CheckFile(f_name, i) == 0)//edited
                       {
                           linkButton.Visible = true;
                           linkButton.Text = "Error found, click to show error";
                       }
                       else
                       {
                           UploadRecord(f_name, i);
                       }
                   }
               }
           }

           tbError.Text = error;
       }





我尝试过:



假设如果循环需要10轮,我怎么能为10次所有的UploadRecord函数调用一次,这样可以减少上传批量加载所需的时间



What I have tried:

Suppose if the loop takes 10 rounds how can i make one call for UploadRecord function for all the 10 times so that it will reduce the time it takes to upload bulk load

推荐答案

尝试使用for的并行版本。如果从中获得性能取决于许多事情,但这可能是最容易尝试的事情。谷歌c#parallel for了解详情和例子。
Try using the parallel version of "for". If you gain performance from it will depends on a number of things, but it's probably the easiest thing to try. Google "c# parallel for" for details and examples.


这篇关于如何提高以下功能的性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆