C # console application still in memory after exiting - Asynchronous web service

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> 
+4
source share
3 answers

You can place the button on the page and record the logic for stopping the timer in the button press event. After that, generate a script to close the window.

+1
source

Although your client has been interrupted, the asynchronous call to mainMethod () is still ongoing. The web service does not know that the client has left and continues to work.

By launching the second client, your code will make a second call to mainMethod (), and now two instances are running. Both are trying to update the values ​​of the iTotal and iCurrent applications. That's why the numbers seem to go back and forth.

You need to specify one of the mainMethod () threads to stop updating the application values ​​and complete it.

So there are many ways to do this, but a simple way might be something like this:

 [WebMethod] public void mainMethod() { iTotal = 42; iCurrent = 1; int iReference = (int)(Application["iReference"] ?? 0); ++iReference; Application["iReference"] = iReference; Application["iTotal"] = iTotal; while( (iCurrent <= iTotal) && ((int)(Application["iReference"]) == iReference) ) { Application["iCurrent"] = iCurrent; ++iCurrent; System.Threading.Thread.Sleep( 10000 ); } } 
+1
source

The IIS workflow is probably still working. You will need to kill w3wp.exe, which works.

0
source

Source: https://habr.com/ru/post/1400261/