简体   繁体   中英

long running task that is viewable having only 1 instance

General question. A webpage is served to a user, data goes in n out for that session. In c# and asp.net, how can u create a page on server that continually does a task and also has only 1 instance running. it does something over and over. Then as needed a person could view what it's doing.

You can run a long task in a separate thread and communicate with it when you need. something like this.

protected void btnRun_Click(object sender, EventArgs e)
{
    var jobState = new StateInfo()
    {
        Id = 1,
        Counter = 0,
        Content = "Start the job",
        Cancelled = false,
        Completed = false
    };
    Session["job"] = jobState;
    System.Threading.ThreadPool.QueueUserWorkItem(
        new System.Threading.WaitCallback(LongJob),
        jobState
        );//returns immediately
    lblToken.Text += "<br />" + jobState.Counter.ToString() 
        + " Completed: " + jobState.Completed.ToString()
        + " Cancelled: " + jobState.Cancelled.ToString()
        + "<br />" + jobState.Content;
    btnCancel.Visible = true;
    btnCheck.Visible = true;
}

protected void btnCancel_Click(object sender, EventArgs e)
{
    var jobState = Session["job"] as StateInfo;
    if (!jobState.Completed)
        jobState.Cancelled = true;
    System.Threading.Thread.Sleep(1000);//wait for the next loop to complete
    lblToken.Text += "<br />" + jobState.Counter.ToString()
        + " Completed: " + jobState.Completed.ToString()
        + " Cancelled: " + jobState.Cancelled.ToString()
        + (jobState.Completed || jobState.Cancelled ? "<br />" + jobState.Content : "");
}

protected void btnCheck_Click(object sender, EventArgs e)
{
    var jobState = Session["job"] as StateInfo;
    lblToken.Text += "<br />" + jobState.Counter.ToString()
        + " Completed: " + jobState.Completed.ToString()
        + " Cancelled: " + jobState.Cancelled.ToString()
        + (jobState.Completed || jobState.Cancelled ? "<br />" + jobState.Content : "");
}

private void LongJob(object state)
{
    var jobState = state as StateInfo;
    do
    {
        System.Threading.Thread.Sleep(1000);
        jobState.Counter++;
        if (jobState.Counter >= 100)
        {
            jobState.Completed = true;
            jobState.Content = "The job is completed";
        }
        else if (jobState.Cancelled)
            jobState.Content = "The job is cancelled";
    }
    while (!jobState.Cancelled && !jobState.Completed);
}
[Serializable]
class StateInfo
{
    public int Id { get; set; }
    public int Counter { get; set; }
    public string Content { get; set; }
    public bool Cancelled { get; set; }
    public bool Completed { get; set; }
}

and obvious client controls.

<asp:Label ID="lblToken" runat="server"></asp:Label><br />
<asp:Button runat="server" ID="btnRun" OnClick="btnRun_Click" Text="Run" />
<asp:Button runat="server" ID="btnCheck" OnClick="btnCheck_Click" Text="Check State" Visible="false" />
<asp:Button runat="server" ID="btnCancel" OnClick="btnCancel_Click" Text="Cancel" Visible="true" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM