CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2005
    Posts
    33

    Question how to break from from an infinite loop based on a click event

    Hi,

    I am writting a applicaton in C#.net and very much new to this.
    I am performing some operation infinitely when i press button A, but I want to break this infinite loop when I press button B ....I want to know which event i should use for this purpose. it would be great if someone can help me out on this.

    Thanks,
    M.

  2. #2
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: how to break from from an infinite loop based on a click event

    I think you will need a thread for that.
    Here is an example:
    Code:
    private void button1_Click(object sender, System.EventArgs e)
    		{
    			_t = new Thread(new ThreadStart(DoSomething));
    			_t.Start();
    		}
    
    		private Thread _t;
    		private void DoSomething()
    		{
    			try
    			{
    				while(true)
    				{
    					Thread.Sleep(0);
    				}
    			}
    			catch (ThreadAbortException ex)
    			{
    				MessageBox.Show("Loop break");
    				return;
    			}
    		}
    
    		private void button2_Click(object sender, System.EventArgs e)
    		{
    			_t.Abort();
    		}

  3. #3
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: how to break from from an infinite loop based on a click event

    You should never kill a thread using Abort unless there's a very good reason to do so. You should have an event to signal that the thread should exit e.g.

    Code:
    private void button1_Click(object sender, System.EventArgs e)
    {
    	_t = new Thread(new ThreadStart(DoSomething));
    	_t.Start();
    }
    
    private Thread _t;
    private ManualResetEvent _eventStop = new ManualResetEvent(false);
    
    private void DoSomething()
    {
    	while (!_eventStop.WaitOne(1, false))
    	{
    		// put your calculation code in here
    	}
    }
    
    private void button2_Click(object sender, System.EventArgs e)
    {
    	_eventStop.Set();
    }
    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  4. #4
    Join Date
    Mar 2005
    Posts
    33

    Re: how to break from from an infinite loop based on a click event

    Thanks a bunch....it works.


    M.

  5. #5
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: how to break from from an infinite loop based on a click event

    Thank you Darwen, I did not know that.
    Can you please explain why calling Abort is not good?

  6. #6
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: how to break from from an infinite loop based on a click event

    Abort is used to terminate threads abnormally. The main reason for not using Abort is because there's no way of knowing at which line of code the thread will exit : so you don't know in this case whether the calculation has finished or not. It's a brute force method which could potentially put the system into an unstable state if something else relies on the result of the thread code.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  7. #7
    Join Date
    Nov 2005
    Posts
    159

    Re: how to break from from an infinite loop based on a click event

    So, after pressing button2 the threadstate is 'Stopped'.

    Is there a way to restart/continue it?

    So one can use it as a 'pause' button?

  8. #8
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: how to break from from an infinite loop based on a click event

    Darwen gave this code:
    Code:
    private void DoSomething()
    {
    	while (!_eventStop.WaitOne(1, false))
    	{
    		// put your calculation code in here
    	}
    }
    You will need to save the state of the operation (in members) when the thread is stopped (after the loop), and continue the operation where it left off by reading the members (before the loop).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured