|
|
#1 |
|
New Member
Join Date: Mar 07
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 |
|
.NUT
Join Date: May 05
Location: Sydney, Australia
Posts: 54,913
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
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.
__________________
![]() 2007, 2008, 2009, 2010 Why is my data not saved to my database? | Communicating between multiple forms | MSDN Data Walkthroughs MSDN "How Do I?" Videos: VB | C# VBForums Database Development FAQ My CodeBank Submissions: VB | C# (ForumAccount has translated some of my VB submissions to C#) My Blog: Defining and Raising Custom Events | Manipulating GDI+ Drawings | Using Parameters in ADO.NET |
|
|
|
![]() |
|
||||||
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|