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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:42:04+00:00 2026-06-05T05:42:04+00:00

I have the following code which is working but it is very dirty. Actually

  • 0

I have the following code which is working but it is very dirty. Actually the code is just fine except the part I added: a pause and a stop button. I’m new to c# so any help would be apreciated.

    private void pause_button_Click(object sender, EventArgs e)
    {
        start = false; pause = true; stop = false;
        guiUpdate();
        PauseEvent.Reset();
    }

    private void stop_button_Click(object sender, EventArgs e)
    {
        if (pause == true)
        {
            PauseEvent.Set();
            pause = false;
            this.start_button.Click -= new System.EventHandler(this.resume_button_Click);
        }
        start = false; stop = true;
    }

    private int activeThreads = 0;
    private Thread thread;
    private void DoWork(object sender)
    {
        string line = null;
        ereader = new StreamReader(MY_LIST);
        do
        {
            lock (ereader)
            {
                PauseEvent.WaitOne();
                line = ereader.ReadLine();
            }

            //
            //other commands for processing & building the argument
            //

            lock (signal)
            {
                ++activeThreads;
            }

            thread = new Thread(new ParameterizedThreadStart(
                o =>
                {
                    processit((object)o);
                    lock (signal)
                    {
                        --activeThreads;
                        Monitor.Pulse(signal);
                    }
                }));
            thread.Start(argument);

            lock (signal)
            {
                while (activeThreads > maxthreads)
                    Monitor.Wait(signal);
            }

            lock (signal)
            {
                if (!start)
                {
                    showwaiting(true);//shows an animated gif with a "please wait" msg
                    while (activeThreads > 0)
                        Monitor.Wait(signal);
                    showwaiting(false);
                    if (stop == true)
                    {
                        this._BackgroundWorker.CancelAsync();
                        break;
                    }
                }
            }
        }
        while (ereader.Peek() != -1);
        showwaiting(true);
        lock (signal)
        {
            while (activeThreads > 0)
                Monitor.Wait(signal);
        }
        showwaiting(false);
    }


    private void _BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        start = false; stop = true;
        guiUpdate();
    }

what exactly can I do to avoid those duplicate commands inside and outside the loop ?

  • 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-05T05:42:05+00:00Added an answer on June 5, 2026 at 5:42 am

    Mentioned in the question comments are suggestions about code practice changes.

    • Do not use == true or == false when checking a boolean variable in a condition. (Jeff)
    • Use an enumerate to represent the state instead of multiple boolean variables.

    I would also add:

    • Use proper C# naming conventions. For your methods if there are multiple words, capitalize all words. (e.g. instead of showwaiting use ShowWaiting).

    For your small loops, you don’t really need to optimize them out though there is more than a simple loop. So you could refactor them to a separate method along with all the code that is the same:

    private void ShowAndWait()
    {
        showwaiting(true);
        lock (signal)
        {
            while (activeThreads > 0)
                Monitor.Wait(signal);
        }
        showwaiting(false);
    }
    

    Note that it is fine for the same thread to lock on a monitor that it previously acquired. This is a fast operation since the thread already owns the lock. You can then call this method from both places.

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

Sidebar

Related Questions

I have the following code which is working, I was wondering if this can
I have trouble to get gluLookAt working. I have the following code which works
I have the simple following code, which is working in a ruby (not rails)
I have following code which works for radio buttons but need to be changed
I have the following code which is fine if I give invalid parameters (though,
I have the follwing code (which is not working): private void Window_PreviewKeyDown(object sender, KeyEventArgs
I want to know is below code correct ? I have following code which
I have the following code which definitely returns a proper data result if I
I have the following code which is used to upload large files (~6MB) to
i have the following code which switches some fullscreen-background-images (fadeOut, fadeIn). setInterval(function() { var

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.