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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

run my form once

Status
Not open for further replies.

tseegii

Programmer
Dec 14, 2003
9
0
0
MN
Hello everyone, i want to run my form once only. Please give me some code or idea.
 
tseegii,

Did you mean have only a single instance of your application running at a time? If so, have a look at the following thread:

thread184-672388

boyd.gif

 
tseegii

The following snippit from a main.prg attempts to open the table errorlog.dbf, which as its name implies, logs any errors found and so the code needs to be placed right at the top of your main.prg.

It checks for the existence of the table and by attempting to open it exclusively will then quit the application if the table is already in use by another instance of the application.

If your version of VFP does not support TRY...ENDTRY, you can use ON ERROR llError = .T., etc and construct a similar code block.
Code:
[COLOR=blue]IF ADIR(laTemp,SYS(5)		;
		+ ADDBS(SYS(2003))	;
		+ [tables\errorlog.dbf]) = 1
	TRY 
		USE tables\errorlog	IN 0 EXCLUSIVE
	CATCH
		MESSAGEBOX(													;
			[Application appears to be either in use or]			;
			+ CHR(13)												;
			+ [maybe still shutting down from the last session.]	;
			+ CHR(13)												;
			+ CHR(13)												;
			+ [Please try again in a few moments]					,;
			16														,;
			[Application in use]									,;
			3000)
		QUIT
	ENDTRY
ELSE
	MESSAGEBOX(													;
		[The ErrorLog table is missing from your application]	;
		+ CHR(13)												;
		+ CHR(13)												;
		+ [Please reinstall the application]					,;
		16														,;
		[ErrorLog table missing]								,;
		3000)
	QUIT
ENDIF[/color]

FAQ184-2483 - answering getting answered.​
Chris [pc2]
PDFcommander[sup]tm[/sup].net
PDFcommander[sup]tm[/sup].co.uk
 
Tseegii,

I'm assuming you have a menu command or a button which launches the form. If the form is already running, you don't want to run it again.

The following function will do that. If the form is already running, it just brings it to the front. The function takes the form name as a parameter. Note that this depends on the form's SCX filename being the same as its Name property.

Code:
LPARAMETERS tcFormName
LOCAL loForm, llOpen

tcFormName = UPPER(tcFormName)
llOpen = .F.
FOR EACH loForm IN _SCREEN.Forms
  IF UPPER(loForm.Name) == tcFormName
    * This is the one we want
    loForm.Show
    llOpen = .T.
    EXIT
  ENDIF
ENDFOR
IF NOT llOpen
  * Not yet open, so open it now
  DO FORM (tcFormName)
ENDIF

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My sites:
Visual FoxPro (www.ml-consult.demon.co.uk)
Crystal Reports (www.ml-crystal.com)
 
tseegii, are you there? If you really want to keep your application from running multiple times, these faq's should help:
faq184-2442 -- Using Windows "Atoms"
faq184-839 -- Using DDE (my favorite, since it allows other inter-program communication)
faq184-1998 -- Using Main Window Caption
faq184-1767 -- In Pre-Vfp6 (I think), using Main Window Caption, or Atoms



- Bill

Get the best answers to your questions -- See FAQ481-4875.
 
If, on the other hand, you really wanted to know how to run your Form (within your application) just once.... that is very simple, and entirely dependant on how you are instantiating your forms.

For example, if you simply always assign your form to a global variable (so it can be referenced from everywhere in your program), then there will always be only one instance:

RELEASE goMyForm
PUBLIC goMyForm
DO myform NAME goMyForm LINKED

Of course, if you don't want instance #2 to release/replace instance #1, but simply to not open again:

if vartype(goMyForm)<>'O'
RELEASE goMyForm
PUBLIC goMyForm
DO myform NAME goMyForm LINKED
endif

This is the simplest (and not necessarily the best). Any more sophisticated way is very dependant on the design of your app: How do you launch your form(s)?

- Bill

Get the best answers to your questions -- See FAQ481-4875.
 

Mike,

Make it modal.

Strongly disagree. There are many situations where you want to disallow multiple instances without having to do without the benefits of modeless forms.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My sites:
Visual FoxPro (www.ml-consult.demon.co.uk)
Crystal Reports (www.ml-crystal.com)
 
Hi Mike,

Too true. Don't know about the strongly, though.<g>

I was just putting forth an option that hadn't been offered, and there are times when it is the best one.

Regards,

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top