Solved

Scheduled Task - SysTray application

Posted on 2004-08-20
26
406 Views
Last Modified: 2010-05-02
I have a VB 6 systray application that seems to work perfectly fine when started manually (i.e. double-clicking the exe).  What I would like is for the application to start via a scheduled task and appear in the system tray.  It would appear as though I simply cannot create a scheduled task to start the application and have it appear in the system tray.

So, what I guess I am asking is... has anybody created a system tray application that will appear when started via a scheduled task?  Will changing it to be a service help?

Thanks
0
Comment
Question by:dtomyn
  • 10
  • 6
  • 5
  • +2
26 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
I just scheduled an app that runs in the systray and it ran just fine.

Do you get any error messages?

Does the entry in the scheduler have a "Next Run Time", "Last Run Time" or "Status"?

Idle_Mind
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
The problem is that the scheduler service (as well as the services service) only have access to the console desktop. By default, the local login refers to that desktop, but as from Windows XP, this is no longer the case.
If your scheduler run as LOCALSYSTEM without the check "interact with desktop", that could be a step forward.

In any other case, you will need to develop 2 applications:
1) started by the scheduler
2) one sitting (all the time) in the systray (maybe hidden while the first one is not running), and displaying/interacting as needed

I have done this now 3 times for real-world projects, and it is working fine. This way, you can even have the systray icon be deployed on remote servers for supervising users that have to look at my application/services...

CHeers
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
dtomyn,

Have you created a SERVICE or just an application that resides in the SYSTEM TRAY?

Idle_Mind
0
 
LVL 5

Author Comment

by:dtomyn
Comment Utility
Idle_Mind...
This is just a regular VB 6 exe.  It is not a service currently (I suppose I could make it a service, but at this point I would rather not).

angelIII...
The OS is Windows 2000 so I am not sure if I am running into what you are describing above.  I believe that this is the case though since it would definitely make sense (and is what I was guessing was the case).  A couple of questions for you:
1) Can you be a bit clearer as to what you mean by "without the check interact with desktop"?  That is I don't know where this option is.
2) I am curious as to your comment regarding having 2 applications.  How do you get the application sitting all the time in the systray to start with?  That is, I am not sure I am following exactly what you are saying since I would think I would have the exact same problem.

Thanks again
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
Comment Utility
angelIIIs comments only applied if you were writing a service.

Here is a basic systray application that you can test with.  Create a new project and add a PictureBox.  Set the PictureBox Picture property to an icon to use in the system tray.  The compiled application can only be closed from the Task Manager.

Private Declare Sub Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA)

Private Type NOTIFYICONDATA
    cbSize As Long
    hWnd As Long
    uID As Long
    uFlags As Long
    uCallbackMessage As Long
    hIcon As Long
    szTip As String * 64
End Type

Private Const NIM_ADD = &H0
Private Const NIM_MODIFY = &H1
Private Const NIM_DELETE = &H2
Private Const NIF_MESSAGE = &H1
Private Const NIF_ICON = &H2
Private Const NIF_TIP = &H4
Private Const NIF_DOALL = NIF_MESSAGE Or NIF_ICON Or NIF_TIP
Private Const WM_MOUSEMOVE = &H200
Private Const WM_LBUTTONDBLCLK = &H203
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_RBUTTONDOWN = &H204

Private tic As NOTIFYICONDATA
Private inTray As Boolean

Private Sub Form_Load()
    Picture1.Visible = False
    Me.WindowState = vbMinimized
End Sub

Private Sub Form_Resize()
    If Me.WindowState = vbMinimized Then
        dropToTray
    End If
End Sub

Private Sub dropToTray()
    tic.cbSize = Len(tic)
    tic.hWnd = Picture1.hWnd ' Callback Control
    tic.uID = 1&
    tic.uFlags = NIF_DOALL
    tic.uCallbackMessage = WM_MOUSEMOVE
    tic.hIcon = Picture1.Picture
    tic.szTip = "My Application" & Chr$(0)
   
    Me.Visible = False
    Shell_NotifyIcon NIM_ADD, tic
    inTray = True
