cross thread notification

hi - I'm using a thread in C# and .net 4.0 to interact with the receive data portion of a serial driver. I can pass data to the main thread using either a static array or the Invoke method. but I'm looking for the best and/or recommended way to have the main thread wait on new data notification coming from the serial driver. I've implemented this a couple times but I've not been satisfied with the results.
essentially I want to post a message and have it return with data (ie synchronously).
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
essentially I want to post a message and have it return with data (ie synchronously)

Assuming you're talking about doing this synchronously from a GUI thread in WinForms?...

...use Async/Await with a Task, and possibly a ManualResetEvent.

Here's a contrived example to demonstrate:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CS_Scratch_001

    public partial class Form1 : Form

        public Form1()

        private string response;
        private ManualResetEvent MRE = new ManualResetEvent(false);

        private async void button1_Click(object sender, EventArgs e)
            button1.Enabled = false;
            label1.Text = "Sending request...";

            MRE.Reset(); // reset the ManualResetEvent before starting the thread
            await Task.Run(delegate {
                // I'm using the Timer here to simulate the "serial" portion of your program
                this.Invoke((MethodInvoker)delegate {
                    timer1.Start(); // replace this whole block with your "send" code call
                MRE.WaitOne(); // wait for the "serial" portion to signal that it has received a response

            label1.Text = response;
            button1.Enabled = true;    

        private void timer1_Tick(object sender, EventArgs e)

            // this is the simulated portion of your "serial" receieve
            // once you've received your response, signal the ManualResetEvent so that the thread can continue
            response = "Response at: " + DateTime.Now.ToString();

            MRE.Set(); // tell the thread to continue



Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jdcoburnAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.