Threading

G

Guest

Trying my first project with a thread. I have a windows application in which
I want to call this class which does a bunch of processing using a thread.
My application validates the values supplied on the input and when all is
well, it displays a message on the screen to indicate that the process is
underway and then it creates the thread.

So when I place a Debug stop on the method I call ThreadStart(MyMethod), the
debugger stops on the line I have designated, but when I press the key to
navigate to the next instruction to process, nothing happens. The line I am
stopped on instantiates a class which does all of the processing. I can't
step into that process and it never comes back from that process. Here is a
snipit of the code.....

private void cmdRun_Click(object sender, EventArgs e)
{
Thread oThread = new Thread(new ThreadStart(GenerateComboEditHtmlPages)
);
oThread.Name = "ComboHtlmReport";
oThread.Start();
}

private void GenerateComboEditHtmlPages()
{
ComboEditReport editReport = new ComboEditReport(_HtmlOutputLoc,
_RuleDescLoc, _Environment, _CoverPage);
cmdRun.Enabled = true;
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;
}

So what am I doing wrong?
 
P

Peter Duniho

[...]
So when I place a Debug stop on the method I call ThreadStart(MyMethod),
the
debugger stops on the line I have designated, but when I press the key to
navigate to the next instruction to process, nothing happens. The line
I am
stopped on instantiates a class which does all of the processing. I
can't
step into that process and it never comes back from that process. Here
is a
snipit of the code.....

[...]
So what am I doing wrong?

From your description, it's unclear what you expect to happen. However,
some information you may find helpful:

* In the debugger, when you step over the call to Thread.Start() (or
even if you tried to step into it), the thread you are debugging does not
switch to the thread you just created. It remains at the one you were
originally debugging. The other thread will get a chance to run each time
you step in the debugger, and of course it will run unimpeded if you
simply allow execution to continue. But you have to explicitly tell the
debugger to switch to the other thread, if you want to step through the
other thread.

If I recall correctly, in the Express version of Visual Studio, this is
only possible if you set a breakpoint in the code executed by the other
thread (e.g. "GenerateComboEditHtmlPages()"). In the retail versions of
Visual Studio, you can change threads using the debug toolbar control that
displays the threads in your application.

* You are talking about "threads" here, not "processes". I wouldn't
mention it, except that "process" has a very specific meaning in the
context of Windows (and other operating systems), and you've used the word
"process" here incorrectly.

* You should not set the Text property of your Label control from the
worker thread. This is an illegal operation and can cause problems. By
default, in .NET 2.0 and later, doing so will cause an MDA exception if
the debugger is attached to the process. You need to use Invoke() or
BeginInvoke() to execute a method on the Label control's owning thread, to
ensure that access to the Text property is done only by the thread that
owns the control.

You also have some kind of variable, named "cmdRun", that you access from
the thread. There's not enough context to know whether that's a safe use
of the variable or not, but do keep in mind that you need to synchronize
access to variables that may be used simultaneously from different threads.

Pete
 

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

Similar Threads


Top