NET_USE and DOS error

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

Post Reply
User avatar
metron9
Posts: 45
Joined: Sat Sep 08, 2012 7:18 am
Location: Minnesota U.S.A.

NET_USE and DOS error

Post by metron9 »

In my old programs i am attempting to move to HMG, I used the locks.prg and net_use function to open files.
I created a netopen() function that also opens index files if they do not exist.

The typical call would use the IF but I get a dos error using HMG when I test the function with a bad file name.
Do I need to set up error trapping for dos errors?
What is the correct way to try/catch a file open error?

I promised myself i would not stay up til 5:00 am tonight so I will look for a reply in the morning.


Code: Select all

IF NETOPEN(DRIVECCDATA+'badfilename') 
 MSGBOX('SUCCESS')
ENDIF
The netopen function and net_use functions used.
Converted to HMG.

Code: Select all

FUNCTION NETOPEN
 local tmpscr
 PARAMETERS FIL,NTX1,FLD1,NTX2,FLD2,NTX3,FLD3,NTX4,FLD4
  
 if .not.net_use(FIL,.f.,3)
  err('Unable to open '+FIL)
  RETURN(.F.)
 endif

DO CASE
CASE PCOUNT()>2
 if .not.file(NTX1+'.NTX')
  if fil_lock(3)
   SHOWINDEX(FLD1,NTX1)
   index on &FLD1 to &NTX1
   unlock
   CLRMYMSGBOX()  
  else
   err('Unable to create index file',NTX1)
   close
   RETURN(.F.)
  endif
 endif
CASE PCOUNT()>4
 if .not.file(NTX2+'.NTX')
  if fil_lock(3)
   SHOWINDEX(FLD1,NTX1)
   index on &FLD1 to &NTX1
   SHOWINDEX(FLD2,NTX2)
   index on &FLD2 to &NTX2
   unlock
  CLRMYMSGBOX()   
  else
   err('Unable to create index file',NTX2)
   close
   RETURN(.F.)
  endif
 endif
CASE PCOUNT()>6
 if .not.file(NTX3+'.NTX')
  if fil_lock(3)
   SHOWINDEX(FLD1,NTX1)
   index on &FLD1 to &NTX1
   SHOWINDEX(FLD2,NTX2)
   index on &FLD2 to &NTX2
   SHOWINDEX(FLD3,NTX3)
   index on &FLD3 to &NTX3
   unlock
    CLRMYMSGBOX()
  else
   err('Unable to create index file',NTX3)
   close
   RETURN(.F.)
  endif
 endif
CASE PCOUNT()>8
 if .not.file(NTX4+'.NTX')
  if fil_lock(3)
   SHOWINDEX(FLD1,NTX1)
   index on &FLD1 to &NTX1
   SHOWINDEX(FLD2,NTX2)
   index on &FLD2 to &NTX2
   SHOWINDEX(FLD3,NTX3)
   index on &FLD3 to &NTX3
   SHOWINDEX(FLD4,NTX4)
   index on &FLD4 to &NTX4
   unlock
   CLRMYMSGBOX()
  else
   err('Unable to create index file',NTX4)
   close
   RETURN(.F.)
  endif
 endif
ENDCASE
UNLOCK
DO CASE
 CASE PCOUNT()>8
  set index to &NTX1,&NTX2,&NTX3,&NTX4
 CASE PCOUNT()>6
  set index to &NTX1,&NTX2,&NTX3
 CASE PCOUNT()>4
  set index to &NTX1,&NTX2
 CASE PCOUNT()>2
  set index to &NTX1
 OTHERWISE
  SET INDEX TO
ENDCASE
RETURN(.T.)

FUNCTION NET_USE
PARAMETERS file, ex_use, wait
Private forever

forever = (wait = 0)
DO WHILE (forever .OR. wait > 0)

   IF ex_use                    && exclusive
      USE &file EXCLUSIVE
   ELSE
      USE &file          && shared
   ENDIF

   IF .NOT. NETERR()           && USE succeeds
      RETURN (.T.)
   ENDIF

   INKEY(1)                   && wait 1 second
   wait = wait - 1
ENDDO
RETURN (.F.)             && USE fails

