run a process in the background

I made a simple VB program to update my WAN IP address to an FTP website.
so I can access my home machine from work.
I didn't want to pay for commercial software for something so simple as this.
I am stubborn that way (and yes.. cheap!)

It is working well but I do not know how to make it "run in the background"

I need it to update every 15 minutes, but I am using a timer and it doesn't seem like the right thing to do.  It does it's thing but seems to interupt my mouse and other functions for a few seconds.

How can I make a routine work in the background every fifteen minutes without affecting other applications?

Note: I am probably going to change it so it updates only when the wan ip is different but I still need to make it work in the background.

LVL 1
gormlyAsked:
Who is Participating?
 
nffvrxqgrcfqvvcConnect With a Mentor Commented:
'Well here it is all in a nutshell...
'I set modminutes to 1 minute for testing purposes you can change it to 15 later..
'Try this see if it works
'ADD 1 timer to your form then copy and paste the following code into Form_declerations
'Once you copied and pasted the code, look in the timer, and enter your FTP settings

'=================================

Option Explicit
'FTP declares start
Private Const FTP_TRANSFER_TYPE_UNKNOWN = &H0
Private Const FTP_TRANSFER_TYPE_ASCII = &H1
Private Const FTP_TRANSFER_TYPE_BINARY = &H2
Private Const INTERNET_DEFAULT_FTP_PORT = 21
Private Const INTERNET_SERVICE_FTP = 1
Private Const INTERNET_FLAG_PASSIVE = &H8000000
Private Const INTERNET_OPEN_TYPE_PRECONFIG = 0
Private Const INTERNET_OPEN_TYPE_DIRECT = 1
Private Const INTERNET_OPEN_TYPE_PROXY = 3
Private Const INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY = 4
Private Const MAX_PATH = 260
Private Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
    dwFileAttributes As Long
    ftCreationTime As FILETIME
    ftLastAccessTime As FILETIME
    ftLastWriteTime As FILETIME
    nFileSizeHigh As Long
    nFileSizeLow As Long
    dwReserved0 As Long
    dwReserved1 As Long
    cFileName As String * MAX_PATH
    cAlternate As String * 14
End Type

Private Declare Function InternetCloseHandle Lib "wininet" (ByRef hInet As Long) As Long
Private Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (ByVal hInternetSession As Long, ByVal sServerName As String, ByVal nServerPort As Integer, ByVal sUserName As String, ByVal sPassword As String, ByVal lService As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Private Declare Function FtpSetCurrentDirectory Lib "wininet.dll" Alias "FtpSetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpGetCurrentDirectory Lib "wininet.dll" Alias "FtpGetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszCurrentDirectory As String, lpdwCurrentDirectory As Long) As Long
Private Declare Function FtpCreateDirectory Lib "wininet.dll" Alias "FtpCreateDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpRemoveDirectory Lib "wininet.dll" Alias "FtpRemoveDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpDeleteFile Lib "wininet.dll" Alias "FtpDeleteFileA" (ByVal hFtpSession As Long, ByVal lpszFileName As String) As Boolean
Private Declare Function FtpRenameFile Lib "wininet.dll" Alias "FtpRenameFileA" (ByVal hFtpSession As Long, ByVal lpszExisting As String, ByVal lpszNew As String) As Boolean
Private Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" (ByVal hConnect As Long, ByVal lpszRemoteFile As String, ByVal lpszNewFile As String, ByVal fFailIfExists As Long, ByVal dwFlagsAndAttributes As Long, ByVal dwFlags As Long, ByRef dwContext As Long) As Boolean
Private Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal hConnect As Long, ByVal lpszLocalFile As String, ByVal lpszNewRemoteFile As String, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
Private Declare Function InternetGetLastResponseInfo Lib "wininet.dll" Alias "InternetGetLastResponseInfoA" (lpdwError As Long, ByVal lpszBuffer As String, lpdwBufferLength As Long) As Boolean
Private Declare Function FtpFindFirstFile Lib "wininet.dll" Alias "FtpFindFirstFileA" (ByVal hFtpSession As Long, ByVal lpszSearchFile As String, lpFindFileData As WIN32_FIND_DATA, ByVal dwFlags As Long, ByVal dwContent As Long) As Long
Private Declare Function InternetFindNextFile Lib "wininet.dll" Alias "InternetFindNextFileA" (ByVal hFind As Long, lpvFindData As WIN32_FIND_DATA) As Long
Private Const PassiveConnection As Boolean = True
'Ftp Declares end
'Additonal
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
'Timer related
Private Const modMinutes = 1 'set in minutes when to execute command
Dim currentTick As Integer
Sub Upload_WANIP(FS As String, UN As String, P As String, F As String, FilePath As String, UploadedFilename As String)
'FS=FTP SERVER
'UN=USERNAME
'P=PASSWORD
'F=FOLDERNAME TO CREATE ON FTP
'FilePath=Location of file on your pc to upload
'UploadedFilename=The filename of the uploaded file

