如何同时向服务器发出2个请求(asp.net mvc 3)

发布于 2025-01-06 02:19:48 字数 4126 浏览 1 评论 0原文

我真的不知道应该如何继续使用 jscript 轮询信息并继续调用将在服务器端调用 methode 的其他脚本。

我想要的是做 42 秒的事情,然后如果花费太多时间就终止进程,否则返回新的 json 数据。

但问题是,当我想在该方法运行时调用新方法时,它不会让我这样做,因为它的线程很简单。我尝试使用异步控制器,但我不确定我是否做得很好,这里是代码的一部分:

Controller

    public class CadController : AsyncController

{

    /// <summary>
    /// Use to set the view and set the model in memory
    /// </summary>
    /// <param name="RessourceNo">The ressource number</param>
    /// <returns>The new view</returns>
    public ActionResult Index()
    {
        var _model = new PollModels();
        HttpRuntime.Cache["PollModels"] = _model;
        return View(_model);
    }

    [OutputCache(Duration = 0)]
    /// <summary>
    /// Use to change the data of the model
    /// </summary>
    /// <returns>The new html code</returns>
    public void CadDataAsync()
    {
        AsyncManager.OutstandingOperations.Increment();

        CadDataService _service = new CadDataService();
        _service.GetPollModelsCompleted += (sender, e) =>
         {
             AsyncManager.Parameters["pollModels"] = e.getValue();
             AsyncManager.OutstandingOperations.Decrement();
         };
        _service.GetPollModelsAsync();
    }

    public JsonResult CadDataCompleted(PollModels pollModels)
    {
        return Json(pollModels, JsonRequestBehavior.AllowGet);
    }


    #region [Partial View]

    /// <summary>
    /// Use to set nothing for a div
    /// useful if you want to hide something when you change page
    /// </summary>
    /// <param name="Model">A CadModels Model</param>
    /// <returns>An empty result</returns>
    public ActionResult Blank()
    {
        return new EmptyResult();
    }
    #endregion

    /// <summary>
    /// Use to send the new status to the database
    /// </summary>
    /// <param name="Status">The new status</param>
    /// <param name="Model">The buttons models</param> 
    /// <returns>a empty view</returns>
    public ActionResult PushButton(string description, string code)
    {
        var _manager = new GeneralManager();
        GeneralModels _generalModel = _manager.GetGeneralModels();

        AppEnt.Cad.CadResource _ress = AppBus.Cad.CadResource.GetCadResourceFromName(_generalModel.RessourceNo);
        _ress.ResourceStatusDesc = description;
        _ress.ResourceStatusCode = code;
        ServerV5.GetInstance().Update(_ress);
        return new EmptyResult();
    }

CadDataService

    public class CadDataService
{

    int getSleep() { return (int)SH.eServiceTime.eCadData; }
    void DoSleep() { Thread.Sleep(getSleep()); }

    public PollModels GetPollModels()
    {
        DoSleep();
        var manager = new LongPollingManager();
        return manager.GetPollModels();
    }

    public event EventHandler<PollModelEventArgs> GetPollModelsCompleted;


    public void GetPollModelsAsync()
    {
        SynchronizationContext syncContext = SynchronizationContext.Current;

        Timer timer = new Timer(state =>
            syncContext.Send(d => OnGetPollModelsCompleted(new PollModelEventArgs()), state),
            null, 10000, Timeout.Infinite);
        HttpContext.Current.Items[this] = timer; // don't let the GC kill the timer
    }

    private void OnGetPollModelsCompleted(PollModelEventArgs e)
    {
        EventHandler<PollModelEventArgs> handler = GetPollModelsCompleted;
        if (handler != null)
        {
            handler(this, e);
        }
    }

}

}

最后是 EventArgs

    public class PollModelEventArgs : EventArgs
{
    public PollModels Value;
    public PollModelEventArgs()
    {

    }

    public PollModels getValue()
    {
        var _manager = new LongPollingManager();
        Value = _manager.GetPollModels();
        return Value;
    }


}

