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?)
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:
BackgroundWorker; which would probably make the progress reporting simpler. See the MSDN article for more information.Threaddirectly and start itThreadPool.QueueUserWorkItemTask.Factory.StartNew