End Sub

Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    X = X / Screen.TwipsPerPixelX
    Select Case X
        Case WM_LBUTTONDBLCLK
            Shell_NotifyIcon NIM_DELETE, tic
            Me.Visible = True
            Me.WindowState = vbNormal
            Me.Show
            inTray = False
    End Select
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    Select Case UnloadMode
        Case vbAppTaskManager, vbAppWindows
            If inTray Then
                Shell_NotifyIcon NIM_DELETE, tic
            End If

        Case Else
            Cancel = True
            If Me.Visible = True Then
                dropToTray
            End If
           
    End Select
End Sub
0
 
LVL 5

Author Comment

by:dtomyn
Comment Utility
Also, the scheduled task itself looks fine.  i.e. when I start it up it has a status of "Running" (I currently have the task itself disabled therefore it has no next run date).
0
 
LVL 5

Author Comment

by:dtomyn
Comment Utility
Just saw your code posting.  I'll check out the code now...
0
 
LVL 5

Author Comment

by:dtomyn
Comment Utility
Idle_Mind,
Thanks for the time in posting some code.  However, this still does not work for me.  That is, when I run the exe by itself it works, but if I schedule it to run it works in terms of it running, but it does not show up in the system tray.  i.e. I know it is running since I the scheduled tasks say "Running" and I see it in the task manager if I choose "Show processes from all users".
NOTE: I am running Windows 2000 Server on both my development machine and my destination machine (it behaves the same way on both machines).  Also, what account are you running your scheduled task under?  It is currently scheduled under my own account.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
I am running WinXp Pro with an Administrator account.  When I scheduled the application I had to type in the the password form my account.

Idle_Mind
0
 
LVL 5

Author Comment

by:dtomyn
Comment Utility
Hmmm... I don't have access to a Win XP machine right now.  I will install Visual Studio 6 on my XP laptop tonight and try this out then.  I am wondering if there is a difference between XP and 2000 Server in regards to this.  BTW, my account is a local admin on the box.
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
Idle_Mind's comment at 1:21PM applies to Windows 2000 also.  You need to provide the account name and password the scheduled task will run under.  From my perspective the first thing to do is sort out whether it's a problem with your program or something to do with the Task Scheduler.  Can you schedule and run any tasks at all?  If not, then it's a scheduling issue.  If other tasks will run from the scheduler, then can you create a copy of your program, one that doesn't use the system tray, and see if it'll run as a scheduled task.  If it does, then it's something to do with the code that puts the task in the tray.  Otherwise, it's something with the program itself.  
0
 
LVL 5

Author Comment

by:dtomyn
Comment Utility
I realize you have to provide the account name and password in order for the scheduled task to run.  That is not what I am commenting on.  Instead, when I run my application or the code that Idle_Mind posted earlier it does not create a system tray when ran via a scheduled task.  If I start it manually everything works fine.  If I start it via the scheduled task the application runs but the system tray icon never appears (likely because the process has its own sort of "virtual desktop" that the icon goes to).  The application itself though IS running as I can see it in the Task Manager (but only after checking the option to see all processes).

So, just to be clear then, I have no issues scheduling tasks.  The problem is scheduling a task that is a VB 6 application that puts an icon in the system tray.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 76

Expert Comment

by:David Lee
Comment Utility
Idle_Mind's last post said "I am running WinXp Pro with an Administrator account.  When I scheduled the application I had to type in the the password form my account." and your reply "I don't have access to a Win XP machine right now.  I will install Visual Studio 6 on my XP laptop tonight and try this out then." led me to believe that that you might not know that you have to supply account information in Windows 2000 also.  Sorry that I misunderstood.

I'm on a Windows 2000 box right now myself and I just tried Idle_Mind's sample code as a scheduled task.  It worked fine.  The icon appeared in the tray right on schedule.  So we know that Idle_Mind's code works on a 2000 machine.  Since neither it nor your app works properly on your PC, it seems to me that it's something about it, the PC or its configuration.  Or perhaps something to do with VB on that PC.  If you'd like to test that possibility, then I've placed a compiled version of Idle_Mind's code on my web site at this address: http://www.mindspring.com/~d_lee/.  It's called STrayTest.Exe.  You can download it, schedule it, and see what happens.  If it works you'll see a stop sign appear in your system tray.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
You have gone above and beyond the call of duty BlueDevilFan.  Even if you don't solve this problem, I hope you at least get an Assist on this question.

