示例#1
0
        public string resolve()
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            string param = System.Web.HttpUtility.UrlDecode(HttpContext.Current.Request.Url.Query.ToString().Substring(1));
            string[] param_s = param.Split('&'); Hashtable key_values = new System.Collections.Hashtable(); ;
            foreach (string item in param_s)
            {
                string[] key_value = item.Split('=');
                //  this.GetType().GetField(key_value[0]).GetValue(key_value[1]).ToString();
                key_values.Add(key_value[0], key_value[1]);
            }

            String LocalPath = null;
            string column = key_values.ContainsKey("column") ? key_values["column"].ToString() : "", type = key_values.ContainsKey("type") ? key_values["type"].ToString() : "", doc_url = key_values.ContainsKey("doc_url") ? key_values["doc_url"].ToString() : "";
            if (column.Equals("") || type.Equals("") || doc_url.Equals(""))
                throw new Exception("输入参数不合法");
            //  return column;

            string message = null;
            try
            {
                String savePath = downfile(doc_url);
                //  string pdfpath = showwordfiles(savePath);
                //不需要此步判断了 if (!File.Exists(savePath)) throw new Exception("保存文件失败" );

                in_column = column;
                //WORD  中数据都规整为一个空格隔开来
                columns = column.Split(',');
                object fileName = savePath;

                object unknow = System.Type.Missing;

                //目前一个线程再跑
                if (type == "rs") { delet_tables(savePath); }

                //count the paragraphs

                if (type == "rs")
                {
                    //然后完成对文档的解析

                    List<System.Threading.Tasks.Task> TaskList = new List<System.Threading.Tasks.Task>();
                    // 开启线程池,线程分配算法
                    System.Threading.Tasks.Task t = null;

                    int k = (int)Math.Ceiling((Double)slice / doc_handler);
                    for (int i = 0; i < doc_handler; i++)
                    {
                        Microsoft.Office.Interop.Word.Application app_in = new Microsoft.Office.Interop.Word.Application();
                        var doc1 = app_in.Documents.Open(ref fileName, ref unknow, true, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow,
                        ref unknow, ref unknow, ref unknow, ref unknow, ref unknow);
                        pcount = doc1.Paragraphs.Count;
                        docs_list[i] = (doc1);
                        apps_list[i] = (app_in);

                        int block = (int)Math.Ceiling((Double)pcount / slice);
                        for (int j = i * k + 1; j <= (i + 1) * k && j <= slice; j++)
                        {

                            //Debug.WriteLine("传入{0},{1}", pcount * (i) / slice + 1 - key_line, pcount * (i + 1) / slice + key_line);
                            int start_in = block * (j - 1) + 1 - key_line <= 0 ? 1 : block * (j - 1) + 1 - key_line, end_in = block * (j) + key_line >= pcount ? pcount : block * (j) + key_line;
                            Debug.WriteLine("outside{0}=>{1},{2}", doc1.GetHashCode(), start_in, end_in);
                            t = new System.Threading.Tasks.Task(() => thread1(doc1, start_in, end_in));//, ref aaa.finals[i]));
                            t.Start();
                            TaskList.Add(t);
                        }
                    }

                    /*
                         var t1 = new System.Threading.Tasks.Task(() => thread1(1,pcount/8+key_line, map[0]));
                         t8.Start(); TaskList.Add(t8);

                 */

                    System.Threading.Tasks.Task.WaitAll(TaskList.ToArray());//t1, t2, t3, t4, t5, t6, t7, t8);
                    var json = new JavaScriptSerializer().Serialize(aaa.finalstrings.Where((x, i) => aaa.finalstrings.FindIndex(z => z["tag"] == x["tag"]) == i).ToList());
                    message = json;// "{\"success\":true,\"msg\":" + (new JavaScriptSerializer().Serialize(json)) + "}";

                }//rs
                else if (type == "tc")
                {

                    //tc并发并没有什么问题

                    List<System.Threading.Tasks.Task> TaskList = new List<System.Threading.Tasks.Task>();

                    int k = (int)Math.Ceiling((Double)slice / doc_handler);
                    Debug.WriteLine("buchang {0}", k);
                    for (int i = 0; i < doc_handler; i++)
                    {
                        Microsoft.Office.Interop.Word.Application app_in = new Microsoft.Office.Interop.Word.Application();
                        var doc1 = app_in.Documents.Open(ref fileName, ref unknow, true, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow,
                        ref unknow, ref unknow, ref unknow, ref unknow, ref unknow);
                        pcount = doc1.Tables.Count;
                        docs_list[i] = (doc1);
                        apps_list[i] = (app_in);
                        int block = (int)Math.Ceiling((Double)pcount / slice);

                        for (int j = i * k + 1; j <= (i + 1) * k && j <= slice; j++)
                        {
                            //Debug.WriteLine("传入{0},{1}", pcount * (i) / slice + 1 - key_line, pcount * (i + 1) / slice + key_line);

                            int start_in = block * (j - 1) + 1 <= 0 ? 1 : block * (j - 1) + 1, end_in = block * (j) >= pcount ? pcount : block * (j);
                            Debug.WriteLine("outside {0},{1}", start_in, end_in);
                            System.Threading.Tasks.Task t = new System.Threading.Tasks.Task(() => readtc(doc1, start_in, end_in));
                            t.Start();
                            TaskList.Add(t);
                        }
                    }

                    System.Threading.Tasks.Task.WaitAll(TaskList.ToArray());
                    var json = new JavaScriptSerializer().Serialize(aaa.final_tc);
                    //(new HashSet<Dictionary<string, object>>(aaa.final_tc)));
                    message = json;// "{\"success\":true,\"msg\":" + (new JavaScriptSerializer().Serialize(json)) + "}";

                }

            }

            catch (Exception e)
            {

                message = e.ToString();//
                // "{\"success\":false,\"msg\":\"" + e.Message + e.StackTrace + e.TargetSite + "\"}";

                return message;

            }

            finally
            {
                stopwatch.Stop();

                //  return  json;

                //异不异常到最后都关闭文档,避免word一直处于打开状态占用资源
                object unknows = System.Type.Missing;
                Debug.WriteLine("打开大小为{0}", docs_list.Length);
                //   if (doc != null) doc.Close();
                for (int i = 0; i < docs_list.Length; i++)
                {
                    if (docs_list[i] == null) { break; } Debug.WriteLine("开始close"); docs_list[i].Close(ref unknows, ref unknows, ref unknows); Debug.WriteLine("结束close");
                }
                for (int i = 0; i < apps_list.Length; i++)
                {

                    if (apps_list[i] == null) { break; } Debug.WriteLine("开始quit"); apps_list[i].Quit(ref unknows, ref unknows, ref unknows); Debug.WriteLine("结束quit");
                }

                GC.Collect();
                GC.Collect();
                Context.Response.ContentType = "text/json";
                // Context.Response.Write(stopwatch.Elapsed);
                Context.Response.Write(message);
                // Context.Response.Write(json);

                Context.Response.End();

                /*     app1.Quit(ref unknow, ref unknow, ref unknow);

                 */

            }//finally
            return null;//不是正常请求方式,不可见这结果
        }