Results 1 to 3 of 3

Thread: [RESOLVED] Help understanding timer synchronizing object

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Location
    cobwebbed to PC
    Posts
    311

    Resolved [RESOLVED] Help understanding timer synchronizing object

    Folks,

    Im reading through MSDN on threading and timers. Looking at the System.Timers.Timer it has a synchronizationObject and says

    "Gets or sets the object used to marshal event-handler calls that are issued when an interval has elapsed."
    and
    "If you use the Timer with a user interface element, such as a form or control, without placing the timer on that user interface element, assign the form or control that contains the Timer to the SynchronizingObject property, so that the event is marshaled to the user interface thread."

    But I do not understand if or how it should be set when interacting with other, non-UI, things, such as other threads or objects

    A) In the case described in the quote would a correct example be the following?
    vb Code:
    1. Sub CreateTimer ()
    2.     '....
    3.     aTimer = New System.Timers.Timer(10000)
    4.     aTimer.SynchronizingObject = myForm1
    5.     '...
    6. End Sub

    B) How should I set it if the timer elapsed event creates a new thread or performs some action with another thread or with some (non-UI) object?

    Basically Im not really understanding what is meant by the phrase "the object used to marshal event-handler calls that are issued".... Im going back to read up on marshalling now but any help would be good as MSDN is not the best place to learn from scratch.

    C) What type of thread is the timer or its elapsed event run on? ThreadPool?
    Thanks
    Thanks

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Help understanding timer synchronizing object

    But I do not understand if or how it should be set when interacting with other, non-UI, things, such as other threads or objects
    It shouldn't be set at all! The whole point of this particular timer is that it runs outside the UI thread allowing events to happen without interrupting or being delayed by the doings of the main programming thread and, in most circumstances, that's precisely how you would want it to run. There may however be occasions when you want the events to affect a UI control, let's say flashing the background of form as an alarm or alert. In those circumstances you set the synchronisation object to the form to allow interaction between the event and the form which would not normally be possible.

    Marshaling is a multi-faceted concept but at heart, as its name suggests, it simply allows things which are not part of the VB type structure as such to be moved around or fenced off so that VB can deal with them as though they were. So in this specific case a threaded process which normally would not have access to controls is dealt with as though it were part of the UI thread. You could always do this using Interop marshalling yourself. The synchronisation object merely provides a simple single way of doing this without additional coding.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Location
    cobwebbed to PC
    Posts
    311

    Re: Help understanding timer synchronizing object

    Dunfiddlin, Thanks for your help again
    I'd rate but I have to spread some around apparently - looks like your just too good at helping folks Appreciate
    Last edited by wolf99; Aug 2nd, 2013 at 04:34 AM.
    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width