gpt4 book ai didi

退出后 C# 控制台应用程序仍在内存中 - 异步 Web 服务

转载 作者:行者123 更新时间:2023-11-30 12:33:15 25 4
gpt4 key购买 nike

我是 C# 和 .Net 的新手。我的老板要求我使用 Web 服务使用异步回调创建一个计时器。我实际创建了它,并让它开始工作,但每当我关闭控制台应用程序窗口时,Web 服务仍在运行。当我重新运行该应用程序时,它从 0 开始计数器,但会来回跳转到上次运行时的数字(仍在内存中)。

mainMethod() 每 10 秒将数字递增 1,monitorMethod() 读取该数字,并返回一个字符串。

如何停止 Web 服务,并在每次关闭控制台窗口时将计数器重置为 0?

我希望这是有道理的!提前致谢!

这是我的代码。

网络服务:

 public class Service1 : System.Web.Services.WebService
{
private int iTotal;
private int iCurrent;

[WebMethod]
public void mainMethod()
{
iTotal = 42;
iCurrent = 1;
Application["iTotal"] = iTotal;
Application["iCurrent"] = iCurrent;
// sleep 10 seconds
while (iCurrent <= iTotal)
{
Application["iCurrent"] = iCurrent;
iCurrent++;
System.Threading.Thread.Sleep(10000);
}
}

[WebMethod]
public string monitorMethod()
{
iCurrent = int.Parse(Application["iCurrent"].ToString());
iTotal = int.Parse(Application["iTotal"].ToString());
if (iCurrent <= iTotal)
{
return iCurrent + " of " + iTotal;
}
else
return "DONE";
}
}
}

客户端

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Service1 client = new Service1(); //web service proxy
client.mainMethodCompleted += new mainMethodCompletedEventHandler(client_mainMethodCompleted);

if (Session["value"] == null)
{
Session["value"] = true;
}

if (!IsPostBack)
{
client.BeginmainMethod(AsyncCallback, null);
string scriptFunction = "<script type=\"text/javascript\">function reSubmit(){ document.getElementById(\"" + btnProcessNext.ClientID + "\").click(); }</script>";
this.RegisterClientScriptBlock("s1", scriptFunction);
string script = "<script type=\"text/javascript\">setTimeout(\"reSubmit()\", 500);</script>";
this.RegisterStartupScript("submit", script);
}
else
{
if (StringParseByMe(lblStatus.Text))
{
string scriptFunction = "<script type=\"text/javascript\">function reSubmit(){ document.getElementById(\"" + btnProcessNext.ClientID + "\").click(); }</script>";
this.RegisterClientScriptBlock("s1", scriptFunction);
string script = "<script type=\"text/javascript\">setTimeout(\"reSubmit()\", 500);</script>";
this.RegisterStartupScript("submit", script);
}
else
return;
}
}

protected void btnProcessNext_Click(object sender, EventArgs e)
{
Service1 client = new Service1();
string label = client.monitorMethod();
Session["value"] = StringParseByMe(label);
lblStatus.Text = label;
}

private bool StringParseByMe(string sparse)
{
if (sparse == "DONE")
return false;

string[] sparseArray = sparse.Split(' ');
if (int.Parse(sparseArray[0]) == int.Parse(sparseArray[2]))
{
return false;
}
else
return true;
}

void client_mainMethodCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
Service1 client = new Service1();
client.EndmainMethod(ar);
}

public void AsyncCallback(IAsyncResult ar)
{
}
}
}

网页

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
Counter</h2>
<br /><br /><br /><br /><br /><br />
<asp:Label ID="lblStatus" runat="server" Text="0 of -1" Font-Bold="True"
Font-Size="XX-Large" ForeColor="#CC0000"></asp:Label>
<br /><br /><br /><br />
<asp:Button ID="btnProcessNext" runat="server" Text="Refresh"
onclick="btnProcessNext_Click" />
</asp:Content>

最佳答案

可以在页面上放一个按钮,把停止定时器的逻辑写在按钮的点击事件中。之后,生成关闭窗口的脚本。

关于退出后 C# 控制台应用程序仍在内存中 - 异步 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9606905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com