New topic: Timers versus App.DoEvents
<http://forums.realsoftware.com/viewtopic.php?t=46241> Page 1 of 1 [ 5 posts ] Previous topic | Next topic Author Message DanSandbergUCONN Post subject: Timers versus App.DoEventsPosted: Fri Dec 14, 2012 12:11 pm Joined: Fri Dec 21, 2007 9:50 am Posts: 204 Hi All - I know that, when you want to pause your program until a certain event is complete, timers are preferred over using a loop with App.DoEvents. But can someone help me with the actual implementation. Let's say we have code that looks like this: //CODE here calls a thread called myThread //Thread has a boolean property called IAmDone do app.DoEvents(0) loop until myThread.IAmDone //More Code here is to be executed only after the thread finishes Now I want to replace the do loop, which calls app.DoEvents() with a timer instead. I can have the time check for myThread.IAmDone=true but how do I pause the execution of the code until the timer tells the code to continue? Thanks -Dan Top ktekinay Post subject: Re: Timers versus App.DoEventsPosted: Fri Dec 14, 2012 12:20 pm Joined: Mon Feb 05, 2007 5:21 pm Posts: 313 Location: New York, NY if myThread.IAmDone then //More Code here is to be executed only after the thread finishes me.Mode = 0 // Stop the Timer from firing again end if The Timer itself is the loop because it fires every Period. You should absolutely avoid App.DoEvents in a GUI app. _________________ Kem Tekinay MacTechnologies Consulting http://www.mactechnologies.com/ Need to develop, test, and refine regular expressions? Try RegExRX. Top DanSandbergUCONN Post subject: Re: Timers versus App.DoEventsPosted: Fri Dec 14, 2012 12:38 pm Joined: Fri Dec 21, 2007 9:50 am Posts: 204 ktekinay wrote:if myThread.IAmDone then //More Code here is to be executed only after the thread finishes me.Mode = 0 // Stop the Timer from firing again end if The Timer itself is the loop because it fires every Period. You should absolutely avoid App.DoEvents in a GUI app. So, once you want to pause the program, all code to be called AFTER the program "resumes" needs to be inside of a timer? Let me give more information about my program. I have a subroutine that I sent two large arrays byref to be filled. The arrays are then used by the program later. This subroutine was time consuming so I moved it to a thread but I still wanted to return the arrays from a subroutine. So I essentially wrapped the thread in a subroutine, where I still pass the arrays byref to the sub, call the thread inside the sub, once the thread fills the arrays the thread's myThread.IAmDone variable is true and the subroutine copies the thread's arrays before the thread actually finishes. There must be an easier way to use threads to return variables. If I pass variables to the thread in a constructor, in a byref way, there is no way to get that data back out? EDIT: You know what, I was able to make everything run just find by putting everything in the timer and it made my code much more efficient. Thanks Kte - your advice is always much appreciated! Top ktekinay Post subject: Re: Timers versus App.DoEventsPosted: Fri Dec 14, 2012 1:35 pm Joined: Mon Feb 05, 2007 5:21 pm Posts: 313 Location: New York, NY I'm not quite sure I understand. Is there anything in the arrays before the Thread gets them, or are you just dim'ing them? If the latter, change the Constructor of your thread to take just the two Ubound values (or one if both arrays should be the same size), and make the arrays properties of the thread. The Run event will create two local arrays, fill them, assign them to the properties, and set IAmDone to true. Your timer would check IAmDone and, when true, would assign thd.Arr1 and the.Arr2 (or whatever you call them) to wherever you need them, then nil the thread. Or... Create a new Class that acts like a wrapper for your arrays. Let's call it Array_Data, and it has two array properties. Its constructor would dim the arrays, you would pass that to the Thread's Run. myData = new Array_Date( 18000 ) // 18001 elements assigned to a global property dim thd as new My_Thread thd.RunMyThread( myArrs ) // A method in the Thread MyTimer.Mode = 2 // Start the Timer When the Thread is done, myData will contain both of your arrays. I'm sure there are 6 other ways to do this too. _________________ Kem Tekinay MacTechnologies Consulting http://www.mactechnologies.com/ Need to develop, test, and refine regular expressions? Try RegExRX. Top DanSandbergUCONN Post subject: Re: Timers versus App.DoEventsPosted: Fri Dec 14, 2012 3:58 pm Joined: Fri Dec 21, 2007 9:50 am Posts: 204 ktekinay wrote:I'm not quite sure I understand. Is there anything in the arrays before the Thread gets them, or are you just dim'ing them? If the latter, change the Constructor of your thread to take just the two Ubound values (or one if both arrays should be the same size), and make the arrays properties of the thread. The Run event will create two local arrays, fill them, assign them to the properties, and set IAmDone to true. Your timer would check IAmDone and, when true, would assign thd.Arr1 and the.Arr2 (or whatever you call them) to wherever you need them, then nil the thread. Or... Create a new Class that acts like a wrapper for your arrays. Let's call it Array_Data, and it has two array properties. Its constructor would dim the arrays, you would pass that to the Thread's Run. myData = new Array_Date( 18000 ) // 18001 elements assigned to a global property dim thd as new My_Thread thd.RunMyThread( myArrs ) // A method in the Thread MyTimer.Mode = 2 // Start the Timer When the Thread is done, myData will contain both of your arrays. I'm sure there are 6 other ways to do this too. Definitely a brilliant way to do it. I definitely would've been better off doing the whole thing right the first time (not using app.DoEvents in my GUI app) but I'm finding as I do it the "right" way I'm generating much more efficient code. So the world makes sense again. Thanks KTE. Top Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending Page 1 of 1 [ 5 posts ] -- Over 1500 classes with 29000 functions in one REALbasic plug-in collection. The Monkeybread Software Realbasic Plugin v9.3. http://www.monkeybreadsoftware.de/realbasic/plugins.shtml [email protected]
