Ejemplo n.º 1
0
        private static Result WaitingForResult(string apikey, string taskid, TimeSpan maxWaitingTime)
        {
            //queued
            DateTime requestSent = DateTime.Now;
            bool     done        = false;

            do
            {
                TimeSpan waiting = DateTime.Now - requestSent;
                string   data    = null;
                try
                {
                    using (WebOcr wc = new WebOcr(60 * 1000))
                    {
                        data = wc.DownloadString(ApiUrl + $"result.ashx?apikey={apikey}&taskid={taskid}");
                    }
                }
                catch (System.Net.WebException ex)
                {
                    throw new ApiException("WaitingForResultAsync web exc", ex);
                }
                TaskStatus status = Newtonsoft.Json.JsonConvert.DeserializeObject <TaskStatus>(data);
                if (status.Status == TaskStatus.CurrentStatus.NotFound)
                {
                    return new Result()
                           {
                               Id = taskid, IsValid = Result.ResultStatus.Invalid, Error = "not found", Started = requestSent, Ends = DateTime.Now
                           }
                }
                ;
                else if (status.Status == TaskStatus.CurrentStatus.Done)
                {
                    return(Newtonsoft.Json.JsonConvert.DeserializeObject <Result>(status.Result));
                }

                if (waiting > maxWaitingTime)
                {
                    return new Result()
                           {
                               Id = taskid, IsValid = Result.ResultStatus.Invalid, Error = "timeout", Started = requestSent, Ends = DateTime.Now
                           }
                }
                ;


                if (waiting.TotalMilliseconds > longTimeThreshold)
                {
                    System.Threading.Thread.Sleep(longWaitingTimesForResult);
                }
                else
                {
                    System.Threading.Thread.Sleep(shortgWaitingTimesForResult);
                }
            } while (done == false);

            return(new Result()
            {
                Id = taskid, IsValid = Result.ResultStatus.Invalid, Error = "timeout", Started = requestSent, Ends = DateTime.Now
            });
        }
Ejemplo n.º 2
0
        public static bool PingExternal(string url)
        {
            var pingStruct = new { server = "" };

            try
            {
                using (WebOcr wc = new WebOcr(30000))
                {
                    var json = Newtonsoft.Json.JsonConvert.DeserializeAnonymousType(wc.DownloadString(url + "ping.ashx"), pingStruct);
                    return(json.server == "OK");
                }
            }
            catch (Exception)
            {
                return(false);
            }
        }