I am really new to C # and .Net. My boss asked me to create a timer using an asynchronous callback using a web service. I created it and started working, but whenever I close the console application window, the web service is still working. When I restart the application, it starts the counter from 0, but jumps back and forth by the number from the previous run (which is still in memory).
mainMethod () increments the number by 1 every 10 seconds, and monitorMethod () reads that number and returns a string.
How to stop the web service and reset the counter back to 0 every time I close the console window?
Hope this makes sense! Thanks in advance!
Here is my code.
Web service:
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"; } } }
Client side
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) { } } }
Webpage
<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>
source share