Results 1 to 14 of 14

Thread: SOLVED: Is simulated multithreading the way to go (for what i need to do)?

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    12

    Resolved SOLVED: Is simulated multithreading the way to go (for what i need to do)?

    First off, let me say I'm an experienced programmer, but I'm still reorienting myself with VB which I hadn't touched in some time.

    I'm writing an app with a heavy-duty function call that takes about 5 minutes to complete (processing a list of files inside a loop). What happens is, the user clicks "Start" which triggers said function, and the program (predictably) becomes unresponsive until the function completes. What i'd like, however, is for the user to still be able to move the window or at least click a Cancel button if they need to abort it (the function itself is primarily a loop, so I could add a check at the end of said loop).

    Now, I've read the horror stories about simulated multithreading in vb6, so I'm wondering...is there another (better) way?
    Last edited by evank; Dec 11th, 2006 at 01:01 PM. Reason: Issue Solved (Thanks rob!)

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Is simulated multithreading the way to go (for what i need to do)?

    If you are just doing something in a loop then it may not be a candidate for multithreading.

    Do you have a doevents in the loop and a boolean variable flag to signify the breaking out of the loop?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    12

    Resolved Re: Is simulated multithreading the way to go (for what i need to do)?

    DoEvents is what I was looking for

    As I said, still accustoming myself to vb, so chock this one up as a stupid question

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: SOLVED: Is simulated multithreading the way to go (for what i need to do)?

    There are no stupid questions.

    Ask any questions and we will try to help (just create a new thread for anything new )

    Glad to have helped.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: SOLVED: Is simulated multithreading the way to go (for what i need to do)?

    You would need to put DoEvents which should execute at regular intervals in your application. However as the normal DoEvents is very slow, just have a look at this link which provides an alternative faster DoEvents function.

    Give Time to Other Events in My Application


    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  6. #6
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: SOLVED: Is simulated multithreading the way to go (for what i need to do)?

    You can speed it up pretty well by using DoEvents only when it is needed. You can use the GetInputState API function for this.

    VB Code:
    1. Private Declare Function GetInputState Lib "user32" Alias "GetInputState" () As Long
    2.  
    3. Do
    4.     DoSomething
    5.     If GetInputState Then DoEvents
    6. Loop Until Condition = True

    There is another (better) API function to use, but I forgot what it is. Hopefully someone remembers and will post it.

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: SOLVED: Is simulated multithreading the way to go (for what i need to do)?

    That is a great API DR but what about mouse moves over the GUI? Wouldnt it not process those and give the locked illusion? Seems the api is only for mouse clicks and keyboard keypresses.

    Good tip though
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: SOLVED: Is simulated multithreading the way to go (for what i need to do)?

    Quote Originally Posted by RobDog888
    That is a great API DR but what about mouse moves over the GUI? Wouldnt it not process those and give the locked illusion? Seems the api is only for mouse clicks and keyboard keypresses.

    Good tip though
    It seems to work for me.

    You may have to put a DoEvents right before the loop, not sure.

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: SOLVED: Is simulated multithreading the way to go (for what i need to do)?

    Havent tested it but mostly referring to your gui if you have events that should fire like mouseovers which dont count towards the queue that the api looks at.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: SOLVED: Is simulated multithreading the way to go (for what i need to do)?

    I know it's marked as resolved, but just in case someone is interested in the multithreading option - a little demo program.

    As to which is faster, MultiThreading or DoEvents, I'll leave others to argue. The obvious advantage of MultiThreading comes in when you have to perform other tasks within the same app. Make an executable of the code below and run it. Click on "Start MultiThread" and move the form around, scroll the textbox etc. The "AddTextMultiThread" function continues without even a pause. Click on "Stop MultiThread". Try the same with "Start/Stop DoEvents". Spot the difference.

    'Original code at:- http://www.free2code.net/plugins/art...read.php?id=94

    VB Code:
    1. Option Explicit
    2. 'Form level code.
    3.  
    4. 'MultiThread versus DoEvents.
    5.  
    6. 'Add a textbox and 4 command buttons to a form.
    7.  
    8. Private Declare Function CreateThread Lib "kernel32" (ByVal lpThreadAttributes As Any, _
    9.    ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, _
    10.    ByVal dwCreationFlags As Long, lpThreadID As Long) As Long
    11.  
    12. Private Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, _
    13.    ByVal dwExitCode As Long) As Long
    14.  
    15. Private Sub Command1_Click()
    16.    id = CreateThread(ByVal 0&, ByVal 0&, AddressOf AddTextMultiThread, ByVal 0&, 0, id)
    17. End Sub
    18.  
    19. Private Sub Command2_Click()
    20.    Call TerminateThread(id, ByVal 0&)
    21. End Sub
    22.  
    23. Private Sub Command3_Click()
    24.    StopDo = True
    25.    AddTextDoEvents
    26. End Sub
    27.  
    28. Private Sub Command4_Click()
    29.    StopDo = False
    30. End Sub
    31.  
    32. Private Sub Form_Load()
    33.    Me.Height = 4545
    34.    Me.Width = 8925
    35.    
    36.    Text1.Move 45, 45, 8700, 3525
    37.    
    38.    Command1.Move 45, 3690, 1635, 375
    39.    Command1.Caption = "Start MultiThread"
    40.  
    41.    Command2.Move 1845, 3690, 1635, 375
    42.    Command2.Caption = "Stop MultiThread"
    43.  
    44.    Command3.Move 5310, 3690, 1635, 375
    45.    Command3.Caption = "Start DoEvents"
    46.  
    47.    Command4.Move 7110, 3690, 1635, 375
    48.    Command4.Caption = "Stop DoEvents"
    49. End Sub
    VB Code:
    1. 'Module level code.
    2. Option Explicit
    3.  
    4. Public id As Long
    5. Public StopDo As Boolean
    6.  
    7. Public Function AddTextMultiThread()
    8.    Do
    9.       Form1.Text1.SelText = "Adding to Text1 - "
    10.    Loop
    11. End Function
    12.  
    13. Public Function AddTextDoEvents()
    14.    StopDo = True
    15.    Do While StopDo = True
    16.       Form1.Text1.SelText = "Adding to Text1 - "
    17.       DoEvents
    18.    Loop
    19. End Function

  11. #11
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: SOLVED: Is simulated multithreading the way to go (for what i need to do)?

    The above code will not work correctly.
    The CreateThread API is not supported in VB and will quite often crash your app.

    The only method I have seen that is close to tru multithreading in the code contained in the Multithreading link in my sig.

    Cheers,

    Woka

  12. #12
    Member Senacharim's Avatar
    Join Date
    Feb 2008
    Posts
    56

    Re: SOLVED: Is simulated multithreading the way to go (for what i need to do)?

    Made the form & module with code as supplied, and when compiled (though not in VB) it runs as advertised. Thank you. Very keen bit of code.
    _____________________________
    . Error Code 34: There is no error .

  13. #13
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: SOLVED: Is simulated multithreading the way to go (for what i need to do)?

    Thank you for letting us know that you have your answer. You may find it easier next time however by just pulling down the Thread Tools menu and clicking the Mark Thread Resolved button.

  14. #14
    Lively Member VBNubcake's Avatar
    Join Date
    Feb 2008
    Location
    Caledon, Ontario, Canada
    Posts
    80

    Re: SOLVED: Is simulated multithreading the way to go (for what i need to do)?

    it seems like people in here know what they're talking about when it comes to multi-threading.

    in that case (since this thread is solved), would anyone like to look at my problem involving multi-threading?


    http://www.vbforums.com/showthread.php?t=521236

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