I recive the following error:
Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.
this is my code:
Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.
this is my code:
Code:
ParameterizedThreadStart Proceso = new ParameterizedThreadStart(this.ReadNPI);
start2 = new Thread(Proceso);
start2.IsBackground = true;
start2.Priority = ThreadPriority.Highest;
start2.Start(FilePath2);
Code:
private void ReadNPI(object FilePath)
{
string FILE = (string)FilePath;
string connectionString = cnSelected;
DataSet dataSet1 = new DataSet();
try
{
string selectString = "SELECT * FROM [" + sheet + "$]";
OleDbConnection con = new OleDbConnection(connectionString);
OleDbCommand cmd = new OleDbCommand(selectString, con);
con.Open();
OleDbDataReader theData = cmd.ExecuteReader();
List<string> al = new List<string>();
int c = theData.FieldCount;
while (theData.Read())
{
string NPI = theData.GetValue(0).ToString();
al.Add(NPI); //al es un ArrayList Publico.);
}
int count = al.Count;
this.toolStripProgressBar1.Minimum = 0;
this.toolStripProgressBar1.Maximum = count; // The error is HERE
this.toolStripProgressBar1.Step = 1;
con.Close();
try
{
string filepath = (string)FilePath; // FilePath es el archivo de NPPES.
Begin:
StreamReader sr = new StreamReader(filepath);
while (sr.Peek() != -1)
{
Back:
string line = sr.ReadLine();
string skip = line.Substring(1, 3);
if (skip == "NPI")
{
goto Back;
}
string[] fields = Regex.Split(line, "\",\"");
string NPI = fields.GetValue(0).ToString().Trim().Replace("\"", "");
if (al.Contains(NPI))
{
InsertData(fields);
al.Remove(NPI);
sr.Close();
sr.Dispose();
this.toolStripProgressBar1.PerformStep();
if (al.Count == 0)
{
toolStripStopThread.Visible = false;
TerminateThreads();
break;
}
goto Begin;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
TerminateThreads();
return;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
TerminateThreads();
return;
}
}
Comment