Application of the method multithreading .net background polling request with the timing ajax

The method focuses on the case are: long processing times background data, the request reception time when the return value is not received back, it will lose the connection. Ajax poll timer can be used to request data from the background.

Multithreading background refers to the computationally intensive processing section into the sub-thread, the main thread still running, and returns the result (without waiting for the results of this child thread finish, obtained in response only to let the front desk).

 

Reception Polling part:

  var Getting = { 
            type: "the Get" , 
            URL: "XXX / XXX" , 
            Data: {}, 
            Success: function (Data: the any) {                              
                 IF (data.length == 0 ) {                                                           
                    // Here the return value for the background null data to indicate that a sub-thread finish 
                    the setTimeout ( function () {$ .ajax (Getting);}, 10000); // time to send the request after 10 seconds 
                } 
                 the else {
                    //Here is a background thread finish processing sub                                                                                                                                                
                             
                } 
            } 
        } 

        $ .ajax (Getting);
View Code

 

Multi-threaded background:

 

 //主线程
        [HttpPost("[action]")]
        public List<ResultFilters> ExonCompare(string sampleid,string gene,string enstId,int select_page=1,int tab_page=0)
        {           
            //本地测试代码
            List<string> inputSeq = new List<string>();
            var path = @"d:\_txt";
            using (var sr = new StreamReader(new FileStream(path, FileMode.Open)))
            {
                var line = sr.ReadLine();
                var a = line.Split("\t");
                line = sr.ReadLine();
                while (line != null && line != "")
                {
                    var arr = line.Split('\t');
                    inputSeq.Add(arr[7]);
                    line = sr.ReadLine();
                }
            }

            List<ThreadParam> paramList = new List<ThreadParam>();
            ThreadParam threadParam = new ThreadParam();
            threadParam.ListSeqPath = sampleid + "_" + gene + "_" + enstId + "_" + select_page + "_" + tab_page+"_";
            threadParam.InputSeq = inputSeq[tab_page];           
            paramList.Add(threadParam);
            List<ResultFilters> rfs = new List<ResultFilters>();
            ParameterizedThreadStart threadList = new ParameterizedThreadStart(OtherObject);
            Thread t = new Thread(threadList);                
            t.Start(paramList);                   
            return rfs;
        }
 public  void otherObject ( object paramObj) 
        { 
 // paramObj only in the sub-thread object to pass parameters to the object, the object may comprise a plurality of parameters 
            List <threadparam> parm = (List <threadparam> ) paramObj;
             var InputSeq parm = [ 0 ] .InputSeq;
             var listSeqPath parm = [ 0 ] .ListSeqPath;         
                                                 
            the try {
                // Processed to an interface where a child thread 
                var List = _IExonsLibService.RNACompare_E (InputSeq, 0.98f ) .ToList (); 
                List <ResultFilters> RFS = new new List <ResultFilters> ();
                IF (list.Count == 0 ) { 
                    ResultFilters resultFilters = new new ResultFilters (); 
                    resultFilters.FullNameParmPath = listSeqPath; 
                    resultFilters.ResultInfo = " No matching sequence " ; 
                    rfs.Add (resultFilters); 
                    CallBackExonanalysis (RFS);   // by delegate back to the callback data                    
                }                 
                List <ExonViewModel> Result = new new List <ExonViewModel> ();
                 the foreach (var item in list)
                {
                    ExonViewModel info = new ExonViewModel();
                    info.ResultSeq = item.GetElement(1).ToString().Substring(6);
                    info._id = ObjectId.Parse(item.GetElement(0).ToString().Substring(4));
                    var findseq = _IExonsLibService.FindSeq(info._id);
                    var findinfo = _IExonsLibService.FindExonInfo(findseq.InfoId);
                    info.FindSeq = findseq.Seq;
                    info.Info_id = findinfo._id;
                    info.StartExonPos = int.Parse(findinfo.StartCoordinate);
                    info.EndExonPos = int.Parse(findinfo.EndCoordinate);
                    info.GeneId = findinfo.GeneID;
                    info.ExonId = findinfo.ExonID;
                    info.InputSeq = inputSeq;
                    int startComPos = info.ResultSeq.IndexOf("=");
                    info.StartComPos = startComPos;
                    int endComPos = info.ResultSeq.LastIndexOf("=");
                    info.EndComPos = endComPos;
                    //char[] usefulSeq = info.ResultSeq.Substring(startComPos, endComPos - startComPos + 1).ToCharArray();
                    info.EqualsCount = info.EndComPos - info.StartComPos + 1;
                    result.Add(info);
                }              
                var AN = result.OrderBy(p => p.StartComPos).ToList();
                List<List<FilterViewModel>> unios = new List<List<FilterViewModel>>();
                for (int i = 0; i < AN.Count;)
                {

                    List<FilterViewModel> couple = new List<FilterViewModel>();
                    FilterViewModel filter = new FilterViewModel();

                    filter.Seq = AN[i].FindSeq;
                    filter.StartExonPos = AN[i].StartExonPos;
                    filter.EndExonPos = AN[i].EndExonPos;
                    filter.GeneId = AN[i].GeneId;
                    filter.ExonId = AN[i].ExonId;
                    filter.EqualsCount = AN[i].EqualsCount;
                    couple.Add(filter);                   
                    int k = i;
                    for (int j = i + 1; j < AN.Count; j++)
                    {
                        if (AN[k].EndComPos < AN[j].StartComPos)
                        {
                            FilterViewModel filter2 = new FilterViewModel();
                            filter2.Seq = AN[j].FindSeq;
                            filter2.StartExonPos = AN[j].StartExonPos;
                            filter2.EndExonPos = AN[j].EndExonPos;
                            filter2.GeneId = AN[j].GeneId;
                            filter2.ExonId = AN[j].ExonId;
                            filter2.EqualsCount = AN[j].EqualsCount;
                            couple.Add(filter2);
                            k = k + 1;
                        }

                    }
                    unios.Add(couple);
                    i = i + 1;
                }
                List<CountWork> Ac = new List<CountWork>();
                foreach (var items in unios)
                {
                    CountWork n = new CountWork();
                    foreach (var m in items)
                    {
                        n.Allcount += m.EqualsCount;
                    }
                    Ac.Add(n);
                }
                //List<ResultFilters> rfs = new List<ResultFilters>();
                if (Ac.Count != 0)
                {
                    var max = Ac.Select(p => p.Allcount).Max();
                    var order = Ac.FindIndex(x => x.Allcount == max);
                    var resultFilter = unios[order].OrderBy(p => p.StartExonPos).ToList();
                    var q = resultFilter.GroupBy(x => x.GeneId).ToList();
                    foreach (var n in q)
                    {
                        ResultFilters resultFilters = new ResultFilters();
                        resultFilters.GeneId = N.Key;
                        var= n.Select MAXM (P => p.EndExonPos) .Max ();
                         var minm = n.Select (X => x.StartExonPos) .MIN (); 
                        resultFilters.SeqLength = MAXM - minm + . 1 ; 
                        resultFilters.filterInfo = n.ToList (); 
                        resultFilters.FullNameParmPath = listSeqPath; 
                        rfs.Add (resultFilters); 
                        CallBackExonanalysis (RFS);   // through a delegate, the data back to the callback function                         
                    } 
                } 
                the else 
                { 
                    rfs.Add ( null ); 
                    CallBackExonanalysis (RFS);   // through a delegate, the data back to the callback function                    
                }               
            } the catch {
                 // List <ResultFilters> RFS = new new List <ResultFilters> ();               
                 // CallBackExonanalysis (RFS );   // through a delegate, the data back to the callback function         
            } 
           
                       
        }       

