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
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