6

I'm very new to threading. I hope someone can give me some example.

I'm trying to start a thread when user click on start button and do the following process:

private void btnStart_Click(object sender, RoutedEventArgs e)
{
    if (serialPort.IsOpen)
        serialPort.Close();
    try
    {
        //To set all the parameters for Serial Comm
        serialPort.PortName = "COM14";
        serialPort.BaudRate = int.Parse("38400");
        serialPort.Parity = Parity.None;
        serialPort.DataBits = 8;
        serialPort.StopBits = StopBits.One;
        serialPort.Encoding = System.Text.Encoding.ASCII;

        serialPort.DataReceived += new SerialDataReceivedEventHandler(GotRawData);

        serialPort.Open();

        //To show that Com Port is Opened
        txtboxOutput.AppendText(DateTime.Now.ToString("hh:mm:ss tt") + " - COM14 is opened." + Environment.NewLine);
        txtboxOutput.ScrollToEnd();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message.ToString());
    }
}

private void GotRawData() is a method where i do something to get some raw data from a hardware.

PVitt
  • 11,500
  • 5
  • 51
  • 85
user981924
  • 463
  • 1
  • 4
  • 8
  • See this question to get more information on the BackgroundWorker: http://stackoverflow.com/questions/5483565/how-to-use-wpf-background-worker – Emond Oct 13 '11 at 04:25

3 Answers3

7

You might find the System.ComponentModel.BackgroundWorker class rather useful which in my understanding is the simplest way to execute an operation on a separate thread.

Nano Taboada
  • 4,148
  • 11
  • 61
  • 89
  • I've look into that but I do not know how to actually use it. I come across a lot of threading, do not know which one is better for my case. Example like threadPool and Dispatcher and this backgroundWorker. I would like to have some example based on my project. Easier for me to understand. Thanks anyway for answering =) – user981924 Oct 13 '11 at 02:52
  • No problem -- glad it helped. To get started just wrap whatever code you need to run in a separate thread within its [`DoWork`](http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.dowork.aspx) event handler. Still I recommend you to walk through the code samples at MSDN, they're well documented. – Nano Taboada Oct 13 '11 at 03:09
0

I do not know if I understood the question correctly. Once the user clicks a button, you want to start a seperate thread and receive data from the serial port there. I think this should that:

private void btnStart_Click(object sender, RoutedEventArgs e)
{
Thread GetData = new Thread(thFunctionToRun);
GetData.Start();
}
Manoj
  • 5,011
  • 12
  • 52
  • 76
0

You're not making any blocking calls in btnStart_Click, so it's fine to just run this on the main UI thread.

A couple of points:

  • Remember that GotRawData will be called on a worker thread, so if you are accessing any UI controls, you will have to marshal those calls back onto the UI thread.

  • From MSDN SerialPort.Open :

The best practice for any application is to wait for some amount of time after calling the Close method before attempting to call the Open method, as the port may not be closed instantly.

Nick Butler
  • 24,045
  • 4
  • 49
  • 70