Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8564159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:07:58+00:00 2026-06-11T17:07:58+00:00

I’m looking to do both synchronous communication (over serial) using .NET’s SerialPort and also

  • 0

I’m looking to do both synchronous communication (over serial) using .NET’s SerialPort and also have asynchronous communication (over Ethernet) using .NET’s Socket.

The current implementation has a Connection class that declares both a SerialPort and Socket, and depending on what the user has defined the device’s connection type in the GUI–either COM or Ethernet–the Connection class will initialize the appropriate serial or socket port.

I have a ConnectionManager that holds all the Connections. So if the user creates a device using the same connection info, it doesn’t create a new Connection; instead, it is just assigned the Connection that is found in ConnectionManager.

This is a problem, because I need to do synchronous communications as well as asynchronous. What happens is there is a loop that creates Tasks while I’m connected to a device and each Task is trying to do a Send and Receive, and since it takes longer for the device to complete a task, than it takes the software to create tasks, there is a lot concurrency problems (blocked threads). Since I need to do synchronous communication, I lock a Mutex before my send Send and release the mutex after my Receive–to assure for every Send I get a Receive. This is not good because then when I would try to do Async communications it will be throttled due to the mutex.

So my question is what is the best design to follow when trying to handle asynchronous and synchronous communications at the same time? Should there be two different connection classes (AConnection, BConnection) that inherit a Connection interface with Send, Receive, Open, Close methods?

Any help is appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:07:59+00:00Added an answer on June 11, 2026 at 5:07 pm

    In general, you should alway prefer object composition over class inheritance. What that means is, instead of creating a super-class (say WorkClass) with most of the functionality that you need and then two concrete sub-classes that inherit from it (say AWorkClass : WorkClass and BWorkClass : WorkClass) and use/override those functions, you should crease two concrese classes with no inheritance (AClass and BClass) which “have a” WorkClass as a member variable.

    Taking this a bit further, and ultimatley trying to answer your question, it sounds like you want to implement the Strategy Pattern which will allow you to switch “strategies” (like between sync and async communication) very easily and allows you to add new “strategies” down the line without much effort (or make modifications to existing ones without having to worry about ripple-effects as much).

    Here is a sample Strategy implemented in C#:

    namespace Your.App
    {
    //correct implementation of the Strategy Pattern
        interface ICommunication
        {
            void Send();
        }
        class SyncCommunication : ICommunication
        {
            public void Send() { /*your sync code*/ }
        }
        class AsyncCommunication : ICommunication
        {
            public void Send() { /*your async code*/ }
        }
        public class WorkClass
        {
            ICommunication Strategy { get; set; }
            public WorkClass()
            {
                UseAsync = false;
            }
            bool _useAsync;
            public bool UseAsync 
            {
                get { return _useAsync; }
                set
                {
                    _useAsync = value;
                    if(_useAsync)
                        Strategy = new AsyncCommunication();
                    else
                        Strategy = new SyncCommunication ();
                }
            }
            public void Send()
            {
                Strategy.Send();
            }
        }
    }
    

    To tie this in with the first paragraph, then use WorkClass like this:

    namespace Your.App
    {
    //correct implementation of the Strategy Pattern
        public class MyWorkContext
        {
            WorkClass Worker {get; set;}
            public MyWorkContext()
            {
                Worker = new WorkClass();
            }
    
            public void SendMyData()
            {
                if(someConditionIsMet)
                    Worker.UseAsync = true;
                else
                    Worker.UseAsync = false;
                Worker.Send();
            }
        }
    }
    

    You can see that the user of your WorkClass (which is MyWorkContext in this case) doesn’t know anything about your strategies, nor should it. It just tells the worker enough for it to know what to do, and then the worker knows which strategy it needs to use to do it. The strategies themselves are the only ones that know how the work is actually done, which is really nice for testing and maintenance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.