M
Melina
Hi,
I just want to run several threads at the same time.
The code below just hang because - I guess - it can't access the
TextBox.Text method.
What is wrong with tis code ?!
Thanks.
Melina.
public class Report
{
public TextBox _log;
private List<RptTemplate> _listeTemplate;
private List<CIF> _listeCIF;
public Report(TextBox logTextBox, List<RptTemplate> listeTemplate,
List<CIF> listeCIF)
{
_log = logTextBox;
_listeTemplate = listeTemplate;
_listeCIF = listeCIF;
}
public void runProduction()
{
List<Thread> threads = new List<Thread>();
foreach (CIF ptf in _listeCIF)
{
Thread aThread = new Thread(delegate() { createPdf(ptf); });
aThread.Start();
threads.Add(aThread);
}
foreach (Thread thread in threads)
thread.Join();
threads.Clear();
}
private void createPdf(CIF ptf)
{
UpdateTextBox("Working on: CIF n°" + ptf.Racine + "\r\n");
foreach (RptTemplate rpt in _listeTemplate)
{
UpdateTextBox("\tWorking on: CIF n°" + ptf.Racine + " --> "
+ rpt.Name + "\r\n");
}
}
delegate void OutputUpdateDelegate(string data);
public void UpdateTextBox(string data)
{
if (_log.InvokeRequired)
_log.Invoke(new OutputUpdateDelegate(OutputUpdateCallback),
new object[] { data });
else
OutputUpdateCallback(data);
}
private void OutputUpdateCallback(string data)
{
_log.Text += data;
}
}
I just want to run several threads at the same time.
The code below just hang because - I guess - it can't access the
TextBox.Text method.
What is wrong with tis code ?!
Thanks.
Melina.
public class Report
{
public TextBox _log;
private List<RptTemplate> _listeTemplate;
private List<CIF> _listeCIF;
public Report(TextBox logTextBox, List<RptTemplate> listeTemplate,
List<CIF> listeCIF)
{
_log = logTextBox;
_listeTemplate = listeTemplate;
_listeCIF = listeCIF;
}
public void runProduction()
{
List<Thread> threads = new List<Thread>();
foreach (CIF ptf in _listeCIF)
{
Thread aThread = new Thread(delegate() { createPdf(ptf); });
aThread.Start();
threads.Add(aThread);
}
foreach (Thread thread in threads)
thread.Join();
threads.Clear();
}
private void createPdf(CIF ptf)
{
UpdateTextBox("Working on: CIF n°" + ptf.Racine + "\r\n");
foreach (RptTemplate rpt in _listeTemplate)
{
UpdateTextBox("\tWorking on: CIF n°" + ptf.Racine + " --> "
+ rpt.Name + "\r\n");
}
}
delegate void OutputUpdateDelegate(string data);
public void UpdateTextBox(string data)
{
if (_log.InvokeRequired)
_log.Invoke(new OutputUpdateDelegate(OutputUpdateCallback),
new object[] { data });
else
OutputUpdateCallback(data);
}
private void OutputUpdateCallback(string data)
{
_log.Text += data;
}
}