尝试重定向时,值未在预期范围内

时间:2019-05-24 09:06:08

标签: c# asp.net multithreading

首先,我看过几乎所有与我标题相同的问题,但没有一个对我有用。

我有一个简单的DigitalSignage Web应用程序,可通过仪表板对其进行控制。有时,管理员想重定向另一个网页。我有一个线程正在侦听json文件,并在2页之间来回重定向。

但是,标题提到了Value does not fall within the expected range.

这是我的堆栈跟踪;

  at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
  at System.Web.Hosting.IIS7WorkerRequest.SendStatus(Int32 statusCode, Int32 subStatusCode, String statusDescription)
  at System.Web.HttpResponse.UpdateNativeResponse(Boolean sendHeaders)
  at System.Web.HttpResponse.Flush(Boolean finalFlush, Boolean async)
  at System.Web.HttpResponse.Flush()
  at System.Web.HttpResponse.End()
  at System.Web.HttpResponse.Redirect(String url, Boolean endResponse, Boolean permanent)
  at System.Web.HttpResponse.Redirect(String url)
  at DigitalSignageCCTV.DigitalSignageFS.TaskLoop()
  at DigitalSignageCCTV.DigitalSignageFS.<>c__DisplayClass1.<Page_LoadComplete>b__0()
  at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
  at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
  at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
  at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
  at System.Threading.ThreadHelper.ThreadStart()

这是我遇到此异常的地方:

System.Web.HttpContext.Current.Response.Redirect("PAGE_TO_REDIRECT.aspx");

任何帮助将不胜感激,

谢谢。

更新

基本上,线程每1分钟读取一次json文件,并根据JSON数据采取措施。 这是引发异常的代码:

这是我的线程循环:

  public void TaskLoop()
       {
           bool isTime = false;

           while (!isTime)
           {
               // Execute scheduled task
               Redirection red = ScheduledTask();

               if (red.end.Date <= DateTime.Now && !red.end.IsRedirected)
               {
                   isTime = true;
                       using (StreamWriter file = new StreamWriter(filePath))
                       {
                           red.end.IsRedirected = true;
                           JsonSerializer serializer = new JsonSerializer();
                           serializer.Serialize(file, red);
                       }
                   System.Web.HttpContext.Current.Response.Redirect("PAGE_TO_REDIRECT.aspx");
               }
               // Wait for certain time interval
               System.Threading.Thread.Sleep(TimeSpan.FromMinutes(1));
           }
       }

这是页面加载后开始我的任务的地方。


       protected void Page_LoadComplete(object sender, EventArgs e)
       {
           HttpContext ctx = HttpContext.Current;
           Thread t = new Thread(new ThreadStart(() =>
           {
               HttpContext.Current = ctx;
               TaskLoop();
           }));
           t.Start();
       }

0 个答案:

没有答案