I have multiline textbox which I can get the value for each line (as well as index value) using;
public void doProcess()
{
if (!string.IsNullOrEmpty(Convert.ToString(deliverynumbers.Text)))
{
string _deliverynumbers = deliverynumbers.Text;
string[] delimiter = {Environment.NewLine};
string[] array_deliverynumbers = _deliverynumbers.Split(delimiter, StringSplitOptions.None);
int count = array_deliverynumbers.Length;
int current = 0;
foreach (string text in array_deliverynumbers)
{
if (!string.IsNullOrEmpty(Convert.ToString(text)))
{
current++;
Double Total_Percentage = count;
Double Current_Percentage = current;
Double progressAmount = (Current_Percentage / Total_Percentage * 100);
//backgroundWorker.ReportProgress(Convert.ToInt32(progressAmount));
//could use this to update progress bar
//progress.Value = progressAmount;
//Do some other processes like for each line, create a file and name it to the value of the line value...
}
}
}
}
However when I try the doProgress() inside a DoWork call I get "A first chance exception of type 'System.InvalidOperationException'...below is the DoWork code;
private void DoWork(object sender, DoWorkEventArgs e)
{
//This works
for (int i = 0; i <= 100; i++)
{
Thread.Sleep(100);
backgroundWorker.ReportProgress(i);
}
//But this causes the exception
doProgress();
}
I have even removed various parts of the doProgress() code to see what works but none of it does...
EDIT
even if(!string.IsNullOrEmpty(Convert.ToString(deliverynumbers.Text))) {} breaks it..
private void DoWork(object sender, DoWorkEventArgs e)
{
if (!string.IsNullOrEmpty(Convert.ToString(deliverynumbers.Text)))
{
}
}
Got it working...as per below..
basically this takes values from 2 textboxes (one is a path value and one is a file path value) & one multiline textbox and for each line in the multiline textbox, copy the file(file path textbox value) to the folder path(path textbox value) and rename the copied file to the value of the line(from multiline Textbox).
To put it simply this mass copies and renames a file to the selected folder, from the list in the multiline Textbox
My onclick event
public void begin_process_Click(object sender, RoutedEventArgs e)
{
//Hide Form
form_process.Visibility = Visibility.Hidden;
//Show Loader (with progress bar)
loader.Visibility = Visibility.Visible;
//Get Multiline Text Box Value
string _deliverynumbers = deliverynumbers.Text;
//Get source file path
string sourceFile = file_path.Text;
//Get copy to path
string destinationPath = share_drive_folder_path.Text;
//Create object to pass through backgroundWorker.RunWorkerAsync(args);
Object[] args = { _deliverynumbers, sourceFile, destinationPath };
//pass "args" through..
backgroundWorker.RunWorkerAsync(args);
}
and in my DoWork() Method - still need exception catching and additional folder & file checks etc
private void DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
//Get arguments we passed in onclick event
Object[] arg = e.Argument as Object[];
//Do stuff for multiline value - which is via (string)arg[0]
string[] delimiter = { Environment.NewLine };
string _deliverynumbers = (string)arg[0];
string[] array_deliverynumbers = _deliverynumbers.Split(delimiter, StringSplitOptions.None);
//Generate Indexing
int count = array_deliverynumbers.Length;
int current = 0;
//ForEach Line in multiline TextBox
foreach (string text in array_deliverynumbers)
{
if (!string.IsNullOrEmpty(Convert.ToString(text)))
{
//Update Indexing
current++;
//Determine Progress bar percentage
Double Total_Percentage = count;
Double Current_Percentage = current;
Double progressAmount = (Current_Percentage / Total_Percentage * 100);
//Copy source file for each line in TextBox & rename new copied file to the line value & source file format
string new_filename = text.Substring(2, text.Length - 2);
string sourceFile = (string)arg[1];
string destinationPath = (string)arg[2];
string destinationFileName = new_filename + System.IO.Path.GetExtension(sourceFile);
string destinationFile = System.IO.Path.Combine(destinationPath, destinationFileName);
//Check Folder exists
if (!System.IO.Directory.Exists(destinationPath))
{
System.IO.Directory.CreateDirectory(destinationPath);
}
//Copy & Move and rename
System.IO.File.Copy(sourceFile, destinationFile, true);
//Sleep for a bit incase processing is to quick
Thread.Sleep(100);
//Update progress bar with new amount
backgroundWorker.ReportProgress(Convert.ToInt32(progressAmount));
}
}
}