FUNCTION FIL_LOCK
PARAMETERS wait
PRIVATE forever

IF FLOCK()
   RETURN (.T.)          && locked
ENDIF

forever = (wait = 0)
DO WHILE (forever .OR. wait > 0)

   INKEY(.5)             && wait 1/2 second
   wait = wait - .5

   IF FLOCK()
      RETURN (.T.)       && locked
   ENDIF

ENDDO
RETURN (.F.)             && not locked

FUNCTION REC_LOCK
PARAMETERS wait
PRIVATE forever

IF RLOCK()
   RETURN (.T.)          && locked
ENDIF

forever = (wait = 0)
DO WHILE (forever .OR. wait > 0)

   IF RLOCK()
      RETURN (.T.)       && locked
   ENDIF

   INKEY(.5)             && wait 1/2 second
   wait = wait - .5

ENDDO
RETURN (.F.)             && not locked



FUNCTION ERR
 PARAMETERS S1,S2,S3,S4
  msgbox(S1+s2+s3+s4,'Error') 
 RETURN NIL
 
FUNCTION SHOWINDEX
 PARAMETERS FLD,FIL
  main.mymsgbox.value := 'Indexing on '+fld+' to '+fil+'.NTX'
 RETURN NIL

 FUNCTION CLRMYMSGBOX()
  MAIN.MYMSGBOX.VALUE := NUL
 RETURN NIL

 
Attachments
So far the main screen is being born.
So far the main screen is being born.
sofar.png (20.9 KiB) Viewed 2394 times
User avatar
esgici
Posts: 4543
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Contact:

Re: NET_USE and DOS error

Post by esgici »

Hello Mark

Congrats :!:

Your form is attractive and your are quite speedy :)
Do I need to set up error trapping for dos errors?
What is the correct way to try/catch a file open error?
There isn't a specific way to try/catch DOS errors; you could develop and use a general error handling mechanism and / or use BEGIN ... END SEQUENCE structure.

Bad file name probably produce DOS Error 2 ( File not found ).

And you can test file existence by FILE() function:

Code: Select all

IF FILE( <file_name> )
   < do something )
ELSE
   < do another thing )
ENDIF
I promised myself i would not stay up til 5:00 am tonight so I will look for a reply in the morning.
Did you take into account time difference ?

By the way, personally my English is not so good; so please be more clear and specific as soon as possible.

Happy HMG'ing :D
Viva INTERNATIONAL HMG :D
User avatar
metron9
Posts: 45
Joined: Sat Sep 08, 2012 7:18 am
Location: Minnesota U.S.A.

Re: NET_USE and DOS error

Post by metron9 »


Quote:
I promised myself i would not stay up til 5:00 am tonight so I will look for a reply in the morning.



Did you take into account time difference ?

By the way, personally my English is not so good; so please be more clear and specific as soon as possible.
I am Sorry, I think you misunderstood my message here.
I was not making any reference to your reply time to my post.

I thank you very much for very speedy reply's.

I have been staying up to 3:00 am for the past week and it is wearing me down
so I wanted to start getting to sleep earlier so I could wake up earlier.
programming and learning a new language is very exciting to me and I stay up too late and miss my sleep
I use to stay up all night when I first started programming ( I was in my 20's then though)
so it was a reference to that time period and I think to many programmers that work into the wee hours of the night.

The time difference is very good as I can post a message and go to sleep when you are just waking up.

Thank you again for your very speedy reply's!

I assumed I would get a dos error if the file was open by another user in exclusive mode.
I did not test that.
Trying to open a file that is being used by another user is different than trying to open a file that does not exist.
I understand that now, I should test for existence of file first to eliminate dos error.

Thank You.
User avatar
esgici
Posts: 4543
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Contact:

Re: NET_USE and DOS error

Post by esgici »

Hi Mark
metron9 wrote:...programming and learning a new language is very exciting to me and I stay up too late and miss my sleep I use to stay up all night when I first started programming ( I was in my 20's then though)...
Believe me, I understand you very good ;)

Yes, especially HMG is highly exciting tool :)

I'm glad that the "DOS Error" problem solved :)

Happy HMG'ing :D
Viva INTERNATIONAL HMG :D
Post Reply