Thread

G

Guest

OK, I am trying to use a thread for the first time. I did not realize that
when you create a thread that you specifically have to go into the debugger
and identify the thread you wanted to look at. So now I have done that and
the first instruction of the method which is invoked by the Thread.Start,
instantiates a class which preforms a lot of processing. Upon it's return, I
want to set the message of my win form to indicate that the process completed
successfully and then turn on some buttons on the win form.

Well, I can follow in the debugger all the instructions of the class which
is instantiated, but when it should come back to the next instruction of the
method in the Thread.start, the debugger does not take me back to that
instruction. Is it legal for me to manipulate the win form from the
Thread.start method?

Here is the snipit of the code.

private void GenerateComboEditHtmlPages()
{
// Instantiate the class that does the processing
ComboEditReport editReport = new ComboEditReport(_HtmlOutputLoc,
_RuleDescLoc, _Environment, _CoverPage);
//Turn back on the win form command button
cmdRun.Enabled = true;
//Test if the process was successful and place the appropriate msg into
the label
if (editReport.ProcessSuccessful)
{ lblStatus.Text = "The request completed successfully"; }
else
{ lblStatus.Text = " **** The request was unsucessful *****" +
Environment.NewLine + "Detail Error Msg: " + editReport.Messages; }
panel1.Visible = true;
}
}

Thanks in advance for your assistance!!!
 
N

Nicholas Paldino [.NET/C# MVP]

No, it isn't. When you perform work that affects controls on a UI
thread, you have to make calls that change those controls from the same UI
thread. You can use the Invoke method of a control in order to make sure
that a delegate is performed on the UI thread. In this case, assuming that
GenerateComboEditHtmlPages is your method that gets called, you can do this:

private delegate void UpdateaUiThreadCallback();

private void GenerateComboEditHtmlPages()
{
// Instantiate the class that does the processing
ComboEditReport editReport = new ComboEditReport(_HtmlOutputLoc,
_RuleDescLoc, _Environment, _CoverPage);

// Invoke on the UI thread.
cmdRun.Invoke(new UpdateUiThreadCallback(UpdateUiThread), null);
}

private void UpdateUiThread()
{
//Turn back on the win form command button
cmdRun.Enabled = true;

//Test if the process was successful and place the appropriate msg into
the label
if (editReport.ProcessSuccessful)
{
lblStatus.Text = "The request completed successfully";
}
else
{
lblStatus.Text = " **** The request was unsucessful *****" +
Environment.NewLine + "Detail Error Msg: " + editReport.Messages;
}

panel1.Visible = true;
}
 
N

Nicholas Paldino [.NET/C# MVP]

I need to point out that the code will not work as compiled, it needs to
be changed to this in order to run (as it doesn't have a reference to
editReport):

private delegate void UpdateaUiThreadCallback(ComboEditReport report);

private void GenerateComboEditHtmlPages()
{
// Instantiate the class that does the processing
ComboEditReport editReport = new ComboEditReport(_HtmlOutputLoc,
_RuleDescLoc, _Environment, _CoverPage);

// Invoke on the UI thread.
cmdRun.Invoke(new UpdateUiThreadCallback(UpdateUiThread), new
object[]{ editReport });
}

private void UpdateUiThread()
{
//Turn back on the win form command button
cmdRun.Enabled = true;

//Test if the process was successful and place the appropriate msg into
the label
if (report.ProcessSuccessful)
{
lblStatus.Text = "The request completed successfully";
}
else
{
lblStatus.Text = " **** The request was unsucessful *****" +
Environment.NewLine + "Detail Error Msg: " + report.Messages;
}

panel1.Visible = true;
}

This also assumes that all the work is done in the constructor, and the
calls to ProcessSuccessful and Messages on the ComboEditReport instance are
not doing any heavy lifting.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top