DoEvents
Dim hConnection As Long, hOpen As Long, sOrgPath  As String
    hOpen = InternetOpen("FTP", INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
    Sleep 500
    DoEvents
    hConnection = InternetConnect(hOpen, FS, INTERNET_DEFAULT_FTP_PORT, UN, P, INTERNET_SERVICE_FTP, IIf(PassiveConnection, INTERNET_FLAG_PASSIVE, 0), 0)
    Sleep 500
    DoEvents
    sOrgPath = String(MAX_PATH, 0)
    FtpGetCurrentDirectory hConnection, sOrgPath, Len(sOrgPath)
    Sleep 500
    DoEvents
    FtpCreateDirectory hConnection, F
   Sleep 500
   DoEvents
    FtpSetCurrentDirectory hConnection, F
  Sleep 500
   DoEvents
    FtpPutFile hConnection, FilePath, UploadedFilename, FTP_TRANSFER_TYPE_ASCII, 0
   Sleep 500
   DoEvents
    FtpSetCurrentDirectory hConnection, sOrgPath
     Sleep 500
   DoEvents
    InternetCloseHandle hConnection
     Sleep 500
   DoEvents
    InternetCloseHandle hOpen
 Sleep 500
   DoEvents
End Sub
Private Sub Form_Load()
currentTick = 0
Timer1.Enabled = True
Timer1.Interval = 60000

End Sub

Private Sub Timer1_Timer()
currentTick = currentTick + 1
    If currentTick >= modMinutes Then

Call Upload_WANIP("ftp.server.com", "username", "password", "WANIP", "c:\wanip.txt", "wanip.txt")
'remember that anyfile you upload to an FTP can't contain any spaces.
'wanip.txt will work
'wan_ip.txt will work
'wan ip.txt won't work
'This rule goes for all methods of FTP

    DoEvents
    currentTick = 0
    End If

End Sub

'=====END OF CODE
Regards,
EGL
0
 
nffvrxqgrcfqvvcCommented:
I have an easy solutution to your problem
It still uses a timer but the timer will execute the function only when it reaches 15 minutes, then it will reset again..
'====================================
Add the following code to the form_declerations section::
'====================================

Private Const modMinutes = 15 'set in minutes when to execute command
Dim currentTick As Integer


'====================================
'In Form_Load add the following code
'====================================

currentTick = 0
Timer1.Enabled = True
Timer1.Interval = 60000

'==========================
Add the following code in a timer::
'==========================

 currentTick = currentTick + 1
    If currentTick >= modMinutes Then

'Your upload function here or whatever you want to execute in 15 minutes.
debug.print "15 minutes has been reached"

    DoEvents
    currentTick = 0
    End If

Well that should do it...

Also can I suggest Remote Desktop Connection? This allows you to access your computer from anywere ...Its in start menu / accessories

Regards,
EGL
0
 
nffvrxqgrcfqvvcCommented:
'If you want to watch the status..you can add this line in the Timer right underneath this line::

>>> under this line add below:: currentTick = currentTick + 1


Label1.Caption = modMinutes - currentTick & " minutes left"


Which will return the number of minutes left until the execution of your function.
This would be useful only for testing purposes...so you can get an idea of when the command will be launched
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
nffvrxqgrcfqvvcCommented:
'The flickering of the mouse is probrably your method of uploading the FTP file which probrably when executes freezes the mouse for a split second...I have a work around this it uses FTP API which is IMHO better then using the inet control and much easier. If you want the upload function just let me know.
0
 
gormlyAuthor Commented:
Hi EGL

Thanks, but I am already using a timer in much the same way (almost exactly the same code) and it does only update every 15 minutes.
The problem isn't getting the timer to work, the problem is getting all this to work in the "background."

You see, when my function is "triggered" by my current timer code , whatever I happen to be working on gets interupted somewhat.. like something else is going on. (the update)  It is annoying and I want the ftp update that my program completes to NOT affect any foreground programs or processes, so you see, it is not the timer.. its the function.

I am not a big time programmer, I fiddle around.
I just need to know how to make the function run without affecting any other process.
I thought that was called a "background process"


also
you said:

"Also can I suggest Remote Desktop Connection? This allows you to access your computer from anywere ...Its in start menu / accessories"

No offense, but I just thought that was funny when I read it, you see, the entire reason for writing the Wan IP updater was to use Remote Desktop.
I said: "so I can access my home machine from work", of course, I need to know the IP to connect... right?

LOL, again, no ofense, I just thought that was funny. Thanks for the help.



0
 
gormlyAuthor Commented:
egl1044

Heck YEA!  
If that is my problem, (and I suspect it is) I would LOVE to have a better snippet than using Inet..  My mouse and process issue is when FTP executes because if I just let the email function go, it doesn't effect anything else, but once I use the ftp option, it causes issues.

that would be fantastic.
0
 
nffvrxqgrcfqvvcCommented:
The reason that I declared everything as Private is because It will use alot less memory thus making it a little faster than declaring as public, just a useful tip.
0
 
nffvrxqgrcfqvvcCommented:
Im sure you already know that all you need to do is add this to form_load, but i will add it anyway

if app.previnstance= true then
end
end if

app.taskvisible=false
form1.hide

0
 
gormlyAuthor Commented:
Alright

NO FAIR!

Perfectly executing code that takes care of my problem.
Now I feel guilty.


Wow, that is just fantastic, no more annoying interuptions!!!
This was way 'above and beyong' of what I asked for here, thanks a lot!
0
 
gormlyAuthor Commented:
one question?

I like this method, but it doesn't sem to overwrite the file that is currently on the server.
Is there a way to make it overwrite?

of delete the old before puting the new?
0
 
gormlyAuthor Commented:
never mind, I got it.. I was doing something stupid.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.