Javascript 如何同时向服务器发出两个请求(asp.net mvc 3)
我真的不知道应该如何继续使用jscript轮询信息,并继续调用将在服务器端调用methode的其他脚本 我想要做的是42秒,然后如果它花费太多时间,请终止进程,否则返回新的json数据 但问题是,当我想在新方法工作时调用它时,它不会让我调用,因为它是简单的线程。我尝试使用async controller,但我不确定我是否做得很好。下面是代码的一部分: 控制器Javascript 如何同时向服务器发出两个请求(asp.net mvc 3),javascript,asynchronous,controller,long-integer,polling,Javascript,Asynchronous,Controller,Long Integer,Polling,我真的不知道应该如何继续使用jscript轮询信息,并继续调用将在服务器端调用methode的其他脚本 我想要做的是42秒,然后如果它花费太多时间,请终止进程,否则返回新的json数据 但问题是,当我想在新方法工作时调用它时,它不会让我调用,因为它是简单的线程。我尝试使用async controller,但我不确定我是否做得很好。下面是代码的一部分: 控制器 public class CadController : AsyncController { /// <summ
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();
}
别担心我找到了我需要的()
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);
}
}
}
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, };
}