groupbox behavior
-
How do I make my groupBox wait until the process finishes?
private void button1_Click(object sender, EventArgs e) { groupBox1.Show(); button2.Hide(); int count=0; string oneline=""; if (!Directory.Exists(textBox1.Text)) Directory.CreateDirectory(textBox1.Text); // I want my groupBox to remain while this process continues. for (int i = 0; i < listBox1.Items.Count; i++) { oneline = listBox1.Items[i].ToString().ToLower(); if (oneline.EndsWith(".mts")) { string infile = label1.Text+ oneline; string outfile = textBox1.Text + oneline; File.Copy(infile,outfile,true); label2.Text= "Exporting: " + infile; count++; } } // I do not want the following to execute until the above has completed label3.Text = count + " Files Copied "; button2.Show(); } private void button2_Click_1(object sender, EventArgs e) { groupBox1.Hide(); }
-
How do I make my groupBox wait until the process finishes?
private void button1_Click(object sender, EventArgs e) { groupBox1.Show(); button2.Hide(); int count=0; string oneline=""; if (!Directory.Exists(textBox1.Text)) Directory.CreateDirectory(textBox1.Text); // I want my groupBox to remain while this process continues. for (int i = 0; i < listBox1.Items.Count; i++) { oneline = listBox1.Items[i].ToString().ToLower(); if (oneline.EndsWith(".mts")) { string infile = label1.Text+ oneline; string outfile = textBox1.Text + oneline; File.Copy(infile,outfile,true); label2.Text= "Exporting: " + infile; count++; } } // I do not want the following to execute until the above has completed label3.Text = count + " Files Copied "; button2.Show(); } private void button2_Click_1(object sender, EventArgs e) { groupBox1.Hide(); }
Since you're not using threads, I see no reason why label3.Text would be set before the loop is over. Have you used debugger to verify the logic. Also if you have some text in the label3, could it be from previous button click since you're not resetting the text in the beginning of this method.
The need to optimize rises from a bad design.My articles[^]
-
How do I make my groupBox wait until the process finishes?
private void button1_Click(object sender, EventArgs e) { groupBox1.Show(); button2.Hide(); int count=0; string oneline=""; if (!Directory.Exists(textBox1.Text)) Directory.CreateDirectory(textBox1.Text); // I want my groupBox to remain while this process continues. for (int i = 0; i < listBox1.Items.Count; i++) { oneline = listBox1.Items[i].ToString().ToLower(); if (oneline.EndsWith(".mts")) { string infile = label1.Text+ oneline; string outfile = textBox1.Text + oneline; File.Copy(infile,outfile,true); label2.Text= "Exporting: " + infile; count++; } } // I do not want the following to execute until the above has completed label3.Text = count + " Files Copied "; button2.Show(); } private void button2_Click_1(object sender, EventArgs e) { groupBox1.Hide(); }
Hi George, the way I understand your code, the following is going on: 1. as long as files are being copied your GUI is dead, since the GUI thread can't do useful things as long as it is busy executing button1_Click handler; that is a no no, an event handler should not take longer than the time a user is willing to wait, it should always remain responsive to window moves, window getting uncovered, etc, so each handler should run in less than say 30 msec, something yours is not guaranteed to do. 2. as a result, the label2 text updates do not show 3. however the label3 text update behaves as you would like it to occur: after all the file copying has been done. The solution to all the above is: 1. use a Thread or easier yet a BackgroundWorker to do most of what your Click routine does, so the Click routine just fires the BGW and returns immediately. 2. don't forget to apply whatever it takes to avoid cross-thread control operations (i.e. either Control.InvokeRequired/Control.Invoke when using a Thread, or using BGW.Progress when using a BGW). Furthermore you may want to add some try-catch logic around the File.Copy stuff since it may fail for one of several reasons (destination exists, disk full, whatever). Greetings,
Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused:
-
Hi George, the way I understand your code, the following is going on: 1. as long as files are being copied your GUI is dead, since the GUI thread can't do useful things as long as it is busy executing button1_Click handler; that is a no no, an event handler should not take longer than the time a user is willing to wait, it should always remain responsive to window moves, window getting uncovered, etc, so each handler should run in less than say 30 msec, something yours is not guaranteed to do. 2. as a result, the label2 text updates do not show 3. however the label3 text update behaves as you would like it to occur: after all the file copying has been done. The solution to all the above is: 1. use a Thread or easier yet a BackgroundWorker to do most of what your Click routine does, so the Click routine just fires the BGW and returns immediately. 2. don't forget to apply whatever it takes to avoid cross-thread control operations (i.e. either Control.InvokeRequired/Control.Invoke when using a Thread, or using BGW.Progress when using a BGW). Furthermore you may want to add some try-catch logic around the File.Copy stuff since it may fail for one of several reasons (destination exists, disk full, whatever). Greetings,
Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused:
TNX: Luc That will take me some time to digest but since the program is copying files from a camcorder video chip it could take possibly over one hour so I guess I will have to implement your suggestion. You always challenge this near senile old man. I guess I will have to do a quick run through the files and calculate the length of the files to implement the progress bar. I did find the Background Worker example code on the "Code Project" but it will take some time to peruse.
-
TNX: Luc That will take me some time to digest but since the program is copying files from a camcorder video chip it could take possibly over one hour so I guess I will have to implement your suggestion. You always challenge this near senile old man. I guess I will have to do a quick run through the files and calculate the length of the files to implement the progress bar. I did find the Background Worker example code on the "Code Project" but it will take some time to peruse.
Hi George, you're welcome. And I don't see you as a near senile old man, I rather see you as a man with many trades, who is experienced in programming and wants to keep up with ever evolving technologies. Which is great. BTW You don't have to implement a progress bar if you don't need one, the names of the BGW properties and events are only suggesting that you do, but you could simply set the label2 text in the Progress event, and the label3 text in the Completed event (both these events solve the cross-thread problem for you, since they fire on the GUI thread automatically). FYI: you might want to add a "Cancel" button and check the completion reason and adapt the text accordingly, e.g. adding a "Canceled" message if the Cancel button got pressed. Enjoy!
Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused:
-
Hi George, you're welcome. And I don't see you as a near senile old man, I rather see you as a man with many trades, who is experienced in programming and wants to keep up with ever evolving technologies. Which is great. BTW You don't have to implement a progress bar if you don't need one, the names of the BGW properties and events are only suggesting that you do, but you could simply set the label2 text in the Progress event, and the label3 text in the Completed event (both these events solve the cross-thread problem for you, since they fire on the GUI thread automatically). FYI: you might want to add a "Cancel" button and check the completion reason and adapt the text accordingly, e.g. adding a "Canceled" message if the Cancel button got pressed. Enjoy!
Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused:
Luc I got your suggestion working there are still a few rough spots but I am sure I will be able to get them resolved in time. I have several other programs that copy large quantities of multi media files and now that I understand the problem I will have to implement a "Background Worker" there also. TNX also TNX to Andrew Weiss for his well commented tutorial I reccomend it for anyone having similar problems with long running processes. http://www.codeproject.com/KB/cpp/BackgroundWorker_Threads.aspx[^]
-
Luc I got your suggestion working there are still a few rough spots but I am sure I will be able to get them resolved in time. I have several other programs that copy large quantities of multi media files and now that I understand the problem I will have to implement a "Background Worker" there also. TNX also TNX to Andrew Weiss for his well commented tutorial I reccomend it for anyone having similar problems with long running processes. http://www.codeproject.com/KB/cpp/BackgroundWorker_Threads.aspx[^]
Hi George, good for you. I didn't know the article you referred to, but it is quite good indeed. One thing it did not mention is you can pass an arbitrary object through the optional second parameter "UserState" of ReportProgress(); e.g. a progress string could be useful there. Cheers.
Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused: