Alpha Software Mobile Development Tools:   Alpha Anywhere    |   Alpha TransForm subscribe to our YouTube Channel  Follow Us on LinkedIn  Follow Us on Twitter  Follow Us on Facebook

Announcement

Collapse

The Alpha Software Forum Participation Guidelines

The Alpha Software Forum is a free forum created for Alpha Software Developer Community to ask for help, exchange ideas, and share solutions. Alpha Software strives to create an environment where all members of the community can feel safe to participate. In order to ensure the Alpha Software Forum is a place where all feel welcome, forum participants are expected to behave as follows:
  • Be professional in your conduct
  • Be kind to others
  • Be constructive when giving feedback
  • Be open to new ideas and suggestions
  • Stay on topic


Be sure all comments and threads you post are respectful. Posts that contain any of the following content will be considered a violation of your agreement as a member of the Alpha Software Forum Community and will be moderated:
  • Spam.
  • Vulgar language.
  • Quotes from private conversations without permission, including pricing and other sales related discussions.
  • Personal attacks, insults, or subtle put-downs.
  • Harassment, bullying, threatening, mocking, shaming, or deriding anyone.
  • Sexist, racist, homophobic, transphobic, ableist, or otherwise discriminatory jokes and language.
  • Sexually explicit or violent material, links, or language.
  • Pirated, hacked, or copyright-infringing material.
  • Encouraging of others to engage in the above behaviors.


If a thread or post is found to contain any of the content outlined above, a moderator may choose to take one of the following actions:
  • Remove the Post or Thread - the content is removed from the forum.
  • Place the User in Moderation - all posts and new threads must be approved by a moderator before they are posted.
  • Temporarily Ban the User - user is banned from forum for a period of time.
  • Permanently Ban the User - user is permanently banned from the forum.


Moderators may also rename posts and threads if they are too generic or do not property reflect the content.

Moderators may move threads if they have been posted in the incorrect forum.

Threads/Posts questioning specific moderator decisions or actions (such as "why was a user banned?") are not allowed and will be removed.

The owners of Alpha Software Corporation (Forum Owner) reserve the right to remove, edit, move, or close any thread for any reason; or ban any forum member without notice, reason, or explanation.

Community members are encouraged to click the "Report Post" icon in the lower left of a given post if they feel the post is in violation of the rules. This will alert the Moderators to take a look.

Alpha Software Corporation may amend the guidelines from time to time and may also vary the procedures it sets out where appropriate in a particular case. Your agreement to comply with the guidelines will be deemed agreement to any changes to it.



Bonus TIPS for Successful Posting

Try a Search First
It is highly recommended that a Search be done on your topic before posting, as many questions have been answered in prior posts. As with any search engine, the shorter the search term, the more "hits" will be returned, but the more specific the search term is, the greater the relevance of those "hits". Searching for "table" might well return every message on the board while "tablesum" would greatly restrict the number of messages returned.

When you do post
First, make sure you are posting your question in the correct forum. For example, if you post an issue regarding Desktop applications on the Mobile & Browser Applications board , not only will your question not be seen by the appropriate audience, it may also be removed or relocated.

The more detail you provide about your problem or question, the more likely someone is to understand your request and be able to help. A sample database with a minimum of records (and its support files, zipped together) will make it much easier to diagnose issues with your application. Screen shots of error messages are especially helpful.

When explaining how to reproduce your problem, please be as detailed as possible. Describe every step, click-by-click and keypress-by-keypress. Otherwise when others try to duplicate your problem, they may do something slightly different and end up with different results.

A note about attachments
You may only attach one file to each message. Attachment file size is limited to 2MB. If you need to include several files, you may do so by zipping them into a single archive.

If you forgot to attach your files to your post, please do NOT create a new thread. Instead, reply to your original message and attach the file there.

When attaching screen shots, it is best to attach an image file (.BMP, .JPG, .GIF, .PNG, etc.) or a zip file of several images, as opposed to a Word document containing the screen shots. Because Word documents are prone to viruses, many message board users will not open your Word file, therefore limiting their ability to help you.

Similarly, if you are uploading a zipped archive, you should simply create a .ZIP file and not a self-extracting .EXE as many users will not run your EXE file.
See more
See less

