Announcement

Collapse
No announcement yet.

Threads

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Threads

    I want to do some file processing from a PBDLL 60 program that will take a few minutes to process. But during that time, would like the user to be able to interact with the dialog (minimize it for example). I am guessing that creating a Thread to run the file processing is the answer. If so, can anyone point me to a program example of proper Thread processing?

    Thanks.

  • #2
    While your loop or whatever processes the data, it could regularly process the messages in the message queue ... this would be a more simple solution.

    Regards


    -------------
    Daniel
    [email protected]

    Comment


    • #3
      What should I use to get messages? DIALOG DOEVENTS? Thanks a lot for your help Daniel - I'm new at this windows stuff.

      Comment


      • #4
        Phil --
        as I understand, your problem is enough simple - you want to have alone "background" process and in reality you don't need seriously think about synñhronization of different tasks.
        I think you can analyze an example from PB/DLL' Help ("THREAD").
        Roughly speaking, you describe "file creation" in separate function and replace "CALL" by "THREAD CREATE".

        [This message has been edited by Semen Matusovski (edited January 05, 2000).]

        Comment


        • #5
          Here is an example
          Code:
          #COMPILE EXE
          #INCLUDE "win32api.inc"
           
          GLOBAL Flag AS LONG
          GLOBAL hDlg AS LONG
           
          DECLARE CALLBACK FUNCTION DlgProc
           
          FUNCTION PBMAIN
           
              DIALOG NEW 0, "Thread Example",,,200,100,%DS_CENTER OR %WS_SYSMENU OR %WS_MAXIMIZEBOX TO hDlg
              CONTROL ADD BUTTON, hDlg, 100, "Start Thread", 10,10,50,12
              CONTROL ADD LABEL, hdlg, 101, "", 10, 30, 100, 12
              DIALOG SHOW MODAL hDlg CALL DlgProc
           
          END FUNCTION
           
          CALLBACK FUNCTION DlgProc
           
              SELECT CASE CBMSG
              CASE %WM_COMMAND
                  SELECT CASE LOWRD(CBWPARAM)
                  CASE 100
                      SELECT CASE Flag
                      CASE 0 ' Thread not running : start it
                          CONTROL SET TEXT CBHNDL, 100, "Stop Thread"
                          Flag = 1
                          THREAD CREATE ThreadProc(1) TO r&
                      CASE 1 ' Thread running : stop it
                          CONTROL SET TEXT CBHNDL, 100, "Start Thread"
                          Flag = 0
           
                      END SELECT
                  END SELECT
              
              END SELECT
           
          END FUNCTION
           
          FUNCTION ThreadProc(x AS LONG) AS LONG
           
              LOCAL z AS LONG
           
              WHILE Flag
                  SLEEP 100
                  INCR z
                  CONTROL SET TEXT hDlg, 101, FORMAT$(z)
              WEND
           
          END FUNCTION

          Comment


          • #6
            Phil,

            I use the following WinMain function to start a program in which I wish the dialog to respond to messages. The main thread creates the dialog then spawns a thread that becomes my main worker thread. After creating the worker thread I let it fall into a DO-LOOP which performs a Dialog Doevents and then sleeps from 10 milliseconds then checks the value of End_Program. If End_Program is true then exit the DO_LOOP close the dialog.
            I use this for utility programs that I use to process files which can take a few minutes or serveral hours The use of two threads lets me keep the dialogs responsive to user input.(As you indicated so that the user can minimize the dialog to execute another utility or whatever else needs to be done.)

            Code:
            FUNCTION WinMain (BYVAL hInstance     AS LONG, _
                              BYVAL hPrevInstance AS LONG, _
                              lpCmdLine           AS ASCIIZ PTR, _
                              BYVAL iCmdShow      AS LONG) AS LONG
            
               'Create dialog window and return handle to hDlg&
            
               'Create a new thread calling WinMain2 and pass the dialog handle (I also place the handle in a Global Var)
               'WinMain2 is the function that becomes my main thread for this program
               THREAD CREATE WinMain2(hDlg&) TO hThread&
               THREAD CLOSE hThread& TO Result& ' free the thread handle but leave the thread running
            
               'Loop till Global var End_Program=%true
               do
                  'respond to messages
                  DIALOG DOEVENTS      
                  sleep 10
            
                  'Exit loop if global var End_Program=%true
                  IF End_Program=%true THEN
                     EXIT DO
                  end if
               loop
            
               DIALOG END hDlg&, 0
            
            
             END FUNCTION  ' WinMain
            James

            [This message has been edited by James Moneypenny (edited January 06, 2000).]
            James Moneypenny
            mailto:[email protected][email protected]</A>

            Comment


            • #7
              Thanks everybody for your help, especially for the sample code.

              Phil

              Comment

              Working...
              X