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

  • Home
  • SEARCH
  • 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 3490068
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:28:17+00:00 2026-05-18T11:28:17+00:00

I’ve got a little stuck with a small problem here: I use serialport communications

  • 0

I’ve got a little stuck with a small problem here:
I use serialport communications – all my functions for comm. are wrapped into my own serialport class.
Instances of that class are supposed to only be used in the

 using(Serport port = new Serport(...)){}

to assure, that Dispose() is called after the operation.

Now this is no problem with one time calls, but I can’t think of a way for this:
I have a function that is supposed to be called via a while loop for permanently refreshing via comport until the user interrupts (the function is designed to reset a timer itself every time it’s called and time out when not called anymore).
Now this operation is quite time-critical and cannot be opening and closing the serialport every time it gets called. So using the using pattern from up there inside that function won’t work ( or will it ?)
The only way to realize this I can think of is to place the using(){} around the while loop – I’d like to avoid that though since it would mix up my code that is clearly built in a way, that comport access is handled low level and to the real app only simple functions that do all the work are availible…

Do you guys see any alternative? Is there a way to terminate the using pattern by hand? Just calling Dispose() by hand in my timeout-timer won’t work since it won’t be called if I get an exception – and I have to be sure that Dispose() gets called, so the serialport stay manageable…

Thanks for your help!!

  • 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-05-18T11:28:17+00:00Added an answer on May 18, 2026 at 11:28 am

    You don’t need to use a using statement for an IDisposable:

    From MSDN:

    The using statement ensures that
    Dispose is called even if an exception
    occurs while you are calling methods
    on the object. You can achieve the
    same result by putting the object
    inside a try block and then calling
    Dispose in a finally block; in fact,
    this is how the using statement is
    translated by the compiler. The code
    example earlier expands to the
    following code at compile time (note
    the extra curly braces to create the
    limited scope for the object):

    {
      Font font1 = new Font("Arial", 10.0f);
      try
      {
        byte charset = font1.GdiCharSet;
      }
      finally
      {
        if (font1 != null)
          ((IDisposable)font1).Dispose();
      }
    }
    

    You can either make sure that your exception handling does call Dispose, or you can for example make some outer Parent class inherit from IDisposable:

    void Parent.Dispose()
    {
      if (port != null)
      {
        port.Dispose();
      }
    }
    

    and wrap the creation of the Parent object in the using statement, and let it worry about calling Dispose on the Serport.

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

Sidebar

Related Questions

No related questions found

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.