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

  • SEARCH
  • Home
  • 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 4619868
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:28:58+00:00 2026-05-22T02:28:58+00:00

Hello is this possible in c#: If I have a loop say: int x

  • 0

Hello is this possible in c#:

If I have a loop say:

int x = 0;
for (int i = 0; i < 1000000; i++) {
  x++;
}

And it takes more than 1 second to complete, is it possible to kill the code and move on?

  • 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-05-22T02:28:59+00:00Added an answer on May 22, 2026 at 2:28 am

    Yes, if you have the loop running in a different thread, you can abort that thread from a different thread. This would effectively kill the code you’re running, and would work fine in the simplified example you’ve given. The following code demonstrates how you might do this:

    void Main()
    {
        double foo = 0;
        var thread = new Thread(() => foo = GetFoo());
        thread.Start();
        string message;
        if(thread.Join(1000))
        {
            message = foo.ToString();
        }
        else 
        {
            message = "Process took too long";
            thread.Abort();
        }
        message.Dump();
    }
    
    public double GetFoo() {
        double a = RandomProvider.Next(2, 5); 
        while (a == 3) { RandomProvider.Next(2, 5);} 
        double b = RandomProvider.Next(2, 5); 
        double c = b /a; 
        double e = RandomProvider.Next(8, 11); 
        while (e == 9) { RandomProvider.Next(8,11); } 
        double f = b / e;
        return f;
    }
    

    However, as you can see from Eric Lippert’s comment (and others), aborting a thread is not very graceful (a.k.a. “pure evil”). If you were to have important things happening inside this loop, you could end up with data in an unstable state by forcing the thread abortion in the middle of the loop. So it is possible, but whether you want to use this approach will depend on what exactly you’re doing in the loop.

    So a better option would be to have your loops voluntarily decide to exit when they’re told to (Updated 2015/11/5 to use newer TPL classes):

    async void Main()
    {
        try
        {
            var cancelTokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(1000));
            var foo = await GetFooAsync(cancelTokenSource.Token);
            Console.WriteLine(foo);
        }
        catch (OperationCanceledException e)
        {
            Console.WriteLine("Process took too long");
        }
    }
    
    private Random RandomProvider = new Random();
    
    public async Task<double> GetFooAsync(CancellationToken cancelToken) {
        double a = RandomProvider.Next(2, 5); 
        while (a == 3) 
        { 
            cancelToken.ThrowIfCancellationRequested();
            RandomProvider.Next(2, 5);
        } 
        double b = RandomProvider.Next(2, 5); 
        double c = b /a; 
        double e = RandomProvider.Next(8, 11); 
        while (e == 9) 
        { 
            cancelToken.ThrowIfCancellationRequested();
            RandomProvider.Next(8,11); 
        } 
        double f = b / e;
        return f;
    }
    

    This gives GetFooAsync an opportunity to catch on to the fact that it’s supposed to exit soon, and make sure it gets into a stable state before giving up the ghost.

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

Sidebar

Related Questions

Say i have this main int int main(); { cout << Hello! ; function1();
I have a JSON data like this: { hello: { first:firstvalue, second:secondvalue }, hello2:
Hello i have this form filling javascript: function onLine(code,nn) { document.writeform.bericht.value+=code; document.writeform.bericht.focus(); document.writeform.nickname.value+=nn; write1();
I have some HTML code that goes a little something like this. <p style=text-align:left;>Hello,
Essentially I have this <p>hello</p> <p>So I wanted just to say hi</p> <p>I hope
say I have a piece of text like this Hello I am some text
Hello! I'm not even sure if this is possible, but hopefully it is in
Hello this is may first question and I have found so far the following
I have a string containing something like this Hello bla bla bla bla ok,
If I have 2 files each with this: Hello World (x 1000) Does that

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.