Using a thread to monitor if file is open

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

    Using a thread to monitor if file is open

    I am trying to monitor if a Word Document is open so when it is closed, I can change the file attributes to read only. I have been reading about threads but don't really understand them. I figured I could open a thread that continuously monitors the file_is_open() with a if-then loop, and when .f. engage action (file attribute change) and close the thread. I can't see that a thread can be closed from within the thread, or how to monitor that the thread is open, or when it is ended. I have the following that I can't seem to monitor. Any help is appreciated.

    Code:
    dim vfile as C
    vfile=vdoc_folder_path+"POL_BB_D991_20100427.doc"
    
    dim thread_name as C
    thread_name = "WORD PROBE"
     
    dim thread_code as C
    thread_code = <<%code%
    dim thread_run_flag as L = .t.
    while thread_run_flag
    RUNAGAIN:
        'the file open
        if file_is_open(vfile)
    		GOTO RUNAGAIN
    	else
    		thread_variables("WORD PROBE").thread_run_flag := .f.
                              msgbox("file closed")
    	end if
    end while
    %code%
    
    thread_create(thread_name, thread_code)
    Mike W
    __________________________
    "I rebel in at least small things to express to the world that I have not completely surrendered"

    #2
    Re: Using a thread to monitor if file is open

    I can't see that a thread can be closed from within the thread
    This does that.

    Code:
    thread_variables("WORD PROBE").thread_run_flag := .f.
    how to monitor that the thread is open
    You have to check with

    THREAD_IS_RUNNING()

    I don't think you can have the thread notify you that it has ended because "No user interface related commands are allowed in a thread".

    Does the user who has the doc open always make a change? You might try using file.watch_create().
    There can be only one.

    Comment


      #3
      Re: Using a thread to monitor if file is open
      • Get rid of the goto. Having the while loop handles rechecking the file and your goto bypasses the while loop, so that thread_run_flag has no ability to stop the thread.
      • Add a sleep in the loop so the thread doesn't consume too much CPU and prevent other threads from executing.
      • The thread will exit when the script runs to completion, so an exit while is all that is needed to end the thread.
      • If you want to toggle the thread_run_flag variable, refer to it simply as that. There is no need to use thread_variables("WORD PROBE").thread_run_flag since you are in that thread.
      • As Stan pointed out, you can't use msgbox or other UI functions in your thread.


      Code:
      dim vfile as C
      vfile=vdoc_folder_path+"POL_BB_D991_20100427.doc"
      
      dim thread_name as C
      thread_name = "WORD PROBE"
       
      dim thread_code as C
      thread_code = <<%code%
      dim thread_run_flag as L = .t.
      while thread_run_flag
          'the file open
          if file_is_open(vfile)
      		sleep(.5)
                      'after the sleep, the while loop is executed again
      	else
      		exit while
      	end if
      end while
      %code%
      
      thread_create(thread_name, thread_code)

      Lenny Forziati
      Vice President, Internet Products and Technical Services
      Alpha Software Corporation

      Comment


        #4
        Re: Using a thread to monitor if file is open

        Hi Mike,

        Threads are tricky if you haven't got a good handle on it. You can also consider using
        File.Watch_create()
        Regards,

        Ira J. Perlow
        Computer Systems Design


        CSDA A5 Products
        New - Free CSDA DiagInfo - v1.39, 30 Apr 2013
        CSDA Barcode Functions

        CSDA Code Utility
        CSDA Screen Capture


        Comment


          #5
          Re: Using a thread to monitor if file is open

          Lenny, Stan, Ira,
          Thank you. Lenny, your directions were super! I got that to work almost 100%. I have an issue with differences with behavior depending which MS Word version is used (at least I believe this the case). I have Office 97 (OK, it's 12 years old but it still works for me). Client uses Word 2000. The order of the functions firing makes a difference.

          thread_script()
          sys_open(file)

          This doesn't work since the thread_script has the checking for the doc being opened, and if closed fires the code to file.attribute_set(file,"+R"), it always opens the file read only.

          sys_open(file)
          thread_script()

          This opens the Word app and starts the thread but the focus ends on A5 app not the Word doc opened (not in Word 97 but yes with Word 2000). So now I'm hunting a command to set focus back to the open Word window. Haven't found one yet, but I know I have one in this app somewhere (age don't you know).

          Ira,
          Yes that function I checked out with Stan's mention of it in his above thread. The flag parameter I need is not provided for.. file open. I'll keep plugging away.

          Thanks all.

          Code:
          dim thread_name as C
          thread_name = "WORD PROBE"
           dim thread_code as C
          
          thread_code = <<%code%
          dim thread_run_flag as L = .t.
          while thread_run_flag
              'the file open
              if file_is_open(vfile)                 ' vfile comes from calling script
          		sleep(3)
                   'after the sleep, the while loop is executed again
             	else
             		file.attributes_set(vfile,"+R")
          		exit while
          	end if
          end while
          %code%
          
          thread_create(thread_name, thread_code)
          Mike W
          __________________________
          "I rebel in at least small things to express to the world that I have not completely surrendered"

          Comment


            #6
            Re: Using a thread to monitor if file is open

            While you are at it (I was going to suggest file watch, Ira already did) so just to add one more to the mix you could use process_list() although I think it's a little slow, maybe too slow.

            Instead of monitoring the file, why not check the file attribute:

            psuedo_code:
            if attribute = this
            change attribute to that
            end if

            That said, I am not really sure what's the overall objective. Is it that you want to make the file read only?
            How about attaching a password to the file instead?

            Comment


              #7
              Re: Using a thread to monitor if file is open

              Gabriel,
              Just to be honorable... STAN shared the file watch first...;)

              12 users (supervisors) have access to documents in the administrative side of this document control app. The documents are divided among 10 section of the department. They are policies. Each user has privileges (responsibility) to read-write to certain sections only, althought they need read rights to all. I wish to store all the documents in a read-only state and if the user has privileges to a document in their section, it will be opened read-write. Otherwise read-only. I have this worked out well. What I'm working on is for the event of the document opening under the user that has read-write privileges, after they are completed with their edits and save the doc and close the Word app, I want the attributes of that doc converted back to read only. So I need to monitor when the file under edit becomes no longer open (closed), and then fire the code to convert the attribute to read-only. The thread startegy seemed like a possibility, and I'm getting it to work. Now I just need a window_get_focus() command to have focus back to the Word window after the thread script is called, which takes focus back to A5.
              Last edited by Mike Wilson; 04-27-2010, 08:13 PM.
              Mike W
              __________________________
              "I rebel in at least small things to express to the world that I have not completely surrendered"

              Comment


                #8
                Re: Using a thread to monitor if file is open

                Is it one Word document with 12 sections? or 12 separate documents?

                Comment


                  #9
                  Re: Using a thread to monitor if file is open

                  G,
                  The document pile in this department is about 1100 documents total and growing. It is one document per section (about 110/section) x # of sections (10) = 1100 total. About 10% of total belong to each of 10 sections. Every document requires review and review signature by supervisor and director every year (annual reviews) with revisions as needed, and every 5 year renewals (complete rewrites due to technology advancements). This is just one department. Once the document control is present in one deartment, there are a dozen other departments available to expand this to. Just so it is clear... I am the director having to sign all these documents and I want......a better way than paper and folders all over my office!!
                  Last edited by Mike Wilson; 04-27-2010, 11:38 PM.
                  Mike W
                  __________________________
                  "I rebel in at least small things to express to the world that I have not completely surrendered"

                  Comment


                    #10
                    Re: Using a thread to monitor if file is open

                    Mike:
                    I go back to password.

                    Word allows you to assign a password to open and/or to edit the document which seems to be what you need here.

                    I am not in favor of anything running in the background or any OnTimer event unless absolutely necessary.

                    Comment


                      #11
                      Re: Using a thread to monitor if file is open

                      sys_focus_put()

                      Lenny Forziati
                      Vice President, Internet Products and Technical Services
                      Alpha Software Corporation

                      Comment


                        #12
                        Re: Using a thread to monitor if file is open

                        G,
                        I somewhat share your reluctance of having running background threads but I have to weigh that against having to script interractions with Word. I do not know MS Basic and want to keep coding between A5 and Word to absolute minimum.

                        Lenny,
                        Thanks. I appreciate that. When I read that, it immediately dawned on me where I have used it in this very application. But I was struggling. I think I've got it working pretty well now. Thanks for the lift.
                        Mike W
                        __________________________
                        "I rebel in at least small things to express to the world that I have not completely surrendered"

                        Comment

                        Working...
                        X