Click here to Skip to main content
15,893,663 members
Home / Discussions / C#
   

C#

 
Questionprocess in c# Pin
noamtzu009-Feb-10 23:19
noamtzu009-Feb-10 23:19 
AnswerRe: process in c# Pin
Rob Philpott9-Feb-10 23:30
Rob Philpott9-Feb-10 23:30 
AnswerRe: process in c# Pin
Calla9-Feb-10 23:52
Calla9-Feb-10 23:52 
AnswerRe: process in c# Pin
PIEBALDconsult10-Feb-10 3:26
mvePIEBALDconsult10-Feb-10 3:26 
QuestionSplit Data Table Records into several parts and assign to different Threads Pin
prabhakar CSPL9-Feb-10 22:47
prabhakar CSPL9-Feb-10 22:47 
QuestionThreading Pin
MumbleB9-Feb-10 22:20
MumbleB9-Feb-10 22:20 
AnswerRe: Threading Pin
OriginalGriff9-Feb-10 22:31
mveOriginalGriff9-Feb-10 22:31 
GeneralRe: Threading Pin
MumbleB9-Feb-10 22:43
MumbleB9-Feb-10 22:43 
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)
     {
         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()
 {
     //Declare Input File
     string infile;
     //Point to Input File
     infile = txtboxSelectFile.Text;
     //Declare output File
     string outfile;
     //Point to Output File
     outfile = saveFileDialog1.FileName;
     //Declare Lines of data
     string holdline;
     //Declare File Reader
     StreamReader sr = new StreamReader(infile);
     //Declare File Writer
     StreamWriter sw = new StreamWriter(outfile);

     //Sets the Control text
     setText(this, "Start Holder File Conversion");
     //Declare counter for number of holders on File
     int holdercount = 0;
     //Declare running Share Total
     Int64 currentbal = 0;
     //Declare running Old Balance Total
     Int64 oldbal = 0;
     //Declare Final Share Total
     long ShareTotal = 0;
     //Declare Final Old balance Total
     long ShareOld = 0;
     //Start Reading the Data
     while (!sr.EndOfStream)
     {
         holdline = sr.ReadLine();
         //Declare all Field variables
         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);
         ///Check for any holders that have a zero balance as these will
         ///not go onto the System unless dividend Data is Migrated.
             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()
 {
     //Declare Input File
     string infile;
     //Point to Input File
     infile = txtboxSelectFile.Text;
     //Declare output File
     string outfile;
     //Point to Output File
     outfile = saveFileDialog1.FileName;
     //Declare Lines of data
     string holdline;
     int totalCount = 0;
     //Declare File Reader
     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.

GeneralRe: Threading Pin
OriginalGriff9-Feb-10 23:25
mveOriginalGriff9-Feb-10 23:25 
GeneralRe: Threading Pin
MumbleB9-Feb-10 23:35
MumbleB9-Feb-10 23:35 
GeneralRe: Threading Pin
Luc Pattyn10-Feb-10 1:57
sitebuilderLuc Pattyn10-Feb-10 1:57 
QuestionLoading an assembly from a new AppDoamin Pin
ThetaClear9-Feb-10 20:28
ThetaClear9-Feb-10 20:28 
AnswerRe: Loading an assembly from a new AppDoamin Pin
Rob Philpott10-Feb-10 0:11
Rob Philpott10-Feb-10 0:11 
GeneralRe: Loading an assembly from a new AppDoamin Pin
ThetaClear10-Feb-10 9:53
ThetaClear10-Feb-10 9:53 
QuestionRSync Pin
satsumatable9-Feb-10 19:22
satsumatable9-Feb-10 19:22 
QuestionPreventing the dropdown from being opened when drop down button pressed in combobox? Pin
Ron.bharath9-Feb-10 18:53
Ron.bharath9-Feb-10 18:53 
AnswerRe: Preventing the dropdown from being opened when drop down button pressed in combobox? Pin
Hessam Jalali10-Feb-10 1:25
Hessam Jalali10-Feb-10 1:25 
QuestionSuspendLayout ResumeLayout Question Pin
Douglas Kirk9-Feb-10 18:52
Douglas Kirk9-Feb-10 18:52 
AnswerRe: SuspendLayout ResumeLayout Question Pin
Dave Kreskowiak10-Feb-10 2:07
mveDave Kreskowiak10-Feb-10 2:07 
AnswerRe: SuspendLayout ResumeLayout Question Pin
Luc Pattyn10-Feb-10 2:09
sitebuilderLuc Pattyn10-Feb-10 2:09 
QuestionRss Pin
kk.tvm9-Feb-10 18:21
kk.tvm9-Feb-10 18:21 
AnswerRe: Rss Pin
Arun Jacob9-Feb-10 19:13
Arun Jacob9-Feb-10 19:13 
AnswerRe: Rss Pin
Giorgi Dalakishvili9-Feb-10 19:32
mentorGiorgi Dalakishvili9-Feb-10 19:32 
QuestionIEnumerable Min() not working as expected Pin
Member 39190499-Feb-10 16:43
Member 39190499-Feb-10 16:43 
AnswerRe: IEnumerable Min() not working as expected Pin
Dave Kreskowiak10-Feb-10 2:02
mveDave Kreskowiak10-Feb-10 2:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.