Problem with Thread

I

ilyaw

Hi, I have a small sample winform application. (Form with 1 label and
2 buttons START and EXIT) It creates a new thread to run the COM
object and assign the value to the variable. Everything working fine
and label populated by the value I need, the only problem is that when
I close the form it gives me the application error message box "The
memory could not be read". I'm new to threads programming and may be
doing something in a wrong way so every advice is much appreciated.
Here is the code:

static void Main()
{
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmMyForm());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

}

public partial class frmMyForm : Form
{
public double tm=0.0;

public frmReutersRIC()
{
InitializeComponent();
}

private void cmdStart_Click(object sender, EventArgs e)
{
//Need List becasue going forward it will be about 10
threads
List<Thread> myThreadList = new List<Thread>();

Thread t = new Thread(UseMyThread);
t.SetApartmentState(ApartmentState.MTA); //Have to run my
COM object in MTA thread
t.Start();
myThreadList.Add(t);
Thread.Sleep(10000); //Time for COM object in t thread do
its job
foreach (Thread tr in myThreadList) tr.Join(); //is
tr.Abort() better?
this.lblTime.Text = tm.ToString();
}


protected void UseMyThread(){
COMObj obj = new COMObj();
try
{
//Do the job
tm=Convert.Double(value);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
System.Runtime.InteropServices.Marshal.ReleaseComObject
(obj);
}

//Get memory leak error clicking this button
private void cmdExit_Click(object sender, EventArgs e)
{
try
{
this.Dispose();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

Thanks for the help
 
P

Peter Duniho

ilyaw said:
Hi, I have a small sample winform application. (Form with 1 label and
2 buttons START and EXIT) It creates a new thread to run the COM
object and assign the value to the variable. Everything working fine
and label populated by the value I need, the only problem is that when
I close the form it gives me the application error message box "The
memory could not be read". I'm new to threads programming and may be
doing something in a wrong way so every advice is much appreciated. [...]

You're definitely doing some things the wrong way. But unfortunately,
it's hardly clear from your incomplete code example whether any of those
things have anything to do with the error you're getting.

Some things I know you're doing wrong:

-- Using Thread.Sleep() to wait for a thread to perform its work.
Just use Thread.Join(), if you must wait.

-- Using Thread.Join() to wait for a thread to perform its work,
after you've already used Thread.Sleep(). Why not just call Join() from
the outset?

-- Using Thread.Join() to wait for a thread to perform its work, in
the main GUI thread. Blocking the main GUI thread is a _big_ "no-no".

-- Failing to mark your "tm" variable as "volatile". In the
specific example, there's an implicit memory barrier in your call to
Thread.Join(), but a) as I said, you shouldn't be using Thread.Join(),
and b) the memory barrier doesn't affect compiler optimizations that
might also cause visibility problems to changes to that variable.

Per your comment asking "is tr.Abort() better", the answer to that is a
definite _no_. Calling Thread.Abort() is almost always the wrong thing
to do. Most threaded code will never need a single call to that method,
anywhere in the code.

Also, if you want to close the form, you should call the Close() method,
not the Dispose() method.

I notice you are calling Thread.SetApartmentState(). But you're setting
the state to MTA, which is already the default for threads. There's
really no point in the call.

Of course, in the code example you posted, you're not actually _doing_
anything with the COM object you create anyway. Since the code example
is incomplete, it's not clear whether the problem actually occurs when
you don't actually call the COM object, or if one needs more in order to
reproduce the problem.

But, as I said, none of the above are things that to me obviously would
cause the error you're seeing. Unless you post a concise-but-complete
code example that reliable demonstrates the problem, it will be
impossible for anyone to state specifically and unambiguously what's
wrong with the code.

In the meantime, you might consider using the debugger, to see where in
your code it's actually executing when the error occurs. That might
lead you to some specific information about why it's happening.

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

Top