New topic: Understanding threads
<http://forums.realsoftware.com/viewtopic.php?t=46056> Page 1 of 1 [ 4 posts ] Previous topic | Next topic Author Message DanSandbergUCONN Post subject: Understanding threadsPosted: Tue Nov 27, 2012 11:56 am Joined: Fri Dec 21, 2007 9:50 am Posts: 193 Most of my coding in RB, to date, has been smaller analyses or programs to create user-friendly interfaces for my co-workers. Anything intensive, I have used C++ or a scripting language. But I am growing more powerful (cue evil laugh)..... Anyway, I have a function within my program that is taking ~10-30 minutes. If I run it from the main program my interface locks up. My understanding is that the way around this is to bury the code into a thread. My question is, how can I pass arguments to the function if it is within a thread. For example, let's say I have a pushbutton that collects a number of variables on the screen and passes them to the function. In the pushbutton action event I call my function with the arguments in parenthesis. However, if the function call is in the thread, I would call the run event of the thread but I don't know how I'd pass the arguments to the function. Alternatively I could nest the function code in the thread but I'd still have the same problem with passing arguments. My initial thought is that I could create global variables, initially store my argument values into the global variables, then the run event would read whatever is in the global variables. Is this the 'right way' to use threads? I apologize in advance for my ignorance on this issue. Top Thom McGrath Post subject: Re: Understanding threadsPosted: Tue Nov 27, 2012 12:08 pm Site Admin Joined: Tue May 06, 2008 1:07 pm Posts: 1276 Location: Greater Hartford Area, CT Create a Thread subclass and add a constructor with the parameters you would pass into the function. In the constructor, store those parameters in private properties on the thread so the Run event has access to them. So you code which looks today like MyFunction(Thing1,Thing2) would now look like Dim Worker As New MyThread(Thing1,Thing2) Worker.Run and overall, your code hasn't changed much. The only additional work necessary is creating a way for your Window to "poll" the thread about it's status. This is typically done with a timer with a short period checking a property on the thread instance. _________________ Thom McGrath - @tekcor Web Framework Architect, Real Software, Inc. Top ktekinay Post subject: Re: Understanding threadsPosted: Tue Nov 27, 2012 1:16 pm Joined: Mon Feb 05, 2007 5:21 pm Posts: 262 Location: New York, NY Also, can you post the code that's taking this long? There may be ways to optimize it. _________________ Kem Tekinay MacTechnologies Consulting http://www.mactechnologies.com/ Need to develop, test, and refine regular expressions? Try RegExRX. Top DanSandbergUCONN Post subject: Re: Understanding threadsPosted: Tue Nov 27, 2012 3:41 pm Joined: Fri Dec 21, 2007 9:50 am Posts: 193 Thom McGrath wrote:Create a Thread subclass and add a constructor with the parameters you would pass into the function. In the constructor, store those parameters in private properties on the thread so the Run event has access to them. So you code which looks today like MyFunction(Thing1,Thing2) would now look like Dim Worker As New MyThread(Thing1,Thing2) Worker.Run and overall, your code hasn't changed much. The only additional work necessary is creating a way for your Window to "poll" the thread about it's status. This is typically done with a timer with a short period checking a property on the thread instance. Brilliant and thank you. ktekinay wrote:Also, can you post the code that's taking this long? There may be ways to optimize it. Thank you very much for your kind offer to pull thru my code kte! That might be quite alot of work. I do not suspect the problem is inefficiencies in the source though. I'm working some VERY large arrays of data, i.e. arr(100, 7000, 60). I also believe (perhaps foolishly so) that there is some proprietary value to the mathematics I've implemented and I hope to capitalize on that Top Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending Page 1 of 1 [ 4 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]
