当我导航到其他页面并返回时,SignalR 进度条未恢复
SignalR progress bar not resumed when i navigate to other page and come back
我在 Web 表单应用程序(不是 MVC、MVVM)中使用 signalR 在显示进度百分比的页面上显示进度条,我的问题是当我导航到其他页面并返回到上一页时,进度条重新启动。我希望它从我离开的地方恢复。下面是我的 signalR 集线器代码-
public void CallLongOperation()
{
for (int x = 0; x <= count; x++)
{
// delay the process for see things clearly
Thread.Sleep(1000);
if (x == 20)
msg = "Loading Application Settings...";
else if (x == 40)
msg = "Applying Application Settings...";
else if (x == 60)
msg = "Loading User Settings...";
else if (x == 80)
msg = "Applying User Settings...";
else if (x == 100)
msg = "Process Completed!...";
// call client-side SendMethod method
Clients.Caller.sendMessage(string.Format
(msg + " {0}% of {1}%", x, count), x);
}
}
我在我的 aspx 页面的 javascript 中调用这个函数。
<script type="text/javascript" language="javascript">
$(document).ready(function () {
// initialize progress bar
$("#progressbar").progressbar({ value: 0 });
// initialize the connection to the server
var progressNotifier = $.connection.progressHub
// client-side sendMessage function that will be called from the server-side
progressNotifier.client.sendMessage = function (message) {
// update progress
UpdateProgress(message);
};
// establish the connection to the server and start server-side operation
$.connection.hub.start().done(function () {
// call the method CallLongOperation defined in the Hub
progressNotifier.server.callLongOperation();
});
});
function UpdateProgress(message) {
// get result div
var result = $("#result");
// set message
result.html(message);
// get progress bar
var value = $("#progressbar").progressbar("option", "value");
// update progress bar
$("#progressbar").progressbar("value", value + 1);
}
</script>
<body>
<form id="form1" runat="server">
<div style="width: 30%; margin: 0 auto;">
<div id="result" style="font-family: Tahoma; font-size: 0.9em; color: darkgray; margin-top: 230px; padding-bottom: 5px">
Initializing and Preparing...
</div>
<div id="progressbar" style="width: 300px; height: 15px"></div>
<br />
</div>
<a href="Default.aspx" target="_self">Go Home</a>
</form>
</body>
请提出解决方案。
我会尽可能简单地解释问题。
此代码:
// establish the connection to the server and start server-side operation
$.connection.hub.start().done(function () {
// call the method CallLongOperation defined in the Hub
progressNotifier.server.callLongOperation();
});
每次重新加载页面并重新建立与集线器的连接时,都在服务器 public void CallLongOperation()
上执行您的方法。您将需要:
- 像 SPA(单页应用程序)一样构建您的网页,这样它们就不需要重新加载。
- 将值存储在静态
ConcurrentDictionary
每个用户 以便服务器可以 "remember" 每个用户的进度条状态。
在我看来,这是您仅有的两个没有进行重大重构的选项。
我在 Web 表单应用程序(不是 MVC、MVVM)中使用 signalR 在显示进度百分比的页面上显示进度条,我的问题是当我导航到其他页面并返回到上一页时,进度条重新启动。我希望它从我离开的地方恢复。下面是我的 signalR 集线器代码-
public void CallLongOperation()
{
for (int x = 0; x <= count; x++)
{
// delay the process for see things clearly
Thread.Sleep(1000);
if (x == 20)
msg = "Loading Application Settings...";
else if (x == 40)
msg = "Applying Application Settings...";
else if (x == 60)
msg = "Loading User Settings...";
else if (x == 80)
msg = "Applying User Settings...";
else if (x == 100)
msg = "Process Completed!...";
// call client-side SendMethod method
Clients.Caller.sendMessage(string.Format
(msg + " {0}% of {1}%", x, count), x);
}
}
我在我的 aspx 页面的 javascript 中调用这个函数。
<script type="text/javascript" language="javascript">
$(document).ready(function () {
// initialize progress bar
$("#progressbar").progressbar({ value: 0 });
// initialize the connection to the server
var progressNotifier = $.connection.progressHub
// client-side sendMessage function that will be called from the server-side
progressNotifier.client.sendMessage = function (message) {
// update progress
UpdateProgress(message);
};
// establish the connection to the server and start server-side operation
$.connection.hub.start().done(function () {
// call the method CallLongOperation defined in the Hub
progressNotifier.server.callLongOperation();
});
});
function UpdateProgress(message) {
// get result div
var result = $("#result");
// set message
result.html(message);
// get progress bar
var value = $("#progressbar").progressbar("option", "value");
// update progress bar
$("#progressbar").progressbar("value", value + 1);
}
</script>
<body>
<form id="form1" runat="server">
<div style="width: 30%; margin: 0 auto;">
<div id="result" style="font-family: Tahoma; font-size: 0.9em; color: darkgray; margin-top: 230px; padding-bottom: 5px">
Initializing and Preparing...
</div>
<div id="progressbar" style="width: 300px; height: 15px"></div>
<br />
</div>
<a href="Default.aspx" target="_self">Go Home</a>
</form>
</body>
请提出解决方案。
我会尽可能简单地解释问题。
此代码:
// establish the connection to the server and start server-side operation
$.connection.hub.start().done(function () {
// call the method CallLongOperation defined in the Hub
progressNotifier.server.callLongOperation();
});
每次重新加载页面并重新建立与集线器的连接时,都在服务器 public void CallLongOperation()
上执行您的方法。您将需要:
- 像 SPA(单页应用程序)一样构建您的网页,这样它们就不需要重新加载。
- 将值存储在静态
ConcurrentDictionary
每个用户 以便服务器可以 "remember" 每个用户的进度条状态。
在我看来,这是您仅有的两个没有进行重大重构的选项。