Results 1 to 2 of 2

Thread: [2005] Listbox will not add item using system.timers

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Posts
    1

    [2005] Listbox will not add item using system.timers

    Hi,

    My current code is as follows: http://www.nomorepasting.com/paste.php?pasteID=76375

    The initial calls to ConsoleWrite work perfectly, the lines are added and all is well. However the timers, when they call ConsoleWrite(), do not write anything to the listbox on the form. I debugged it and the steps seem to go upto the calls to ConsoleWrite(), then step into the subroutine, but after that, do not do anything.. the timer.enabled property is not restored to true and everything stops. I'm guessing this has something to do with the fact these timer threads are calling ConsoleWrite() and not the main UI thread.. but im not sure why.

    On line 29, I have simply attempted to see whether manually adding an item will change result, but this didn't work.

    I also tried adding the lines GC.keepalive() for each timer in form_load, as I thought it might be the garbage collection messing with the timer after it finished.

    Thanks,
    Andy

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Listbox will not add item using system.timers

    When you create a Timers.Timer in the designer it automatically sets its SynchronizingObject property to the form its added to, thus it will raise its Elapsed events on the thread that owns that form. If you create a Timers.Timer in code then its SynchronizingObject property is Nothing by default, so it will raise its Elapsed events on a worker thread. That means that you cannot access control members directly from the Elapsed event handler. You have three choices:

    1. Use delegation to marshal the method call to the thread that owns the control you want to access.

    2. Set the SynchronizingObject property of the Timer to the form or a control on the form so the Elapsed event is raised in the UI thread.

    3. Use a Windows.Forms.Timer instead, which is optimised for use in a Windows Forms environment.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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