From: Melina on
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;
}

}




From: Peter Duniho on
Melina wrote:
> 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 ?!

Since you didn't bother to post a concise-but-complete code example that
reliably reproduces the problem, it's not possible to say for sure.

But I'd bet that you are calling your runProduction() method from the
main GUI thread, thus blocking that thread from processing any window
messages, including those necessary to handle the Invoke() call.

It is very important to not block the main GUI thread. Don't do that.
Fix your code so that you don't need the runProduction() method to wait
for the threads to actually finish. It should start them and then
return right away.

If you have additional work to do after the processing has been
completed, you need to detect that situation in a different way. For
example, keep a count of the number of threads currently running. At
the end of each thread, decrement the count, and if the result is 0,
that thread can execute whatever end-of-processing code needs to happen
(which can happen either in that thread, or as a result of a call to
Invoke(), or both, as appropriate to your needs).

Pete