protected void Button1_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {

                CustomTask customTask = new CustomTask();
                customTask.key = key;
                customTask.status = "uploading ...";
                Queue task_queue = (Queue)Application["task_queue"];
                customTask.percent = 0;
                WriteLog(customTask.key, customTask);
                string fileName = this.FileUpload1.FileName;
                string fileExtensionApplication = System.IO.Path.GetExtension(fileName);
                string newFileName = Guid.NewGuid().ToString() + fileExtensionApplication;
                string mapPath = HttpContext.Current.Request.MapPath("~/tmp/");
                string newFilePath = mapPath + newFileName;
                this.FileUpload1.SaveAs(newFilePath);
                customTask.url = "tmp/" + newFileName;
                Application.Lock();
                customTask.status = "waiting ...";
                task_queue = (Queue)Application["task_queue"];
                Object running_task = Application["running_task"];
                if (running_task != null)
                {
                    customTask.waitQueue = 1;
                }
                customTask.waitQueue += task_queue.Count;
                customTask.percent = 0;
                task_queue.Enqueue(customTask);
                Cache.Insert(key, customTask);
                Application.UnLock();
                WriteLog(customTask.key, customTask);

            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            string time = Request.QueryString["t"];

            key = Request.QueryString["guid"];
            if (String.IsNullOrEmpty(key))
            {
                key = Guid.NewGuid().ToString();
            }
            string statusText = getStatus(key);

            if (time != null)
            {
                CustomTask customTask = (CustomTask)Cache.Get(key);
                if (statusText == "complete")
                {
                    Response.Clear();
                    Response.Write("<a href='" + customTask.url + "'>download result</a>");
                    Response.End();

                    Cache.Remove(key);
                }
                else
                {
                    string customJson = js.Serialize(customTask);
                    Response.Clear();
                    Response.Write(customJson);
                    Response.End();
                }
            }
            else
            {
                guid.Text = key;
                if (System.IO.File.Exists(Server.MapPath(key + ".txt")))
                {
                    System.IO.File.Delete(Server.MapPath(key + ".txt"));
                }
            }
        }
        private void runTask()
        {
            string basedir = AppDomain.CurrentDomain.BaseDirectory;
            string mapPath = basedir + "/tmp/";
//            string mapPath = HttpContext.Current.Server.MapPath("~/tmp/");
            REngine engine          = Utils.getREngineInstance();
            string  primer3path     = System.Web.Configuration.WebConfigurationManager.AppSettings["primer3Home"];
            string  processNum      = System.Web.Configuration.WebConfigurationManager.AppSettings["processNum"];
            string  isDeleteTempDir = System.Web.Configuration.WebConfigurationManager.AppSettings["deleteTempDir"];

            while (true)
            {
                if (task_queue.Count != 0)
                {
                    Application.Lock();
                    CustomTask customTask = (CustomTask)task_queue.Dequeue();
                    Application.UnLock();
                    customTask.waitQueue = 0;
                    Object[] task_queue_array = task_queue.ToArray();
                    for (int i = 0; i < task_queue_array.Length; i++)
                    {
                        CustomTask tmpTask = (CustomTask)task_queue_array[i];
                        tmpTask.waitQueue = i + 1;
                    }
                    Application["running_task"] = customTask;
                    string fileName = customTask.url;
                    fileName = basedir + "/" + customTask.url;
                    DataTable dt = read_primer_sequence(fileName);
                    string[,] primerMat = getPrimerMat(dt);
                    customTask.status   = "preparing ...";
                    WriteLog(customTask.key, customTask);
                    CharacterMatrix primer    = engine.CreateCharacterMatrix(primerMat);
                    string          rand_file = System.IO.Path.GetRandomFileName();
                    string          tmp_path  = mapPath + rand_file;
                    //string primer3path = "D:/Install/primer3-win-bin-2.3.6";

                    if (Directory.Exists(tmp_path))
                    {
                        DirectoryInfo di = new DirectoryInfo(tmp_path);
                        di.Delete(true);
                    }
                    else if (File.Exists(tmp_path))
                    {
                        FileInfo fi = new FileInfo(tmp_path);
                        fi.Delete();
                    }
                    Directory.CreateDirectory(tmp_path);
                    engine.Evaluate("library(xlsx)");
                    customTask.percent = 8;
                    string script_path = basedir + "/primer_dimer_check.R";
                    script_path = script_path.Replace(@"\", @"/");
                    engine.Evaluate("source(\"" + script_path + "\")");
                    customTask.percent = 10;
                    engine.SetSymbol("primer", primer);
                    engine.SetSymbol("tmp_dir", engine.CreateCharacter(tmp_path));
                    engine.SetSymbol("primer", primer);
                    engine.SetSymbol("primer3dir", engine.CreateCharacter(primer3path));
                    int?nProcess = Convert.ToInt32(processNum);
                    if (nProcess != null)
                    {
                        engine.SetSymbol("nprocess", engine.CreateInteger(Convert.ToInt32(nProcess)));
                    }
                    else
                    {
                        engine.SetSymbol("nprocess", engine.CreateInteger(4));
                    }
                    engine.SetSymbol("outputfile", engine.CreateCharacter(fileName));
                    string[] bat_cmds = engine.Evaluate("prepare_bat(tmp_dir,primer,primer3dir,nprocess)").AsCharacter().ToArray();
                    customTask.status  = "dimer calculating ...";
                    customTask.percent = 20;
                    WriteLog(customTask.key, customTask);
                    AutoResetEvent[] resets = new AutoResetEvent[bat_cmds.Length];

                    for (int i = 0; i < bat_cmds.Length; i++)
                    {
                        resets[i] = new AutoResetEvent(false);
                        Global.ThreadTransfer transfer = new Global.ThreadTransfer(bat_cmds[i], resets[i]);
                        Thread thread = new Thread(new ParameterizedThreadStart(run_cmd));
                        thread.Start(transfer);
                    }
                    foreach (var v in resets)
                    {
                        v.WaitOne();
                        customTask.percent += 60 / resets.Length;
                    }
                    customTask.status  = "result generating ...";
                    customTask.percent = 80;
                    WriteLog(customTask.key, customTask);
                    engine.Evaluate("output_result(tmp_dir,primer,outputfile)");
                    if (isDeleteTempDir == "true")
                    {
                        DirectoryInfo di = new DirectoryInfo(tmp_path);
                        di.Delete(true);
                    }
                    customTask.status  = "complete";
                    customTask.percent = 100;
                    WriteLog(customTask.key, customTask);


                    Application["running_task"] = null;
                }
            }
        }
        private void SaveData(CustomTask customTask)
        {
            string key = customTask.key;

            WriteLog(key, customTask);
        }
 private void WriteLog(string key, CustomTask customTask)
 {
     string basedir = AppDomain.CurrentDomain.BaseDirectory;
     string log_dir = basedir + "/logs";
     if (!Directory.Exists(log_dir))
     {
         Directory.CreateDirectory(log_dir);
     }
     System.IO.StreamWriter sw = new System.IO.StreamWriter(log_dir + "/" + key + ".txt", true);
     sw.WriteLine("Status = " + customTask.status + " " + System.DateTime.Now.ToString());
     sw.Close();
 }
 private void SaveData(CustomTask customTask)
 {
     string key = customTask.key;
     WriteLog(key,customTask);
 }