Threading problem - Urgent Help

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have the following problem. I created a

Properties section:
private SerialComm oComm = new SerialComm(); // Wraps a DLL to handle Serial

//comms.
private Thread PrinterThread = null; // Serial Communications Thread

private void Form_load(...)
{
PrinterThread = new Thread(new ThreadStart(StartPrinter));
PrinterThread.Name = "SerialPrinter";
}

// The Thread is started in this procedure
private void button_click(....)
{
if (PrinterThread.ThreadState.ToString() == "Unstarted")
{
try
{
PrinterThread.Start();
Thread.Sleep(0);
}
catch (Exception Ex)
{
//Handle exception
}
}

// This is the procedure the for the Thread
private void StartPrinter()
{
Thread.Sleep(0);
LogInfo("Label Printing: printing thread started");
oComm.sendToPrinter(sFactor,false);
// Calls the serial printing process. It starts its own receiving Thread
within it.
// Returns to this procedure when done. The receiving thread must have
been
//aborted.

if (oComm.lStatus)
this.sMessage.Text = "Printing label has Finished";
// I got an Thread exeption here. After the aborting the receiving
thread
// If this.sMessage.txt is not there, it doesn't happen. Why????
// The exception says: ThreadException found while thread is being
aborted.
// I think it refers to the receiving thread.

this.cmdLabel.Enabled = true;
this.cmdReprint.Enabled = true
}


Thank you,

Carlos
 
Carlos Lozano said:
Hi,

I have the following problem. I created a

Properties section:
private SerialComm oComm = new SerialComm(); // Wraps a DLL to handle
Serial

//comms.
private Thread PrinterThread = null; // Serial Communications Thread

private void Form_load(...)
{
PrinterThread = new Thread(new ThreadStart(StartPrinter));
PrinterThread.Name = "SerialPrinter";
}

// The Thread is started in this procedure
private void button_click(....)
{
if (PrinterThread.ThreadState.ToString() == "Unstarted")
{
try
{
PrinterThread.Start();
Thread.Sleep(0);
}
catch (Exception Ex)
{
//Handle exception
}
}

// This is the procedure the for the Thread
private void StartPrinter()
{
Thread.Sleep(0);
LogInfo("Label Printing: printing thread started");
oComm.sendToPrinter(sFactor,false);
// Calls the serial printing process. It starts its own receiving
Thread
within it.
// Returns to this procedure when done. The receiving thread must have
been
//aborted.

if (oComm.lStatus)
this.sMessage.Text = "Printing label has Finished";
// I got an Thread exeption here. After the aborting the receiving
thread
// If this.sMessage.txt is not there, it doesn't happen. Why????
// The exception says: ThreadException found while thread is being
aborted.
// I think it refers to the receiving thread.

this.cmdLabel.Enabled = true;
this.cmdReprint.Enabled = true
}


Thank you,

Carlos

To add to what Jon replied.....You are updating the UI from a non UI thread,
this is something you should never do on Windows.
See http://www.pobox.com/~skeet/csharp/threads/winforms.shtml


Willy.
 
Sorry I didn't describe correctly.

It seems the answer is that I am trying to update the UI as you said,
because when removing it, the problem is gone.

My question here is: How can I catch the result of the thread to notify the
user? I have a message field on the form.

Thank you,

Carlos
 
Carlos Lozano said:
Sorry I didn't describe correctly.

It seems the answer is that I am trying to update the UI as you said,
because when removing it, the problem is gone.

My question here is: How can I catch the result of the thread to notify the
user? I have a message field on the form.

The answer is in the link Willy posted.
 
Back
Top