Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8599279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:21:49+00:00 2026-06-12T01:21:49+00:00

I have a while loop in a windows service which runs for some x

  • 0

I have a while loop in a windows service which runs for some x times and inititate x phone calls after reading them from some file . Now if i want to make a user interface which gives an option to stop the phone calls i.e. break the while loop even before it is completed. How shall i do it ?

Lets suppose i have a function .

DialCalls(x)
{
  for(int i= 0 ; i<x ; i++)
  {
    // Initiate call
  }
}

There can be 2,3 DialCalls functions running at the same time in different threads as i have done threading in the application as well . So basically what is best way to break the loop from a webpage maybe.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T01:21:50+00:00Added an answer on June 12, 2026 at 1:21 am

    Using task cancellation ( new in .net 4):

    [Fact]
    public void StartAndCancel()
    {
        var cancellationTokenSource = new CancellationTokenSource();
        var token = cancellationTokenSource.Token;
        var tasks = Enumerable.Repeat(0, 2)
                              .Select(i => Task.Run(() => Dial(token), token))
                              .ToArray(); // start dialing on two threads
        Thread.Sleep(200); // give the tasks time to start
        cancellationTokenSource.Cancel();
        Assert.Throws<AggregateException>(() => Task.WaitAll(tasks));
        Assert.True(tasks.All(t => t.Status == TaskStatus.Canceled));
    }
    
    public void Dial(CancellationToken token)
    {
        while (true)
        {
            token.ThrowIfCancellationRequested();
            Console.WriteLine("Called from thread {0}", Thread.CurrentThread.ManagedThreadId);
            Thread.Sleep(50);
        }
    }
    

    http://blogs.msdn.com/b/csharpfaq/archive/2010/07/19/parallel-programming-task-cancellation.aspx
    The task swallows exceptions so there is some cleanup to to, maybe as IDisposable?
    About task exceptions

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The while loop I have while reading in from a file doesn't break. I'm
I have some problems with controlling a while loop inside an event structure. Say
I have a workflow inside a Windows Service that is a loop that performs
I have a multi-thread windows service in .Net 3.5, and I am having some
This is a Windows Forms application. I have a function which captures some mouse
I have a while loop that goes while a BuffedReader still has data, what
I have a while loop that loops through 3 results and echo's these out
hi i have a while loop: var i = 0; while(i < 20) {
So with pygame you have a while loop that loops continuously, then your event
Currently, I have a while loop for listening the port. When a socket accepta

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.