The callback function result processing sub-threads (here is the child thread callback function, and the results can be written txt file):

 public void CallBackExonanalysis(List<ResultFilters> rfs)
        {
           
           ... 
                         
        }

By then converted txt file list, returned to the front desk

 // read a text file into a List 
        [HttpGet ( " [Action] " )]
         public List <ResultFilters> ReadTextFileToList ( String sampleid, String select_gene, String enstId, int select_page = . 1 , int tab_page = 0 ) 
        { 
           ... 
           . .. 
           return List; 
        }

 

Finally, the front desk may request additional first with a single request this ReadTextFileToList , if the list is not empty indicates that the file content (that is, the background is not yet processed data) then call the main thread request, otherwise no treatment.

 // only one request is determined whether a file has results 
        Axios.get ( 'API / ExonCom / ReadTextFileToList' , {the params: {sampleid: query.sampleid, select_gene: query.select_gene, enstId: query.enstId, select_page: query.select_page, tab_page: _this.tab_page}}) 
            .then ( function (RES: the any) {
                 IF (res.data.length == 0 ) {
                     // do not result 
                    _this.ExonCompareFlag = to true ;
                     IF (== _this.ExonCompareFlag to true ) { 
                        _this.requestExonCompare (); 
                    } 
                } the else {                   
                    _this.ExonCompareFlag = false;
                }
            })
            .catch(function (error) {
                alert(error);
            });
 requestExonCompare() {
        const _this = this;
        var query = _this.$route.query;
        let formData = new FormData();
        let config = {
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        }
        formData.append("sampleid", query.sampleid);
        formData.append("gene", query.select_gene);
        formData.append("enstId", query.enstId);
        formData.append("select_page", query.select_page);
        formData.append("tab_page", _this.tab_page.toString());
        
        Axios.post('api/ExonCom/ExonCompare', formData, config).then(function (res:any) {
            //console.log(res);            
        });         
    } 

 

Guess you like

Origin www.cnblogs.com/xinjianheyi/p/11242129.html