所以你面临的问题是你需要在服务器端做一些工作,但你想显示那个客户端。问题的第一部分是 HTTP 是一种无状态的无连接协议,因此您必须不断向服务器询问更多信息(更新)。
所以你需要几个资源:
服务器了解当前发送的电子邮件数量的一种方法(例如您可以访问的公共计数器),一种服务器运行以发送电子邮件的方法(这通常需要在后台线程上运行,但要小心,崩溃的后台线程(例如来自未处理的异常)将崩溃整个应用程序池)以及从客户端初始化进程的方法(您已经拥有这部分)。
然后在客户端上,您需要一种方法来发起请求,一种方法来检查更新并知道您何时完成,以及管理您想要的所有其他事情的方法。
我假设您使用的是 jQuery(它是 javascript 之上的一个库,但不会取代 javascript,这似乎是您需要学习的东西)并在此处编写一些基本代码。我还将假设您在页面上运行了 microsoft ASP.NET 库(您的代码引用了 ScriptManager,因此这是一个非常安全的选择)。
让我们从 C# 开始:(这在你的页面中,我不会写所有的东西,只是它的大部分)
//this is a class you need to write, see below
//this way other code can access this variable too
public MySpecialMailerClass mailer;
[WebMethod] //this may require a include, you're on your own for that
//for now (System.Web.Services)
public void StartEmails(){
//do some stuff here to start the email process?
//create a class that can run the db scripts uninterrupted,
//and provide it a public member that can be accessed for the current
//count, and the expected max count (set to -1 on init so you know
//that you haven't finished initting this yet)
mailer = new MySpecialMailerClass();
new Thread( mailer.Start ) { IsBackground = true }.Start( );
}
[WebMethod]
public int GetMaxCount(){
return mailer.MaxCount;
}
[WebMethod]
public int GetCurrentCount(){
return mailer.CurrentCount;
}
这实际上是您现在需要添加的所有 C# 代码。
Javascript:
var checkingOnEmailsSentIntervals,
checkingOnMaxMessagesIntervals,
maxMessages = 0;
function SuccessHandler(){
//Note that you should do something here to handle the success each
//time or that you should write a separate "SuccessHandler"
// for each one that you want to execute _when the server call is done_
// Note that this is what I do (one success per call)
// But in this case a generic one is sometimes handy, such as here
}
function FailHandler() {
//Note that you should do something here to handle the failure each time
}
function startScript(){
PageMethods.StartEmails(StartEmailsSuccessHandler,FailHandler);
}
function StartEmailsSuccessHandler(){
//this will get triggered when the emails have started sending,
// so this is where you create the boxes you wanted earlier, etc.
$('#counter-span-identifier').show();
checkingOnEmailsSentIntervals = setInterval(CheckForMoreMessages,100);
checkingOnMaxMessagesIntervals = setInterval(CheckForMaxMessages,10);
}
function CheckForMoreMessages() {
PageMethods.GetCurrentCount(CheckForMoreMessagesSuccessHandler,FailHandler);
}
function CheckForMoreMessagesSuccessHandler(result) {
var currentCount = result;
$('#counter-span-identifier').text(currentCount + ' messages sent');
if (currentCount == maxMessages) {
//we have finished now, so we can display another message or hide our div
clearInterval(checkingOnEmailsSentIntervals);
$('#counter-span-identifier').hide();
}
}
function CheckForMaxMessages() {
PageMethods.GetMaxCount(CheckForMaxMessagesSuccessHandler,FailHandler);
}
function CheckForMaxMessagesSuccessHandler(result) {
maxMessages = parseInt(result); //parseInt not really needed here,
//shown as example of coercion
if (maxMessages > -1){
clearInterval(checkingOnMaxMessagesIntervals);
}
}
这应该很好地结束了这里的情况,因为我们在 javascript 中有两个循环 (setInterval) 来检查 MaxMessages(因此返回 -1)和 CurrentCount。一旦获得所需的信息,我们也有停止每个循环的代码。
留给您做的部分是将发送电子邮件的代码放入一个单独的类中,该类公开成员变量和一个方法(并重命名我的示例以使其符合您的要求)。
因为我认为它需要完成,这里是 C# 让MySpecialMailerClass
你开始:
public class MySpecialMailerClass {
public MySpecialMailerClass() {
MaxCount = -1;
CurrentCount = 0;
}
public int MaxCount {get;set;}
public int CurrentCount {get;set;}
public void Start(){
//do something to get the MaxCount from the database
//do something to get the messages
var messages = mailerDbClass.GetMessagesToBeSent();
MaxCount = messages.Count;
//send the messages
foreach(message in messages) {
SendMessage(message);
CurrentCount += 1;
}
}
}