herb auld

herb auld

  • 1.7k
  • 26
  • 4.8k

C# datagridview backgroundworker update incomplete

Jan 21 2014 12:43 PM
 

my C# main form has many backgroundworker functions and the majority of the backgroundworkers are in the same form class.

I have one backgroundworker calling child class which is pass the backgroundworker object. for the most part it will successfully update the backgroundworker.progresschanged which one step is to update a datagridview (

case 0:
   msg = (string[])e.UserState;
   //tbStatus.Text = msg[0] + " = " + msg[1] + " > " + msg[2];
   dataGridView1.Rows.Add(msg[0], msg[1], msg[2], msg[3]);
   break;

).

as I stated it work for the most part. if loop through three iteration of this backgroundworker thread all but the last iteration will update the datagridview. according to the step through I have done I see the last iteration step through the backgroundworker.progresschange mention above. but it will not display in the datagrid on the form.

private void backgroundWorker6_ProgressChanged(object sender, ProgressChangedEventArgs e)

{

string[] msg = new string[4];

switch (e.ProgressPercentage)

{

case 0:

msg = (string[])e.UserState;

//tbStatus.Text = msg[0] + " = " + msg[1] + " > " + msg[2];

dataGridView1.Rows.Add(msg[0], msg[1], msg[2], msg[3]);

break;

case 1:

tbStatus.Text = (string)e.UserState;

break;

case 2:

tBResult.Text = (string)e.UserState;

break;

case 3:

string msg1 = "Processing file " + (string)e.UserState + " of " + tBN_Files.Text;

label7.Text = msg1;

break;

}

}//private void backgroundWorker6_ProgressChanged(object sender, ProgressChangedEventArgs e)

 the class call to the "progress change":

try

{

foreach (string filename in filenames)

{

Globals.dtStart = DateTime.Now;

if (Globals.strFilename.IndexOf("$FILE") >= 0)

{

workpath = Path.GetDirectoryName(filename);

sFilename = workpath + "\\" + "S4S-" + Globals.strFilename.Replace("$FILE", Path.GetFileName(filename));

}

else if (Globals.strFilename != "")

{

workpath = Path.GetDirectoryName(filename);

sFilename = workpath + "\\" + Globals.strFilename;

}

else if (Globals.strFilename == "")

sFilename = Application.StartupPath + @"\" + "Search4String.txt";

 

lFiles++;

bgWorker5.ReportProgress(3, lFiles.ToString());

workpath = Path.GetDirectoryName(filename);

FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

using (StreamReader sr = new StreamReader(fs))

{

string file = Path.GetFileName(filename);

//bgWorker.ReportProgress(1, "Processing file: " + file);

bgWorker5.ReportProgress(1, file);

while ((sData = sr.ReadLine()) != null)

{

lLines++;

if (lCollection == aCollection.Length - 25)

{

Array.Resize(ref aCollection, aCollection.Length + 500);

}

if (Globals.aKEYS.Length > 1)

{

for (int i = 0; i < Globals.aKEYS.Length; i++)

{

if (sData.IndexOf(Globals.aKEYS[i]) > 0)

{

GetCollection(sData, sArray, file, bgWorker5, e);

break;

}

}

}

else

{

GetCollection(sData, sArray, file, bgWorker5, e);

//break;

}

}//while ((sData = sr.ReadLine()) != null)

sr.Close();

PrintCollection(aCollection, lCollection, sFilename);

dtStop5 = DateTime.Now;

TimeSpan span = dtStop5.Subtract(Globals.dtStart);

sDuration = span.Hours.ToString("00") + ":" + span.Minutes.ToString("00") + ":" + span.Seconds.ToString("00") + "." + span.Milliseconds.ToString("000");

string[] result = new string[4];

result[0] = file;

result[1] = lLines.ToString("#,##0");

result[2] = iMatch.ToString("#,##0");

result[3] = sDuration;

lTotalLines += iMatch;

bgWorker5.ReportProgress(0, result);

bgWorker5.ReportProgress(2, lTotalLines.ToString());

lLines = 0;

iMatch = 0;

}//using (StreamReader sr = new StreamReader(filename))

}//foreach (string filename in filenames)

lFiles = 0;

}//try

the last iteration in the try loop is the one that fails the other (for example 3 files) two tries are ok. always the last update fails. I'm not sure why. stepping through (debug mode) shows it going to the process but the GRIDVIEW only has two files.