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 9065565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:32:01+00:00 2026-06-16T16:32:01+00:00

I have a while loop running in my .NET backgroundworker. I would like to

  • 0

I have a while loop running in my .NET backgroundworker. I would like to end the loop when Timers.Timer reaches 0.

Problem is that since I’m working in another thread (backgroundworker), my timer has to be instantiated in that same thread. So I can’t set any private boolean timer_Elapsed. Nether do I know how to give reference of boolean thro event.

Code Example:

private bool timer_Elapsed = false;

    private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        System.Timers.Timer timer = new System.Timers.Timer();
        Set_Timer(timer);

        timer.Start();

        while(timer_Elapsed) //Has to be a boolean that indicates if timer elapsed
        {
            this.Do_Proces();
        }
    }

    private void Set_Timer(System.Timers.Timer timer)
    {
        timer.Interval = 200;
        timer.Elapsed += new ElapsedEventHandler(timer_ElapsedEvent);
    }

    private void timer_ElapsedEvent(object source, ElapsedEventArgs e)
    {
        timer_Elapsed = true; //I can't set my private boolean since it got instantiated in another thread
    }

Particular questions in code. I’m new with this kind of stuff.
Any suggestions? Thanks in advance

EDIT: To clarify, I want the Do_Proces() to run for 200 milliseconds, when that time passed, I want it to stop. When it stops after 200 millisec, I want to and update GUI with data generated in backgroundWorker. Then check if user wants the proces to stop, if not, I want it to run again.. I use the timer because the thread will have to get restarted to much, this will have effect on the main thread as well, effecting the user experience badly.

  • 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-16T16:32:03+00:00Added an answer on June 16, 2026 at 4:32 pm

    Is the timer serving any other purpose other than listed here? If not, you may just want to record the current time at the beginning of your BackgroundWorker method, and change the condition on the while loop to check if the required amount of time has elapsed.

    private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        TimeSpan timeout = TimeSpan.FromMinutes(5);
        DateTime start_time = DateTime.Now;
    
        while(DateTime.Now - start_time < timeout)
        {
            this.Do_Proces();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a while(true) loop running for a Java server (to listen) that I
I have a while loop running that is returning values from a MYSQL table.
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
So with pygame you have a while loop that loops continuously, then your event
I have a script that uses a simple while loop to display a progress
I have a function creating entries via grid() like this: (replace while-loop with function,
I have a while loop that constructs a url for an SMS api. This
EDIT: I'm now sure that the problem is related to the while (true) loop
I'm running a WebSocketHandler with Tornado, and I have a while loop inside the

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.