Threading
-
Hi Guys. I have a slight problem. I have written an app that processes various types of files. I create a Thread for each type of file being processed depending on a checkbox being checked. Now, from each file processed I output totals to a label on the form. However, this only seems to work for the first two files being processed and the remainder does not get their totals output to the labels. Is this a known issue and is there a way I can get around this? The code is kinda long so don't really want to add any here now. But the process flow of the program is such. 1) User selects a searches for file to process. 2) User checks the appropriate checkbox. 3) Thread is started calling the block of code to run. 4) Once file is processed finalize by calling GC.Collect(). if user wants to process another file he unchecks the current chechbox, checks a new checkbox and searches for new file and above process is followed calling the appropriate Thread to run. Can somebody advise why when running more than two "Thread" processes in succession results in the third and fourth not outputting totals to the Label's?
Excellence is doing ordinary things extraordinarily well.
-
Hi Guys. I have a slight problem. I have written an app that processes various types of files. I create a Thread for each type of file being processed depending on a checkbox being checked. Now, from each file processed I output totals to a label on the form. However, this only seems to work for the first two files being processed and the remainder does not get their totals output to the labels. Is this a known issue and is there a way I can get around this? The code is kinda long so don't really want to add any here now. But the process flow of the program is such. 1) User selects a searches for file to process. 2) User checks the appropriate checkbox. 3) Thread is started calling the block of code to run. 4) Once file is processed finalize by calling GC.Collect(). if user wants to process another file he unchecks the current chechbox, checks a new checkbox and searches for new file and above process is followed calling the appropriate Thread to run. Can somebody advise why when running more than two "Thread" processes in succession results in the third and fourth not outputting totals to the Label's?
Excellence is doing ordinary things extraordinarily well.
Without some idea of the code it is really difficult to suggest anything, but there general things you can check: Are the thread running at all? Add some logging to a file to record what is being done, and in what order. Are the output overwriting each other? Try logging the outputs as well. Are the thread having problems with conflicts? Are there any locks which might be waiting? Is it always the same threads that don't run? Or does that change depending on what order they are started in, say? Sorry not to give concrete answers, but there is a heck of a lot that could be going wrong here - I think your first call is to gather more info!
If Barbie is so popular, why do you have to buy her friends? Eagles may soar, but weasels don't get sucked into jet engines. If at first you don't succeed, destroy all evidence that you tried.
-
Without some idea of the code it is really difficult to suggest anything, but there general things you can check: Are the thread running at all? Add some logging to a file to record what is being done, and in what order. Are the output overwriting each other? Try logging the outputs as well. Are the thread having problems with conflicts? Are there any locks which might be waiting? Is it always the same threads that don't run? Or does that change depending on what order they are started in, say? Sorry not to give concrete answers, but there is a heck of a lot that could be going wrong here - I think your first call is to gather more info!
If Barbie is so popular, why do you have to buy her friends? Eagles may soar, but weasels don't get sucked into jet engines. If at first you don't succeed, destroy all evidence that you tried.
Hi Griff. The Threads are all running. Output is created from all the Threads. However, I display some totals on screen for the user to verify against. It will output the totals for two of the threads run after each other, but when running the third and fourth thread I get no totals output to screen. I have tested by adding the totals to a messagebox after it is supposed to be output to the label and that works. Below is the code. It might be long but this is what I have.
#region Set Text Code public delegate void SetText(Control ctrl, string str); public delegate void SetLabel(Control lblctrl, string filename); private delegate void updateBar(); private void setText(Control ctrl, string str) { if (this.WindowState == FormWindowState.Minimized) return; if (ctrl.InvokeRequired) ctrl.BeginInvoke(new SetText(setText), new object\[\] { ctrl, str }); else ctrl.Text = str; } private void setFilename(Control lblctrl, string filename) { if (this.WindowState == FormWindowState.Minimized) return; if (lblctrl.InvokeRequired) lblctrl.BeginInvoke(new SetLabel(setFilename), new object\[\] { lblctrl, filename }); else lblctrl.Text = filename; } private void finalizeProcess() { //progressBar1.Style = ProgressBarStyle.Blocks; //progressBar1.Value = 0; //progressBar1.Dispose(); GC.Collect(); //base.Close(); } #endregion #region Open File Dialog Code private void button3\_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() != DialogResult.OK) { return; } else { txtboxSelectFile.Text = openFileDialog1.FileName; } } #endregion #region Select File Code private void btnStart\_Click(object sender, EventArgs e) { setText(this, "Checking Process Selection"); if (chkboxHolders.CheckState == CheckState.Unchecked && chkboxCert.CheckState == CheckState.Unchecked && chkboxPiadd.CheckState == CheckState.Unchecked && chkboxDivRecords.CheckState == CheckState.Unchecked) {
-
Hi Griff. The Threads are all running. Output is created from all the Threads. However, I display some totals on screen for the user to verify against. It will output the totals for two of the threads run after each other, but when running the third and fourth thread I get no totals output to screen. I have tested by adding the totals to a messagebox after it is supposed to be output to the label and that works. Below is the code. It might be long but this is what I have.
#region Set Text Code public delegate void SetText(Control ctrl, string str); public delegate void SetLabel(Control lblctrl, string filename); private delegate void updateBar(); private void setText(Control ctrl, string str) { if (this.WindowState == FormWindowState.Minimized) return; if (ctrl.InvokeRequired) ctrl.BeginInvoke(new SetText(setText), new object\[\] { ctrl, str }); else ctrl.Text = str; } private void setFilename(Control lblctrl, string filename) { if (this.WindowState == FormWindowState.Minimized) return; if (lblctrl.InvokeRequired) lblctrl.BeginInvoke(new SetLabel(setFilename), new object\[\] { lblctrl, filename }); else lblctrl.Text = filename; } private void finalizeProcess() { //progressBar1.Style = ProgressBarStyle.Blocks; //progressBar1.Value = 0; //progressBar1.Dispose(); GC.Collect(); //base.Close(); } #endregion #region Open File Dialog Code private void button3\_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() != DialogResult.OK) { return; } else { txtboxSelectFile.Text = openFileDialog1.FileName; } } #endregion #region Select File Code private void btnStart\_Click(object sender, EventArgs e) { setText(this, "Checking Process Selection"); if (chkboxHolders.CheckState == CheckState.Unchecked && chkboxCert.CheckState == CheckState.Unchecked && chkboxPiadd.CheckState == CheckState.Unchecked && chkboxDivRecords.CheckState == CheckState.Unchecked) {
Without actually running it, I can't see anything obviously wrong. Since this is the code that (doesn't) show the problem, I would be tempted to try
private void setFilename(Control lblctrl, string filename) { Console.WriteLine("setFileName Entered: " + lblCtrl.Name + " : " + filename); if (this.WindowState == FormWindowState.Minimized) return; Console.WriteLine("setFileName PreInvokeCheck: " + lblCtrl.Name + " : " + filename); if (lblctrl.InvokeRequired) { Console.WriteLine("setFileName BeginInvoke: " + lblCtrl.Name + " : " + filename); lblctrl.BeginInvoke(new SetLabel(setFilename), new object\[\] { lblctrl, filename }); } else { Console.WriteLine("setFileName Direct: " + lblCtrl.Name + " : " + filename); lblctrl.Text = filename; } }
Or equivilent to check if it gets to the "setFileName Direct" bit when you don't get the label written. It may give a clue to what is going on (or worse, remove the problem)
If Barbie is so popular, why do you have to buy her friends? Eagles may soar, but weasels don't get sucked into jet engines. If at first you don't succeed, destroy all evidence that you tried.
-
Without actually running it, I can't see anything obviously wrong. Since this is the code that (doesn't) show the problem, I would be tempted to try
private void setFilename(Control lblctrl, string filename) { Console.WriteLine("setFileName Entered: " + lblCtrl.Name + " : " + filename); if (this.WindowState == FormWindowState.Minimized) return; Console.WriteLine("setFileName PreInvokeCheck: " + lblCtrl.Name + " : " + filename); if (lblctrl.InvokeRequired) { Console.WriteLine("setFileName BeginInvoke: " + lblCtrl.Name + " : " + filename); lblctrl.BeginInvoke(new SetLabel(setFilename), new object\[\] { lblctrl, filename }); } else { Console.WriteLine("setFileName Direct: " + lblCtrl.Name + " : " + filename); lblctrl.Text = filename; } }
Or equivilent to check if it gets to the "setFileName Direct" bit when you don't get the label written. It may give a clue to what is going on (or worse, remove the problem)
If Barbie is so popular, why do you have to buy her friends? Eagles may soar, but weasels don't get sucked into jet engines. If at first you don't succeed, destroy all evidence that you tried.
Thanks mate. I will give it a go and post back. It is just strange that it does it for two consecutive runs but for the third and fourth it doesn't. And it doesn't matter in what order it is run or not.
Excellence is doing ordinary things extraordinarily well.
-
Hi Griff. The Threads are all running. Output is created from all the Threads. However, I display some totals on screen for the user to verify against. It will output the totals for two of the threads run after each other, but when running the third and fourth thread I get no totals output to screen. I have tested by adding the totals to a messagebox after it is supposed to be output to the label and that works. Below is the code. It might be long but this is what I have.
#region Set Text Code public delegate void SetText(Control ctrl, string str); public delegate void SetLabel(Control lblctrl, string filename); private delegate void updateBar(); private void setText(Control ctrl, string str) { if (this.WindowState == FormWindowState.Minimized) return; if (ctrl.InvokeRequired) ctrl.BeginInvoke(new SetText(setText), new object\[\] { ctrl, str }); else ctrl.Text = str; } private void setFilename(Control lblctrl, string filename) { if (this.WindowState == FormWindowState.Minimized) return; if (lblctrl.InvokeRequired) lblctrl.BeginInvoke(new SetLabel(setFilename), new object\[\] { lblctrl, filename }); else lblctrl.Text = filename; } private void finalizeProcess() { //progressBar1.Style = ProgressBarStyle.Blocks; //progressBar1.Value = 0; //progressBar1.Dispose(); GC.Collect(); //base.Close(); } #endregion #region Open File Dialog Code private void button3\_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() != DialogResult.OK) { return; } else { txtboxSelectFile.Text = openFileDialog1.FileName; } } #endregion #region Select File Code private void btnStart\_Click(object sender, EventArgs e) { setText(this, "Checking Process Selection"); if (chkboxHolders.CheckState == CheckState.Unchecked && chkboxCert.CheckState == CheckState.Unchecked && chkboxPiadd.CheckState == CheckState.Unchecked && chkboxDivRecords.CheckState == CheckState.Unchecked) {
Hi, you are not supposed to touch a Control from a thread that did not create the Control. While your code is using several InvokeRequired/Invoke patterns, it is also not using them in several locations, see: 1.
if (this.WindowState == FormWindowState.Minimized)
is probably OK, but not documented to be OK 2.infile = txtboxSelectFile.Text;
is probably not OK (not documented, I guess it is not OK for Controls that allow text editing) 3.outfile = saveFileDialog1.FileName;
is most likely not OK, file dialogs are known to be critical (e.g. you need STAThread for them) IMO you should fix all of the above. :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.