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()
{
GC.Collect();
}
#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)
{
MessageBox.Show("Please make a valid processing selection", "Invalid Process Selection",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
if (chkboxHolders.Checked)
{
setText(this, "Holder File Processing Selected");
processHolders();
}
if (chkboxCert.Checked)
{
setText(this, "Certificate File Processing Selected");
processCerts();
}
if (chkboxPiadd.Checked)
{
setText(this, "Payment Instruction File Processing Selected");
processDiv();
}
if (chkboxDivRecords.Checked)
{
setText(this, "Create Dividend Files");
processDividends();
}
}
}
#endregion
#region All Threading Code
private void processHolders()
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
ThreadStart starthld = new ThreadStart(convertHolders);
Thread threadhld = new Thread(starthld);
threadhld.Start();
}
}
private void processCerts()
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
ThreadStart startcert = new ThreadStart(convertCert);
Thread threadcert = new Thread(startcert);
threadcert.Start();
}
}
private void processDiv()
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
ThreadStart startdiv = new ThreadStart(convertDiv);
Thread threaddiv = new Thread(startdiv);
threaddiv.Start();
}
}
private void processDividends()
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
ThreadStart startdivs = new ThreadStart(convertDividendPayments);
Thread threaddivs = new Thread(startdivs);
threaddivs.Start();
}
}
#endregion
#region All Processing Code
private void convertHolders()
{
string infile;
infile = txtboxSelectFile.Text;
string outfile;
outfile = saveFileDialog1.FileName;
string holdline;
StreamReader sr = new StreamReader(infile);
StreamWriter sw = new StreamWriter(outfile);
setText(this, "Start Holder File Conversion");
int holdercount = 0;
Int64 currentbal = 0;
Int64 oldbal = 0;
long ShareTotal = 0;
long ShareOld = 0;
while (!sr.EndOfStream)
{
holdline = sr.ReadLine();
string HID = holdline.Substring(0, 10);
string Title = holdline.Substring(10, 45).Trim();
string Surname = holdline.Substring(55, 100).Trim();
string FirstName = holdline.Substring(155, 100).Trim();
string OtherNames = holdline.Substring(255, 100).TrimEnd();
string Gender = holdline.Substring(355, 1);
string Addr1 = holdline.Substring(356, 35).TrimEnd();
string Addr2 = holdline.Substring(396, 35).TrimEnd();
string Addr3 = holdline.Substring(436, 35).TrimEnd();
string Addr4 = holdline.Substring(476, 35).TrimEnd();
string Addr5 = holdline.Substring(516, 35).TrimEnd();
string PostCode = holdline.Substring(556, 10).TrimEnd();
string Nationality = holdline.Substring(566, 25).TrimEnd();
string BranchCode = holdline.Substring(591, 6);
string AccountNumber = holdline.Substring(597, 16);
string AccountType = holdline.Substring(613, 2);
string NationalId = holdline.Substring(615, 16).TrimEnd();
string TotalShares = holdline.Substring(631, 18);
string FullName = Title + " " + FirstName + " " + Surname;
int namelen = FullName.Length;
int namerem = namelen - 35;
currentbal = Convert.ToInt64(TotalShares);
string OldBalance = holdline.Substring(649, 18);
oldbal = Convert.ToInt64(OldBalance);
setText(this, "Processing HolderID: " + HID);
if(namelen > 35)
{
sw.Write(" ".PadRight(5, ' ') + "NAADD" + " ".PadRight(7, ' ') + "C" + HID +
FullName.Substring(0, 35).PadRight(35, ' ') + FullName.Substring(35, namerem).PadRight(35, ' ') +
Addr1.PadRight(35, ' ') + Addr2.PadRight(35, ' ') + Addr3.PadRight(35, ' ') + Addr4.PadRight(35, ' ') +
Addr5.PadRight(35, ' ') + " ".PadRight(35, ' ') + PostCode.PadRight(5, ' ') + "ZAF" +
"IND" + " ".PadRight(57, ' ') + HID.PadRight(12, ' ') + " ".PadRight(13, ' '));
}
else if (namelen <= 35)
{
sw.Write(" ".PadRight(5, ' ') + "NAADD" + " ".PadRight(7, ' ') + "C" + HID + FullName.Trim().PadRight(35, ' ') +
Addr1.PadRight(35, ' ') + Addr2.PadRight(35, ' ') + Addr3.PadRight(35, ' ') + Addr4.PadRight(35, ' ') +
Addr5.PadRight(35, ' ') + " ".PadRight(70, ' ') + PostCode.PadRight(5, ' ') + "ZAF" +
"IND" + " ".PadRight(57, ' ') + HID.PadRight(12, ' ') + " ".PadRight(13, ' '));
}
if (NationalId == "" || NationalId.Length < 13)
{
sw.Write(" ".PadRight(1142, ' ') + " ".PadRight(1456, ' ') + Environment.NewLine);
}
else
{
sw.Write("Z" + NationalId.PadRight(16, ' ') + " ".PadRight(1125, ' ') + " ".PadRight(1456, ' ') + Environment.NewLine);
}
ShareTotal = ShareTotal + currentbal;
ShareOld = ShareOld + oldbal;
holdercount++;
}
sr.Close();
sw.Close();
setFilename(lblShareTotals, ShareTotal.ToString());
setFilename(lblHolderTotal, holdercount.ToString());
setFilename(lblOldBalanceTotals, ShareOld.ToString());
this.progressBar1.Invoke(new updateBar(this.finalizeProcess));
MessageBox.Show("Completed Processing Holder File", "Holder File Completed",
MessageBoxButtons.OK, MessageBoxIcon.Information);
setText(this, "Holder File Processing Completed");
}
private void convertCert()
{
string infile;
infile = txtboxSelectFile.Text;
string outfile;
outfile = saveFileDialog1.FileName;
string holdline;
Int64 currentbal = 0;
long ShareTotal = 0;
int totaltrans = 0;
StreamReader sr = new StreamReader(infile);
StreamWriter sw = new StreamWriter(outfile);
setText(this, "Start Certificate File Conversion");
while (!sr.EndOfStream)
{
holdline = sr.ReadLine();
string HID = holdline.Substring(0, 10);
string day = holdline.Substring(10, 2);
string month = holdline.Substring(13, 2);
string year = holdline.Substring(16, 4);
string cert = holdline.Substring(20, 10).Trim();
string transaction = holdline.Substring(30, 55).TrimEnd();
string shares = holdline.Substring(85, 18);
setText(this, "Processing HolderID: " + HID);
if (chkoxCreateCerts.Checked)
{
if (shares != "000000000000000000")
{
sw.WriteLine(" ".PadRight(5, ' ') + "CAPIN" + " ".PadRight(8, ' ') +
day + month + year.Substring(2, 2) + "+" + shares.Substring(4, 14) + "000000" +
"ORD" + "ZAF" + " ".PadRight(5, ' ') + "C" + HID + " ".PadRight(10, ' ') +
cert.PadLeft(12, '0') + " ".PadRight(2911, ' '));
currentbal = Convert.ToInt64(shares);
ShareTotal = ShareTotal + currentbal;
}
}
else
{
if (shares != "000000000000000000")
{
sw.WriteLine(" ".PadRight(5, ' ') + "ADJIN" + " ".PadRight(8, ' ') +
day + month + year.Substring(2, 2) + "+" + shares.Substring(4, 14) + "000000" +
"ORD" + "ZAF" + " ".PadRight(5, ' ') + "C" + HID + " ".PadRight(10, ' ') +
cert.PadLeft(12, '0') + " ".PadRight(2911, ' '));
currentbal = Convert.ToInt64(shares);
ShareTotal = ShareTotal + currentbal;
}
}
totaltrans++;
}
setFilename(lblCertificateTotals, totaltrans.ToString());
setFilename(lblCertificateBalance, ShareTotal.ToString());
sr.Close();
sw.Close();
this.progressBar1.Invoke(new updateBar(this.finalizeProcess));
MessageBox.Show("Completed Processing Certificate File", "Certificate File Completed",
MessageBoxButtons.OK, MessageBoxIcon.Information);
setText(this, "Certificate File Processing Completed");
}
private void convertDiv()
{
string infile;
infile = txtboxSelectFile.Text;
string outfile;
outfile = saveFileDialog1.FileName;
string holdline;
int totalCount = 0;
StreamReader sr = new StreamReader(infile);
StreamWriter swb = new StreamWriter(outfile);
setText(this, "Start Payment Instruction Conversion");
while (!sr.EndOfStream)
{
holdline = sr.ReadLine();
string HID = holdline.Substring(0, 10);
string BranchCode = holdline.Substring(591, 6);
string AccountNumber = holdline.Substring(597, 16);
string shares = holdline.Substring(85, 18);
string newBranch = BranchCode.Substring(0, 3) + "-" + BranchCode.Substring(3, 3);
setText(this, "Processing HolderID: " + HID);
if (BranchCode != " ")
{
if (shares != "000000000000000000")
{
swb.WriteLine(" ".PadRight(5, ' ') + "PIADD" + " ".PadRight(7, ' ') + "C" + HID + "10" +
"D" + " " + newBranch.PadRight(10, ' ') + AccountNumber.PadRight(16, ' ') + " ".PadRight(2940, ' '));
totalCount++;
}
}
}
sr.Close();
swb.Close();
setFilename(lblPaymentInstrucTotals, totalCount.ToString());
this.progressBar1.Invoke(new updateBar(this.finalizeProcess));
MessageBox.Show("Completed PIADD Conversion", "PIADD Conversion Message",
MessageBoxButtons.OK, MessageBoxIcon.Information);
setText(this, "Payment Instruction File Processing Completed");
}
private void convertDividendPayments()
{
string filePath;
string inFile;
string holdLine;
inFile = txtboxSelectFile.Text;
filePath = folderBrowserDialog1.SelectedPath;
long TotalGross = 0;
long TotalTax = 0;
long TotalNet = 0;
Int64 gross = 0;
Int64 tax = 0;
Int64 net = 0;
StreamReader sr = new StreamReader(inFile);
StreamWriter swp = new StreamWriter(Path.Combine(filePath, "er_au_xxxx_seqmpd.temp"));
setText(this, "Starting Payment File Conversions");
while (!sr.EndOfStream)
{
holdLine = sr.ReadLine();
string DividendNumber = holdLine.Substring(0, 9).TrimEnd();
string CADescription = holdLine.Substring(9, 55);
string HolderRef = holdLine.Substring(64, 10);
string PaymentType = holdLine.Substring(74, 1);
string PaymentRef = holdLine.Substring(75, 10);
string LdrBal = holdLine.Substring(85, 18);
string GrossAmount = holdLine.Substring(103, 18);
string TaxAmount = holdLine.Substring(121, 18);
string NetAmount = holdLine.Substring(139, 18);
string PaymentStatus = holdLine.Substring(157, 1);
string PaymentDate = holdLine.Substring(158, 10);
gross = Convert.ToInt64(GrossAmount);
tax = Convert.ToInt64(TaxAmount);
net = Convert.ToInt64(NetAmount);
setText(this, "Processing HolderID: " + HolderRef);
swp.Write("C" + HolderRef + "DIV" + DividendNumber + PaymentRef.Substring(2, 8).PadLeft(8, '0'));
if (PaymentType == "E")
{
swp.Write("D" + PaymentDate.Substring(0, 2) + PaymentDate.Substring(3, 2) + PaymentDate.Substring(6, 4));
}
else
{
swp.Write(PaymentType + " ".PadRight(8, ' '));
}
swp.Write(GrossAmount.Substring(7, 11) + "00" + TaxAmount.Substring(7, 11) + "00" + NetAmount.Substring(7, 11) + "00" + "S");
if (PaymentStatus == "P")
{
swp.Write("2");
}
if (PaymentStatus == "C")
{
swp.Write("3");
}
if (PaymentStatus == "U")
{
swp.Write("1");
}
if (PaymentStatus == "R")
{
swp.Write("E");
}
swp.Write("R" + " ".PadRight(24, ' ') + Environment.NewLine);
TotalGross = TotalGross + gross;
TotalTax = TotalTax + tax;
TotalNet = TotalNet + net;
}
sr.Close();
swp.Close();
setFilename(lblGrossTotals, TotalGross.ToString());
setFilename(lblTaxTotals, TotalTax.ToString());
setFilename(lblNetTotals, TotalNet.ToString());
this.progressBar1.Invoke(new updateBar(this.finalizeProcess));
MessageBox.Show("Completed Dividend Payment Processing", "Dividend Processing",
MessageBoxButtons.OK, MessageBoxIcon.Information);
setText(this, "Dividend File Processing Completed");
}
#endregion
Excellence is doing ordinary things extraordinarily well.
|