=)
Idle_Mind
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
Thanks!  You did the hard part though.  I'm just following along with troubleshooting ideas.
0
 
LVL 5

Author Comment

by:dtomyn
Comment Utility
Thanks for testing on a Win 2000 machine.  This can then only lead me to believe that there is some configuration that is preventing the icon in the system tray from displaying.  I will test myself on my own Win 2000 machine (once I find an install disk so that I can install in my Virutal PC environment on my laptop).
0
 
LVL 5

Author Comment

by:dtomyn
Comment Utility
Well, I tested it on my own Win 2000 and just like everyone is saying, it works.  So, now I am baffled as to why it does not work on these work machines.  I will update and award points once I figure this out (or someone helps me to figure this out).
0
 
LVL 3

Expert Comment

by:aParser
Comment Utility
Sorry if I'm adding more smoke than fresh air, but it looks like folks were confused about what AngelIII said -- and it made sense from my past experience with some scheduled tasks.

I understand that dtomyn's application isn't a service.  The Task Scheduler, however, *is* a service and that service has to run under a user ID.  This is a different thing than the user ID and password you give Task Scheduler when you schedule a task.

Under Windows 2000, go Start | Programs | Administrative Tools | Services.  Find "Task Scheduler", right-click it, and select Properties.  There will be four tabs, the second of which is Log On.  Under that tab is a radio button to select either "Local System account" or "This account".  If the "Local System account" radio button is selected, there is a checkbox to allow the *service* to interact with the local desktop.  This is the option that AngelIII was talking about and may be the "variable" that's allowing some machines to show the application in the tray and other machines to not.  Since it's the *service* that's launching the application, I would think that the service would have to be able to get at the local desktop, even if the application is running with a different user's permissions.

Again, my apologies if this makes things more confusing or if I'm just flat wrong.  Good luck!
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
aParser,

That doesn't appear to be the problem.  I can uncheck the "Allow service to interact with desktop" checkbox and when the scheduler launches the program the program's icon still shows up in the system tray.  So at least on my system that checkbox has no effect on the icon appearing.
0
 
LVL 3

Expert Comment

by:aParser
Comment Utility
You're absolutely right, BlueDevilFan.  Using your test application, I verified your results, but I also did want to maybe help clarify what AngelIII was talking about.

In my test, however, I did (somewhat) duplicate dtomyn's anomaly.  I tried running the task under two different user IDs (from the Run as: setting of the scheduled task).  The first time, as myself, the scheduled item showed up in my tray.  The second, with the task running as a different user ID, the item did NOT show in the tray, but DID show in the task list.

Dtomyn, is it at all possible that in the cases where the item wasn't showing up in the tray, that you were logged in as a different user from the username you used to schedule the task? possibly even the same username, but in one case a domain user ID versus a local user ID?
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
Recommend accepting Idle_Mind http:#11853016
0
 
LVL 5

Author Comment

by:dtomyn
Comment Utility
Honestly, none of the comments helped me.  As a result, I would prefer to delete this question, but if I hear objections I guess I will accept somebody's answer.  However, since none of them were a solution to my problem I will have to grade it low.

Any comments?

As for the solution I implemented... well, it was decided that it was not worth trying to come up with a solution to this one.  As a result, we took a completely different approach.
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
"Honestly, none of the comments helped me."
The comments must've helped some.  In your 08/23/2004 12:20PM post you said, "Well, I tested it on my own Win 2000 and just like everyone is saying, it works."  That sounds like you felt the work done up to that point had helped demonstrate that the code works and it's possible to do what you wanted to do.  Certainly Idle_Mind's sample code helped get to this point.  
0
 
LVL 5

Author Comment

by:dtomyn
Comment Utility
I suppose it helped in that others have shown that they could get it working and that I could get it working, but I already had code that worked (the code posted was just more of the same).  The issue seems to be a security one.
Anyway, I am glad that people at least tried to help, so I will give the points to Idle_Mind.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now