BeginInvoke with a property?

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

    BeginInvoke with a property?

    Hello everyone,

    How do I access a property from another thread in VB'05? Thanks

    public class frmblah
    inherits system.windows. forms.form

    private mCount as integer

    private property Count as integer
    get
    return mCount
    end get
    set (value as integer)
    mCount = value
    end set
    end property

    private sub updatecount
    if me.InvokeRequir ed then
    '
    '???????
    '
    else
    me.count += 1
    end if
    end sub

    end class


  • AMDRIT

    #2
    Re: BeginInvoke with a property?

    Well, I am begininvoking the UpdateCount instead and it appears to be
    working.


    "AMDRIT" <amdrit@hotmail .com> wrote in message
    news:enmtp3uaGH A.3328@TK2MSFTN GP02.phx.gbl...[color=blue]
    > Hello everyone,
    >
    > How do I access a property from another thread in VB'05? Thanks
    >
    > public class frmblah
    > inherits system.windows. forms.form
    >
    > private mCount as integer
    >
    > private property Count as integer
    > get
    > return mCount
    > end get
    > set (value as integer)
    > mCount = value
    > end set
    > end property
    >
    > private sub updatecount
    > if me.InvokeRequir ed then
    > '
    > '???????
    > '
    > else
    > me.count += 1
    > end if
    > end sub
    >
    > end class
    >[/color]


    Comment

    • Brian Henry

      #3
      Re: BeginInvoke with a property?

      This is for .NET not VB Classic, you might want to try a classic VB group
      instead

      "AMDRIT" <amdrit@hotmail .com> wrote in message
      news:OgL1l%23ua GHA.4520@TK2MSF TNGP03.phx.gbl. ..[color=blue]
      > Well, I am begininvoking the UpdateCount instead and it appears to be
      > working.
      >
      >
      > "AMDRIT" <amdrit@hotmail .com> wrote in message
      > news:enmtp3uaGH A.3328@TK2MSFTN GP02.phx.gbl...[color=green]
      >> Hello everyone,
      >>
      >> How do I access a property from another thread in VB'05? Thanks
      >>
      >> public class frmblah
      >> inherits system.windows. forms.form
      >>
      >> private mCount as integer
      >>
      >> private property Count as integer
      >> get
      >> return mCount
      >> end get
      >> set (value as integer)
      >> mCount = value
      >> end set
      >> end property
      >>
      >> private sub updatecount
      >> if me.InvokeRequir ed then
      >> '
      >> '???????
      >> '
      >> else
      >> me.count += 1
      >> end if
      >> end sub
      >>
      >> end class
      >>[/color]
      >
      >[/color]


      Comment

      • david

        #4
        Re: BeginInvoke with a property?

        On 2006-04-28, AMDRIT <amdrit@hotmail .com> wrote:[color=blue]
        > Hello everyone,
        >
        > How do I access a property from another thread in VB'05? Thanks
        >
        > public class frmblah
        > inherits system.windows. forms.form
        >
        > private mCount as integer
        >
        > private property Count as integer
        > get
        > return mCount
        > end get
        > set (value as integer)
        > mCount = value
        > end set
        > end property
        >
        > private sub updatecount
        > if me.InvokeRequir ed then
        > '
        > '???????
        > '
        > else
        > me.count += 1
        > end if
        > end sub
        >
        > end class[/color]

        You don't need to worry about InvokeRequired here, that's for accessing
        UI components on the UI thread. For ordinary properties, even if they
        are properties of a Form class, you can just set them from any thread
        (watching out for thread contention of course).

        [color=blue]
        >
        >[/color]

        Comment

        • AMDRIT

          #5
          Re: BeginInvoke with a property?

          David,

          Thanks for the response. The actual issue I am working through is a splash
          screen.

          I have a component library that has a base form and a splash screen form.
          All my application forms derive from the custom form, which now has two
          additional methods, (ShowWait, HideWait). These methods will be used
          primarly for long running processes called by the custom form, however, the
          derived form could make use of it as well.

          In VB'03 is was working great, my problem is in VB'05. The splash screen is
          supposed to fade in and out over time while it is displayed. I encountered
          two issues from the conversion.

          1. I do not have access to doevents. Since control libraries do not have
          access to the My namespace, I have been exploring
          Microsoft.Visua lBasic.Applicat ionServices.Win dowsFormsApplic ationBase. I
          haven't found a way to associate my ExecutingAssemb ly with my library.
          Threading.Threa d.Sleep doesn't seem to do the job, of cource that could be a
          result of my second issue.

          2. Accessing the splash screens form methods and properties cause:
          a. System.Threadin g.ThreadAbortEx ception
          b. System.Argument Exception
          c. System.InvalidO perationExcepti on
          d. System.Transact ions Critical at
          System.Windows. Forms.Control.S etVisibleCore(B oolean value)

          What I have going on is a do...while loop that is calling the fade routine
          when the splash screen is visible.

          public sub DoFading
          do while not done
          call Fade
          loop

          me.hide()
          me.close()

          end sub

          private sub Fade

          dim fadedirection as fadetypes

          if me.opacity <= 0 then
          fadedirection = fadetypes.up
          elseif me.opacity >= 1 then
          fadedirection = fadetypes.down
          end if

          select case fadedirection
          case fadetypes.up
          me.opacity += pintStepping
          case else
          me.opacity -= pintStepping
          end select

          end sub

          I changed my call to fade to Me.Invoke(New
          Windows.Forms.M ethodInvoker(Ad dressOf Fade))

          as well as the logic to hide and close the form

          I don't know what I am doing wrong, but it is not behaving like it used to.

          "david" <david@woofix.l ocal.dom> wrote in message
          news:slrne55052 .o10.david@loca lhost.localdoma in...[color=blue]
          > On 2006-04-28, AMDRIT <amdrit@hotmail .com> wrote:[color=green]
          >> Hello everyone,
          >>
          >> How do I access a property from another thread in VB'05? Thanks
          >>
          >> public class frmblah
          >> inherits system.windows. forms.form
          >>
          >> private mCount as integer
          >>
          >> private property Count as integer
          >> get
          >> return mCount
          >> end get
          >> set (value as integer)
          >> mCount = value
          >> end set
          >> end property
          >>
          >> private sub updatecount
          >> if me.InvokeRequir ed then
          >> '
          >> '???????
          >> '
          >> else
          >> me.count += 1
          >> end if
          >> end sub
          >>
          >> end class[/color]
          >
          > You don't need to worry about InvokeRequired here, that's for accessing
          > UI components on the UI thread. For ordinary properties, even if they
          > are properties of a Form class, you can just set them from any thread
          > (watching out for thread contention of course).
          >
          >[color=green]
          >>
          >>[/color][/color]


          Comment

          • Herfried K. Wagner [MVP]

            #6
            Re: BeginInvoke with a property?

            "Brian Henry" <nospam@nospam. com> schrieb:[color=blue]
            > This is for .NET not VB Classic, you might want to try a classic VB group
            > instead[/color]

            Mhm... The question is clearly related to VB.NET.

            --
            M S Herfried K. Wagner
            M V P <URL:http://dotnet.mvps.org/>
            V B <URL:http://classicvb.org/petition/>

            Comment

            • david

              #7
              Re: BeginInvoke with a property?

              On 2006-04-28, AMDRIT <amdrit@hotmail .com> wrote:[color=blue]
              > David,
              >
              > Thanks for the response. The actual issue I am working through is a splash
              > screen.
              >
              > I have a component library that has a base form and a splash screen form.
              > All my application forms derive from the custom form, which now has two
              > additional methods, (ShowWait, HideWait). These methods will be used
              > primarly for long running processes called by the custom form, however, the
              > derived form could make use of it as well.
              >
              > In VB'03 is was working great, my problem is in VB'05. The splash screen is
              > supposed to fade in and out over time while it is displayed. I encountered
              > two issues from the conversion.[/color]

              VB05 is a bit more persnickety about calling UI components from the
              wrong thread.[color=blue]
              >
              > 1. I do not have access to doevents. Since control libraries do not have
              > access to the My namespace, I have been exploring
              > Microsoft.Visua lBasic.Applicat ionServices.Win dowsFormsApplic ationBase. I
              > haven't found a way to associate my ExecutingAssemb ly with my library.[/color]

              I'm not sure why you need doEvents here, but it can be found at
              System.Windows. Forms.Applicati on.DoEvents().

              Of course, it needs to be called from the UI thread.

              [color=blue]
              > Threading.Threa d.Sleep doesn't seem to do the job, of cource that could be a
              > result of my second issue.
              >
              > 2. Accessing the splash screens form methods and properties cause:
              > a. System.Threadin g.ThreadAbortEx ception
              > b. System.Argument Exception
              > c. System.InvalidO perationExcepti on
              > d. System.Transact ions Critical at
              > System.Windows. Forms.Control.S etVisibleCore(B oolean value)
              >
              > What I have going on is a do...while loop that is calling the fade routine
              > when the splash screen is visible.[/color]

              Seems like a timer would be more appropriate. In fact, the Forms.Timer
              component will even handle calling you on the correct thread.[color=blue]
              >
              > public sub DoFading
              > do while not done
              > call Fade
              > loop
              >
              > me.hide()
              > me.close()
              >
              > end sub
              >
              > private sub Fade
              >
              > dim fadedirection as fadetypes
              >
              > if me.opacity <= 0 then
              > fadedirection = fadetypes.up
              > elseif me.opacity >= 1 then
              > fadedirection = fadetypes.down
              > end if
              >
              > select case fadedirection
              > case fadetypes.up
              > me.opacity += pintStepping
              > case else
              > me.opacity -= pintStepping
              > end select
              >
              > end sub
              >
              > I changed my call to fade to Me.Invoke(New
              > Windows.Forms.M ethodInvoker(Ad dressOf Fade))
              >
              > as well as the logic to hide and close the form
              >
              > I don't know what I am doing wrong, but it is not behaving like it used to.[/color]

              It's hard to tell from this snippet. I'm not even sure the threading is
              the error here is from the UI thread issues (usually you get an
              InvalidOperatio nException when that happens). One trick I like to do
              for these types of problems is have the form methods handle their own
              invoke requirements, like...


              Private Sub Fade()
              If Me.InvokeRequir ed Then
              Me.Invoke(New MethodInvoker(A ddressOf Fade))
              Exit Sub
              End If
              ' Do the real work




              [color=blue]
              >
              > "david" <david@woofix.l ocal.dom> wrote in message
              > news:slrne55052 .o10.david@loca lhost.localdoma in...[color=green]
              >> On 2006-04-28, AMDRIT <amdrit@hotmail .com> wrote:[color=darkred]
              >>> Hello everyone,
              >>>
              >>> How do I access a property from another thread in VB'05? Thanks
              >>>
              >>> public class frmblah
              >>> inherits system.windows. forms.form
              >>>
              >>> private mCount as integer
              >>>
              >>> private property Count as integer
              >>> get
              >>> return mCount
              >>> end get
              >>> set (value as integer)
              >>> mCount = value
              >>> end set
              >>> end property
              >>>
              >>> private sub updatecount
              >>> if me.InvokeRequir ed then
              >>> '
              >>> '???????
              >>> '
              >>> else
              >>> me.count += 1
              >>> end if
              >>> end sub
              >>>
              >>> end class[/color]
              >>
              >> You don't need to worry about InvokeRequired here, that's for accessing
              >> UI components on the UI thread. For ordinary properties, even if they
              >> are properties of a Form class, you can just set them from any thread
              >> (watching out for thread contention of course).
              >>
              >>[color=darkred]
              >>>
              >>>[/color][/color]
              >
              >[/color]

              Comment

              • Chris Chilvers

                #8
                Re: BeginInvoke with a property?

                On Fri, 28 Apr 2006 16:34:12 -0500, "AMDRIT" <amdrit@hotmail .com> wrote:
                [color=blue]
                >1. I do not have access to doevents.[/color]

                You don't need it if your fade code is running on a thread you've created.

                [color=blue]
                >2. Accessing the splash screens form methods and properties cause:
                > a. System.Threadin g.ThreadAbortEx ception
                > b. System.Argument Exception
                > c. System.InvalidO perationExcepti on
                > d. System.Transact ions Critical at
                >System.Windows .Forms.Control. SetVisibleCore( Boolean value)
                >
                >What I have going on is a do...while loop that is calling the fade routine
                >when the splash screen is visible.
                >
                >public sub DoFading
                > do while not done
                > Me.Invoke(New Windows.Forms.M ethodInvoker(Ad dressOf Fade))
                > loop
                >
                > me.hide()
                > me.close()
                >
                >end sub
                >[/color]

                If you look at that code (assuming you've wrapped that call to Fade() inside of an invoke like you described) you will
                see that your calls to hide() and close() are not using a MethodInvoker. These calls need to be executed from the
                controls thread and will need to be invoked as well:

                public sub DoFading
                MethodInvoker fader = New MethodInvoker(A ddressOf Fade)

                do while not done
                Me.Invoke(fader )
                'Really should think about a sleep here so as not to eat cpu time
                loop

                Me.Invoke(New MethodInvoker(A ddressOf Close())
                end sub

                This is making the assumption you have launched DoFading on a new thread, which it seems like you have done.

                Comment

                • AMDRIT

                  #9
                  Re: BeginInvoke with a property?

                  Thanks David and Chris, everything appears to be right again.

                  "Chris Chilvers" <keeper@dynafus .com> wrote in message
                  news:ov165212jb ai81gps1fqvmau6 bcl5nhd64@4ax.c om...[color=blue]
                  > On Fri, 28 Apr 2006 16:34:12 -0500, "AMDRIT" <amdrit@hotmail .com> wrote:
                  >[color=green]
                  >>1. I do not have access to doevents.[/color]
                  >
                  > You don't need it if your fade code is running on a thread you've created.
                  >
                  >[color=green]
                  >>2. Accessing the splash screens form methods and properties cause:
                  >> a. System.Threadin g.ThreadAbortEx ception
                  >> b. System.Argument Exception
                  >> c. System.InvalidO perationExcepti on
                  >> d. System.Transact ions Critical at
                  >>System.Window s.Forms.Control .SetVisibleCore (Boolean value)
                  >>
                  >>What I have going on is a do...while loop that is calling the fade routine
                  >>when the splash screen is visible.
                  >>
                  >>public sub DoFading
                  >> do while not done
                  >> Me.Invoke(New Windows.Forms.M ethodInvoker(Ad dressOf Fade))
                  >> loop
                  >>
                  >> me.hide()
                  >> me.close()
                  >>
                  >>end sub
                  >>[/color]
                  >
                  > If you look at that code (assuming you've wrapped that call to Fade()
                  > inside of an invoke like you described) you will
                  > see that your calls to hide() and close() are not using a MethodInvoker.
                  > These calls need to be executed from the
                  > controls thread and will need to be invoked as well:
                  >
                  > public sub DoFading
                  > MethodInvoker fader = New MethodInvoker(A ddressOf Fade)
                  >
                  > do while not done
                  > Me.Invoke(fader )
                  > 'Really should think about a sleep here so as not to eat cpu time
                  > loop
                  >
                  > Me.Invoke(New MethodInvoker(A ddressOf Close())
                  > end sub
                  >
                  > This is making the assumption you have launched DoFading on a new thread,
                  > which it seems like you have done.[/color]


                  Comment

                  Working...