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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:57:57+00:00 2026-06-09T20:57:57+00:00

I have a windows form (Form1) which when a button is clicked on it

  • 0

I have a windows form (Form1) which when a button is clicked on it I want to open another windows form(form2) in a thread.

I want to open it in a thread because when it starts up I have it read and color syntax of 10k+ line long files which takes about 5 or 6 min.

My problems are I do not know the “proper” way to do it. I have found out how to do it and it works right, but I want to be able to have a progress bar telling me how far along Form2 is with its processing. (probably with a Background worker object)

Here is my layout

Form1 has a RichTextBoxes and one buton, click the button and it opens a form in another thread that colors the text in form1’s rtb.

Form2 has a Rtb also, this rtb has a method ProcessAllLines which processes the lines and does the work that I want done in another thread.

This is why I think I am having difficulty. The textbox is doing the work while the form is waiting there to load.

Here is how I open the Form2:

    private void button1_Click(object sender, EventArgs e)
    {
        ColoringThread colorer = new ColoringThread(this.m_bruteView.Text);
        Thread theThread = new Thread(new ThreadStart(colorer.OpenColorWindow));
        theThread.Start();
    }

    public class ColoringThread
    {
        string text;
        public ColoringThread(string initText)
        {
            text = initText;
        }
        public void OpenColorWindow()
        {
            Form2 form2 = new Form2(text);
            form2.ShowDialog();
        }
    };

And here is how form2 does work:

    string initText;
    public Form2(string initTextInput)
    {
        initText = initTextInput;
        InitializeComponent();
    }


private void InitializeComponent()
    {
        this.m_ComplexSyntaxer = new ComplexSyntaxer.ComplexSyntaxer();
        this.SuspendLayout();
        // 
        // m_ComplexSyntaxer
        // 
        this.m_ComplexSyntaxer.Dock = System.Windows.Forms.DockStyle.Fill;
        this.m_ComplexSyntaxer.Location = new System.Drawing.Point(0, 0);
        this.m_ComplexSyntaxer.Name = "m_ComplexSyntaxer";
        this.m_ComplexSyntaxer.Size = new System.Drawing.Size(292, 273);
        this.m_ComplexSyntaxer.TabIndex = 0;
        this.m_ComplexSyntaxer.Text = initText;
        this.m_ComplexSyntaxer.WordWrap = false;

        // THIS LINE DOES THE WORK
        this.m_ComplexSyntaxer.ProcessAllLines();
        // 
        // Form2
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292, 273);
        this.Controls.Add(this.m_ComplexSyntaxer);
        this.Name = "Form2";
        this.Text = "Form2";
        this.ResumeLayout(false);
    }

And here is where the work is done:

    public void ProcessAllLines()
    {
        int nStartPos = 0;
        int i = 0;
        int nOriginalPos = SelectionStart;
        while (i < Lines.Length)
        {
            m_strLine = Lines[i];
            m_nLineStart = nStartPos;
            m_nLineEnd = m_nLineStart + m_strLine.Length;
            m_nLineLength = m_nLineEnd - m_nLineStart;

            ProcessLine(); // This colors the current line!
            i++;

            nStartPos += m_strLine.Length + 1;
        }
        SelectionStart = nOriginalPos;
    }

Sooo, what is the “proper” way to open this form2 and have it report progress to Form1 to show to the user? (I ask because I was told this is not the right thing to do, I will get a “cross-thread” violation apparently?)

  • 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-09T20:57:58+00:00Added an answer on June 9, 2026 at 8:57 pm

    I want to open it in a thread because when it starts up I have it read and color syntax of 10k+ line long files which takes about 5 or 6 min.

    Why not read the data in a separate thread but then keep the UI itself on the same thread as the first form?

    In my experience if you use just one thread for UI operations, it makes life significantly simpler.

    As for how you should start a new thread to read the data, there are various options:

    • You could use BackgroundWorker; which would probably make the progress reporting simpler. See the MSDN article for more information.
    • Create a new Thread directly and start it
    • Use a thread-pool thread with ThreadPool.QueueUserWorkItem
    • If you’re using .NET 4, use Task.Factory.StartNew
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a windows application which has 3 forms : Form1,2,3. I want to
I have a windows application which has three forms : Form1,2,3. --Form 1, has
I have a windows-form which runs without any problem and displayed well. But if
I have a form MainForm which is a Windows Forms form that contains many
I have a windows form app, and a class which is called Game. Let's
(Modifying the question)I have a windows form app in C# which calls into some
I have a requirement to create a windows form control which has to detect
I have a form created in Windows Forms which is draggable wherever I click.
I have a program which displays an image to the windows form, and places
Im using c# .net , windows form application. I have a XML file which

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.