public static class SH
{
    public enum eServiceTime { eCadData = 700, };
}

I really dont know how should I proceed to be able to poll information with a jscript and continue to call other script that will call methode on the server side.

What i want is to do stuff for 42 seconds, and then if its take too much time kill the process, otherwise return the new json data.

But the problem is that when I want to call new method while this is working, it wont let me because its simple threaded. I tried with async controller, but im not sure if I do it fine here is part of the code :

Controller

    public class CadController : AsyncController

{

    /// <summary>
    /// Use to set the view and set the model in memory
    /// </summary>
    /// <param name="RessourceNo">The ressource number</param>
    /// <returns>The new view</returns>
    public ActionResult Index()
    {
        var _model = new PollModels();
        HttpRuntime.Cache["PollModels"] = _model;
        return View(_model);
    }

    [OutputCache(Duration = 0)]
    /// <summary>
    /// Use to change the data of the model
    /// </summary>
    /// <returns>The new html code</returns>
    public void CadDataAsync()
    {
        AsyncManager.OutstandingOperations.Increment();

        CadDataService _service = new CadDataService();
        _service.GetPollModelsCompleted += (sender, e) =>
         {
             AsyncManager.Parameters["pollModels"] = e.getValue();
             AsyncManager.OutstandingOperations.Decrement();
         };
        _service.GetPollModelsAsync();
    }

    public JsonResult CadDataCompleted(PollModels pollModels)
    {
        return Json(pollModels, JsonRequestBehavior.AllowGet);
    }


    #region [Partial View]

    /// <summary>
    /// Use to set nothing for a div
    /// useful if you want to hide something when you change page
    /// </summary>
    /// <param name="Model">A CadModels Model</param>
    /// <returns>An empty result</returns>
    public ActionResult Blank()
    {
        return new EmptyResult();
    }
    #endregion

    /// <summary>
    /// Use to send the new status to the database
    /// </summary>
    /// <param name="Status">The new status</param>
    /// <param name="Model">The buttons models</param> 
    /// <returns>a empty view</returns>
    public ActionResult PushButton(string description, string code)
    {
        var _manager = new GeneralManager();
        GeneralModels _generalModel = _manager.GetGeneralModels();

        AppEnt.Cad.CadResource _ress = AppBus.Cad.CadResource.GetCadResourceFromName(_generalModel.RessourceNo);
        _ress.ResourceStatusDesc = description;
        _ress.ResourceStatusCode = code;
        ServerV5.GetInstance().Update(_ress);
        return new EmptyResult();
    }

CadDataService

    public class CadDataService
{

    int getSleep() { return (int)SH.eServiceTime.eCadData; }
    void DoSleep() { Thread.Sleep(getSleep()); }

    public PollModels GetPollModels()
    {
        DoSleep();
        var manager = new LongPollingManager();
        return manager.GetPollModels();
    }

    public event EventHandler<PollModelEventArgs> GetPollModelsCompleted;


    public void GetPollModelsAsync()
    {
        SynchronizationContext syncContext = SynchronizationContext.Current;

        Timer timer = new Timer(state =>
            syncContext.Send(d => OnGetPollModelsCompleted(new PollModelEventArgs()), state),
            null, 10000, Timeout.Infinite);
        HttpContext.Current.Items[this] = timer; // don't let the GC kill the timer
    }

    private void OnGetPollModelsCompleted(PollModelEventArgs e)
    {
        EventHandler<PollModelEventArgs> handler = GetPollModelsCompleted;
        if (handler != null)
        {
            handler(this, e);
        }
    }

}

}

And finnaly the EventArgs

    public class PollModelEventArgs : EventArgs
{
    public PollModels Value;
    public PollModelEventArgs()
    {

    }

    public PollModels getValue()
    {
        var _manager = new LongPollingManager();
        Value = _manager.GetPollModels();
        return Value;
    }


}

public static class SH
{
    public enum eServiceTime { eCadData = 700, };
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文