Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Single User App

Status
Not open for further replies.

newtofoxpro

Programmer
Sep 16, 2007
301
IN
Code not tested. This is my idea, Pls post yours.

Code:
ApplicationType="SingleUser"
IF !FILE("users.dbf")
   CREATE TABLE users.dbf (CompName c(20), UserName c(20))
   USE 
ENDIF
USE users.dbf IN 0 ALIAS "users"
ON ERROR WAIT WINDOW NOWAIT ""
SET REPROCESS TO 1
DELETE FOR LOCK() IN users
INSERT INTO users VALUES (UPPER(GETENV("COMPUTERNAME")),UPPER(GETENV("USERNAME")))
=LOCK("users")
SELECT * FROM users INTO ARRAY JustCheck GROUP BY CompName,UserName
IF _tally>1 AND ApplicationType="S"
   WAIT WINDOW "Single user application"
   QUIT
ENDIF
 
We are not here to GIVE you code.

We are here to assist/advise you on how to handle a problem or suggest to you an approach that you might want to consider.

As Mike asked above, please clearly define a problem that you want assistance with and we will try to assist you.

Also you should TEST your own approach first before asking a question about it.
That way you will better understand where your problem is.

Good Luck,
JRB-Bldr



 
The term single user application typically refers to an application used by only one user. The essential part is not disallowing a second user, but EXE and data is local, so the single user may even work on dbf files exclusive, you have no share, no shared data. Such an application would not need a users.dbf

The way you seem to define it is only allowing one user. This won't work, unless the EXE is located in a share and nobody makes a link to it with a differing working directory.
Besides, as you always INSERT a new record, even the single allowed user could only run this EXE once. though you eliminate double data by GROUP BY that means this dbf will grow one record with each start and take longer and longer to verify all records are equal. Remember: DELETE does not really delete records.

So first of all this would do as we strongly not recommend many times including lately to have an EXE in a share and it's not at all unbreakable, eg simply delete the user.dbf when noone is running the EXE and you can become the license owner.

What do you really want? If you want to detect no second start, there should be at least two FAQs here on how to accomplish that.

Bye, Olaf.
 
I want, Single User should work on single machine with multi-tasking environment.
I want to allow a table open on a single machine with multiple times by re-open exe. But not by another user on another machine.

 
So, let's get this right.

You want a user to be able to open a certain table multiple times. But you don't want any other user to open that same table while the first user has it open. Is that correct?

If so, the easiest way is to locate the table on the computer of the person who is allowed to open it, and not to make the location sharable.

But if you can't do that for any reason, you could do the following:

[tt]SET EXCLUSIVE ON
USE TheTable IN 0 ALIAS Tab1
USE TheTable IN 0 AGAIN ALIAS Tab2[/tt]

That will give the user two instances of the table. Other users won't be able to open it, because it is exclusive to the first user. But be aware that, although EXCLUSIVE is set on, the user won't be able to do things like PACK or INDEX, because the table is in fact "shared" between two work areas.

But before continuting this discussion, please confirm that my assumption re your requirement is correct. Your explanation was not very clear.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Mike has hit the nail, if it's really about a table your application needs exclusive access to, that's what exclusive access is for. EXCLUSIVE means a user can have as many aliases as he wants or needs, but this will only work in one process. Why do you need your application started multiple times to act on a table multiple times? Just create a form with private datasession to start it more than once even with same alias and at app start try to get the table you need there exclusive. You know you're the first when having success and quit, if not.

That way you'll also not need a users.dbf. It'll be ok, if you want to see which user has the exclusive access, but make it a one row table, in which you try an UPDATE, don't INSERT there. The security of that users.dbf doesn't matter much, if you open the other table exclusive, but the way your code does not act on a certain path anyone wanting to start your exe can do so by making a link to it with a local working directory, so a new users.dbf is created there and will allow the exe to run. That of course won't matter, as he still can't get access to the other exclusively open dbf. Following sample even just writes out OD() to a text file:

Code:
Local llSecond
* eventually read in config
Try
   * somepath may come from config
   Use (somepath+"thedatatable.dbf") In 0 Exclusive
Catch
   llSecond = .T.
Endtry

If llSecond
   Messagebox("Single user only!")
   Quit
Else
   *First user
   Set Safety Off
   StrToFile(ID(),"currentuser.txt",0)
   Set Safety On
Endif
* continue as wanted eg
*    Do Form someform
*    Do menu.mpr
* make the Do Form command a menu item, so the user can start as often as he wants.
* READ EVENTS
* Erase currentuser.txt

You may also CATCH to loException and analyse, whether the error is exclusive acces denied or file not found to decide if config is ok or it's really just the second user.

Bye, Olaf.
 
Thank you

I post vfp-code because my vfp language good than english

I have build single form/screen application. So I need to start another instance / 2nd start. So I have to always exclusive=OFF.
 
Rather change from single form to multi form than trying to cover exclusive access without real exclusive access but locking of another table. This is easier, trust us. And it is just one setting, the form.datasession=2 &&private datasession.

You can even do your private datasession form as top level form, so it looks as if many application instances are started. No screen with all these forms confined in them, just the desktop is your container, as with any application. Form.showwindow = 2 && as top level form - that also adds their own taskbar item and they dock to the desktop independent and in general feel like a separate started process.

Bye, Olaf.
 
>because my vfp language good than english
Well, code describes a behaviour very precise, but it doesn't help, if it's not hitting the nail.
A bad english description additional or as comments helps much better to understand needs.
This is not the grammar forum, so don't be afraid.

Bye, Olaf.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top