Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Threads and event handlers

Threads and event handlers

Scheduled Pinned Locked Moved C#
tutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TMattC
    wrote on last edited by
    #1

    I´m writing a communication library and have a threaded method listening for incoming data:

    ///// Inside my library /////
    public partial class MCSerialPort
    {
    (SerialPort)port.DataReceived += DataReceivedHandler;

    // The event handler method
    private async void DataReceivedHandler(object sender, SerialDataReceivedEventArgs args)
    {
    await ReadData();
    }

    private async Task ReadData()
    {
    await Task.Run(() =>
    {
    ... // Reading incoming
    // Triggering an event
    PackageReceived(this, new PackageReceivedEventArgs(package));
    });
    }
    }

    ///// Inside the "client" using the library /////
    (MCSerialPort)port.PackageReceived += PackageReceivedHandler;

    private void PackageReceivedHandler(object sender, PackageReceivedEventArgs args)
    {
    Package p = args.Package;
    receivedRtb.Invoke((Action)delegate{receivedRtb.Text += p.ToString();} );
    }

    The thing is that when using this library in a "client" project the PackageReceived event handler method in the client (here the PackageReceivedHandler()) seems to be running on the same thread as the librarys ReadData() method, because I have to call Invoke() in this example in order to get the info into the receivedRtb RichTextBox. This complicates things slightly for the average library user, who might not think of having to write threadsafe code in the handler method. Is there any convenient way to make PackageReceivedHandler() run on the "main" thread?

    P Richard DeemingR 2 Replies Last reply
    0
    • T TMattC

      I´m writing a communication library and have a threaded method listening for incoming data:

      ///// Inside my library /////
      public partial class MCSerialPort
      {
      (SerialPort)port.DataReceived += DataReceivedHandler;

      // The event handler method
      private async void DataReceivedHandler(object sender, SerialDataReceivedEventArgs args)
      {
      await ReadData();
      }

      private async Task ReadData()
      {
      await Task.Run(() =>
      {
      ... // Reading incoming
      // Triggering an event
      PackageReceived(this, new PackageReceivedEventArgs(package));
      });
      }
      }

      ///// Inside the "client" using the library /////
      (MCSerialPort)port.PackageReceived += PackageReceivedHandler;

      private void PackageReceivedHandler(object sender, PackageReceivedEventArgs args)
      {
      Package p = args.Package;
      receivedRtb.Invoke((Action)delegate{receivedRtb.Text += p.ToString();} );
      }

      The thing is that when using this library in a "client" project the PackageReceived event handler method in the client (here the PackageReceivedHandler()) seems to be running on the same thread as the librarys ReadData() method, because I have to call Invoke() in this example in order to get the info into the receivedRtb RichTextBox. This complicates things slightly for the average library user, who might not think of having to write threadsafe code in the handler method. Is there any convenient way to make PackageReceivedHandler() run on the "main" thread?

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      TMattC wrote:

      the PackageReceivedHandler()) seems to be running on the same thread as the librarys ReadData() method

      Correct. I think it's fine the way it is; the library should neither know nor care about the concerns of the calling application.

      1 Reply Last reply
      0
      • T TMattC

        I´m writing a communication library and have a threaded method listening for incoming data:

        ///// Inside my library /////
        public partial class MCSerialPort
        {
        (SerialPort)port.DataReceived += DataReceivedHandler;

        // The event handler method
        private async void DataReceivedHandler(object sender, SerialDataReceivedEventArgs args)
        {
        await ReadData();
        }

        private async Task ReadData()
        {
        await Task.Run(() =>
        {
        ... // Reading incoming
        // Triggering an event
        PackageReceived(this, new PackageReceivedEventArgs(package));
        });
        }
        }

        ///// Inside the "client" using the library /////
        (MCSerialPort)port.PackageReceived += PackageReceivedHandler;

        private void PackageReceivedHandler(object sender, PackageReceivedEventArgs args)
        {
        Package p = args.Package;
        receivedRtb.Invoke((Action)delegate{receivedRtb.Text += p.ToString();} );
        }

        The thing is that when using this library in a "client" project the PackageReceived event handler method in the client (here the PackageReceivedHandler()) seems to be running on the same thread as the librarys ReadData() method, because I have to call Invoke() in this example in order to get the info into the receivedRtb RichTextBox. This complicates things slightly for the average library user, who might not think of having to write threadsafe code in the handler method. Is there any convenient way to make PackageReceivedHandler() run on the "main" thread?

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        TMattC wrote:

        ... the PackageReceived event handler method in the client (here the PackageReceivedHandler()) seems to be running on the same thread as the librarys ReadData() method, because I have to call Invoke() ...

        No, you have to call Invoke() because the PackageReceivedHandler isn't running on the UI thread. There are various ways to raise the event on the UI thread - for example, using the SynchronizationContext class[^]. (It's All About the SynchronizationContext - MSDN Magazine[^]) However, since most libraries don't both doing this, most UI developers should be used to having to marshal the event handler code to the UI thread.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups