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

退出后C#控制台应用程序仍在内存中 – 异步Web服务,第1张

概述我是C#和.Net的新手.我的老板让我使用Web服务使用异步回调创建一个计时器.我实际上创建了它,并让它工作,但每当我关闭控制台应用程序窗口时,Web服务仍在运行.当我重新运行应用程序时,它从0开始计数器,但是来回跳转到前一次运行的数字(仍然在记忆中). mainMethod()每10秒将数字增加1,monitorMethod()读取该数字,并返回一个字符串. 如何停止Web服务,并在每次关闭控制 我是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服务全部内容,希望文章能够帮你解决退出后C#控制台应用程序仍在内存中 – 异步Web服务所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1234475.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存