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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:14:49+00:00 2026-05-16T05:14:49+00:00

I’m just trying to run a new thread each time a button click even

  • 0

I’m just trying to run a new thread each time a button click even occurs which should create a new form. I tried this in the button click event in the MainForm:

private void button1_Click(object sender, EventArgs e)
{
    worker1 = new Thread(new ThreadStart(thread1));
    worker2 = new Thread(new ThreadStart(thread2));

    worker1.Start();
    worker2.Start();
}

private void thread1()
{
    SubForm s = new SubForm();
    s.Show();
}

private void thread2()
{
    SubForm s = new SubForm();
    s.Show();
}

The code in the Subform button click event goes like this:

private void button1_Click(object sender, EventArgs e)
{
    int max;
    try
    {
        max = Convert.ToInt32(textBox1.Text);
    }
    catch
    {
        MessageBox.Show("Enter numbers", "ERROR");
        return;
    }

    progressBar1.Maximum = max;

    for ( long i = 0; i < max; i++)
    {
        progressBar1.Value = Convert.ToInt32(i);
    }          
}

Is this the right way? Because I’m trying to open two independent forms, operations in one thread should not affect the other thread.

Or is BackGroundworker the solution to implement this? If yes, can anyone please help me with that?

  • 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-16T05:14:50+00:00Added an answer on May 16, 2026 at 5:14 am

    Although I’m not 100% aware of anything that says running completely seperate forms doing completely isolated operations in their own threads is dangerous in any way, running all UI operations on a single thread is generally regarded as good practice.

    You can support this simply by having your Subform class use BackgroundWorker. When the form is shown, kick off the BackgroundWorker so that it processes whatever you need it to.

    Then you can simply create new instances of your Subform on your GUI thread and show them. The form will show and start its operation on another thread.

    This way the UI will be running on the GUI thread, but the operations the forms are running will be running on ThreadPool threads.

    Update

    Here’s an example of what your background worker handlers might look like – note that (as usual) this is just off the top of my head, but I think you can get your head around the basic principles.

    Add a BackgroundWorker to your form named worker. Hook it up to the following event handlers:

    void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        // Executed on GUI thread.
        if (e.Error != null)
        {
            // Background thread errored - report it in a messagebox.
            MessageBox.Show(e.Error.ToString());
            return;
        }
    
        // Worker succeeded.
    }
    
    void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        // Executed on GUI thread.
        progressBar1.Value = e.ProgressPercentage;
    }
    
    void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        // Executed on ThreadPool thread.
        int max = (int)e.Argument;
    
        for (long i = 0; i < max; i++)
        {
            worker.ReportProgress(Convert.ToInt32(i));
        }
    }
    

    Your click handler would look something like:

    void button1_Click(object sender, EventArgs e)
    {
        int max;
    
        try
        {
            // This is what you have in your click handler,
            // Int32.TryParse is a much better alternative.
            max = Convert.ToInt32(textBox1.Text);
        }
        catch
        {
            MessageBox.Show("Enter numbers", "ERROR");
            return;
        }
    
        progressBar1.Maximum = max;
    
        worker.RunWorkerAsync(max);
    }
    

    I hope that helps.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I would like to run a str_replace or preg_replace which looks for certain words
I'm trying to select an H1 element which is the second-child in its group
I'm trying to create an if statement in PHP that prevents a single post
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported

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.