简体   繁体   English

ASP .NET MVC - 如何在线程完成时更改视图

[英]ASP .NET MVC - how to change view when thread will finished

I want to make ActionResult with run a thread.我想通过运行线程来创建 ActionResult。 When thread are running I will return View with loading but when thread finished the ActionResult return the another View当线程运行时,我将返回带有加载的视图,但是当线程完成时,ActionResult 返回另一个视图

My code look like:我的代码看起来像:

  private static void ExecuteScrapper()
        {
            ScriptEngine pythonEngine = IronPython.Hosting.Python.CreateEngine();
            ScriptSource pythonScript = pythonEngine.CreateScriptSourceFromFile(HostingEnvironment.MapPath("/PythonScript/main.py"));

            var searchPath = pythonEngine.GetSearchPaths();
            searchPath.Add(HostingEnvironment.MapPath("/PythonScript/"));
            searchPath.Add(HostingEnvironment.MapPath("/PythonScript/drivers/"));
            pythonEngine.SetSearchPaths(searchPath);

            var result = pythonScript.Execute();

        }

        private ScrapperEnum.ScrapperOperations scraperParam;

        private Thread t = new Thread(new ThreadStart(ExecuteScrapper));

        public ActionResult RunScrapper(ScrapperEnum.ScrapperOperations operation)
        {
            scraperParam = operation;
            t.Start();
            bool run = false;

            while(t.IsAlive)
            {
                if(!run)
                {
                    run = true;
                    return View("ScrapperLoadingView");
                }
            }

            return RedirectToAction("Scrapper");
        }

I don't know exactly how to do that, because when in this code I return ScrapperLoginView it will break while so the function don't switch the View我不知道该怎么做,因为在这段代码中我返回 ScrapperLoginView 时它会中断,而 function 不会切换视图

You just need to add an else if I'm correct如果我是对的,你只需要添加一个 else

     while(t.IsAlive)
                {
                    if(!run)
                    {
                        run = true;
                        return View("ScrapperLoadingView");
                    }

             else 
            {
                return RedirectToAction("Scrapper");
            }
           }
         }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM