Calling methods from constructor?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Grandma Wilkerson

    #1

    Calling methods from constructor?

    Hi,

    I have a class whose constructor accepts a Socket as an argument. The
    constructor then makes a call to Socket.BeginRec eive(). The callback
    delegate passed to BeginReceive() is a method of my class. There is a slight
    chance that the callback delegate will be called from the I/O thread pool
    BEFORE my constructor completes. Is calling this delegate before the
    constructor finishes a problem?

    In C++, this type of arrangement was a problem as I recall. In C#, it
    doesn't seem like it SHOULD be a problem. After all, I can call private
    methods from my constructor so why shouldn't some external party be able to
    call them in the midst of construction as well? In any case, I just wanted
    to double-check!

    Granny


  • William Stacey

    #2
    Re: Calling methods from constructor?

    To side-step this issue and possible others, you could (of course) create
    ..Send() public method or .Connect() and use that after construction. You
    can still set the socket var in your constructor and/or property of the
    class.
    --wjs
    "Grandma Wilkerson" <turd@bottlenec k.scalability.c rash.dum> wrote in message
    news:OWMx2B$SDH A.2196@TK2MSFTN GP12.phx.gbl...[color=blue]
    > Hi,
    >
    > I have a class whose constructor accepts a Socket as an argument. The
    > constructor then makes a call to Socket.BeginRec eive(). The callback
    > delegate passed to BeginReceive() is a method of my class. There is a[/color]
    slight[color=blue]
    > chance that the callback delegate will be called from the I/O thread pool
    > BEFORE my constructor completes. Is calling this delegate before the
    > constructor finishes a problem?
    >
    > In C++, this type of arrangement was a problem as I recall. In C#, it
    > doesn't seem like it SHOULD be a problem. After all, I can call private
    > methods from my constructor so why shouldn't some external party be able[/color]
    to[color=blue]
    > call them in the midst of construction as well? In any case, I just wanted
    > to double-check!
    >
    > Granny
    >
    >[/color]


    Comment

    • Jon Skeet

      #3
      Re: Calling methods from constructor?

      Grandma Wilkerson <turd@bottlenec k.scalability.c rash.dum> wrote:[color=blue]
      > I have a class whose constructor accepts a Socket as an argument. The
      > constructor then makes a call to Socket.BeginRec eive(). The callback
      > delegate passed to BeginReceive() is a method of my class. There is a slight
      > chance that the callback delegate will be called from the I/O thread pool
      > BEFORE my constructor completes. Is calling this delegate before the
      > constructor finishes a problem?[/color]

      Well, it'll be a potential problem if the delegate relies on
      information which is set up after Socket.BeginRec eive() is called, but
      I don't think it's fundamentally a problem. Probably worth documenting
      very clearly though, for maintenance purposes.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      Working...