Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have two button
one asp.net control button start and other html button stop .

in start button i have written for loop code as
C#
protected void Button2_Click(object sender, EventArgs e)
   {

for(int i=0;i<=50000;i++)
{
 //dosomething
}

}


what i want is when user click on stop button for loop should exist.
Posted
Updated 7-Nov-13 23:40pm
v3

In the scenario you proposed (both the operation in the GUI thread) you cannot do that.
In order to properly do that, you should move the for loop in a worker thread.
See, for instance "Multithreading in Windows Forms Controls"[^].
 
Share this answer
 
Comments
aassaahh 8-Nov-13 5:50am    
I am using web application
CPallini 8-Nov-13 6:03am    
You are right, I missed the ASP.NET tag. in any case you have to use a similar approach.
aassaahh 8-Nov-13 6:15am    
any example for the above
See I did a solution in asp.net
Code are following below

C#
Thread thLoop = null;
    static string value = "";
    static bool isStopRequired = false;
    protected void Button1_Click(object sender, EventArgs e)
    {
        isStopRequired = false;
        value = "";
        thLoop = new Thread(new ThreadStart(GetLoop));
        thLoop.Start();
    }
    void GetLoop()
    {
        for (int i = 0; i <= 50000; i++)
        {
            value += "  " + i.ToString();
            if (isStopRequired) break;
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        isStopRequired = true;
        if (thLoop != null) thLoop.Abort();
        Response.Write(value);
    }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900