Programming a windows service - c#

I have started prgramming a windows service. I have added a notify icon from the toolbox.
It has the small notify icon that appears in the systray as a member of those icons.
It works so far.
So far I have a blank form. I have used the DoubleClick for the notifyIcon to bring up
the form (I will use the form for something later).
Now I have a list of things I want to accomplish to make this work like a true windows
service.
First of all, if possible, I owuld like to remove the maximize and cancel button on the form.
Most windos service apps that I have seen offer the ability to close the app by right-mouse-button clicking on the notify icon which brings up a menu of options.
I see in the properties of the form under Misc there is an CancelButton. But I do not see how do deactivate it. In the Properties of the forum I see under Window Style there is a ControlBox option that, if I turn to false, all three buttons, (minimize, maximize and cancel) go away.
These are not what i am looking for. I would not like the option for them to resize,
maximize or close the form here. I suspect people will close the box intending to
make the box go away while still wanting the app to run.
Under the "Focus" caption in Properties, there id "Deactivate". I have created my own
event/method/function for this and in debug I noticed that when you click on the x-box in the upper right corner, this function is called. The problem is that after the function is
over, the app closes anyway. How do I over-ride this function?
Secondly, how do you catch the right button click event on the notify icon in the
systray?
I can see how to create events for "Click" and "MouseClick" etc. but how so I determine
which button was click?
Using the right buton click is how such programs know when to pull up a menu. So I would
like to know how to do this as well.

Windows services should not display notification icons, nor show forms. You'll find out very soon that post Vista they are explicitly forbid from doing so.
You need to split your application into a user program that may show an icon in the notification tray and shows forms, and the service. They need to be distinct processes and communicate via IPC (shared memory, named pipes, sockets etc).
As to how to react to a righ-click on the notification icon question, you simply need to assign something in your designer to the ContextMenu property:
The menu is shown when the user
right-clicks an icon in the
notification area of the taskbar.
Shortcut menus also are known as
pop-up menus.

Standard Windows services do not run with any user interface at all. Generally speaking, your Windows service project type in Visual Studio won't contain any UI components whatsoever.
The UIs you see when working with other services are secondary applications designed to manage those services. Those may run in the system notification area ("tray"), or may run as minimized windows, may be MMC snap-ins, or may be separate applications the administrator launches on an ad-hoc basis.
As an example, SQL Server runs as a service, whether or not Management Studio or any of the other ancillary UI apps are running.
In any event, the user interface "component" of a service needs to be an entirely separate application. This is particularly important in the post-Vista world, for reasons cited by Remus Rusanu. You'll create a separate WinForms project to do this, and have it send messages to the service to control. (This may include the basics such as start, stop and restart; it usually also includes other service-specific commands.)
You may want to consider reading the following other questions here on StackOverflow for some guidance on the standard Windows Service/UI helper application pattern:
What can Services do under Windows?
What is the difference between a windows service and a regular application?
Creating a user interface for monitoring and interacting with a running windows service
Running an exe from windows service that interacts the the user’s desktop
Why do forms fail in Windows Services

Related

How do I show status to users from my UWP app?

We are creating an application(must be UWP for Microsoft app store). The application will remain running and we want to “hide” the UI from the desktop after they log in (not in taskbar). We want to put it somewhere where they can see the status of the application and/or logout (like a VPN or Anti-virus app shows status).
We have investigated using systray (non existent in uwp), quick actions tray (seems to be for system applications), notifications (show status there, but user can remove it—looking for something like the Action Center buttons for Wifi). Our goal is to have an application running, but out of the way, with access to the status and logout functionality. What is the Microsoft approved way? If there is no such approved way, how have you solved this?
At this point, that's not supported in UWP.
You can however publish WPF applications to the Microsoft Store using Desktop Bridge. I haven't touch it, but I've seen a few apps in the store running more elevated access than UWP and you may have the ability to control the visibility of the application's icon (ie taskbar/systemtray).
Give it a try and let us know if it works.
You're right, but there are some workarounds that can be used:
1. USING THE DISMISSED EVENT to show the notification again if user dismissed it.
2. UWP APP WITH SYSTRAY EXTENSION
As explained in this blogpost, you can add a component that will create the tray icon.

Implementing separate application instances in taskbar like Skype

I noticed for a few updates of Skype now that if you start 2 or 3 instances of Skype, in the windows taskbar they appear as separate windows and you can drag them individually as opposed to other applications when they are "glued" together and you can drag them all at once.
My question is how can I implement the individual appearance in my application and is it possible from C# or through winapi?
The shell groups windows in the taskbar using each window's Application User Model ID (AppUserModelID).
By default, every window generated by a given EXE (even in different processes) shares a system-generated AppUserModelID.
You can give each process its own AppUserModelID by calling SetCurrentProcessExplicitAppUserModelID. From your description this is probably what Skype is doing, though I haven't checked.
You can give each window its own AppUserModelID by setting a different PKEY_AppUserModel_ID property on the windows.
Note that these IDs are required to have a particular format:
CompanyName.ProductName.SubProduct.VersionInformation
Raymond Chen wrote an article about this, and it's also worth reading the documentation I linked to.
I'm not aware of WinForms having explicit support for this, but you could certainly use interop to call the Win32 API directly.

How to get handle for menu items of an application running in the back ground from an application in C#

I am working on a application, which would be the face of lot of other tools running in background. I am facing an issue. During the launch of a background application ,it needs to load a specific file(file-->load--> file name).
Let the front end application be Fapp and the background application be Bapp. Is it possible for Fapp to get the handle of Bapp's menu item and trigger the load function. I am able to get the handle for buttons but not able to do the same for menu items.
Now we are achieving this using AutoIt, I am trying to achieve this in C# itself.
After you have obtained the handle of the window that you want to invoke its menus, then you may use
HMENU GetMenu(HWND) windows api to get menu
HMENU GetSubMenu(HMENU, int) to get to the file menu and again to open menu.
BOOL GetMenuItemInfo( ... ) to get info about menu
and you can use PostMessage((IntPtr)hWnd, WM_COMMAND, 0, ID_MENU_ITEM); (related post) to perform a click on that item.
all these apis are what AutoIt calls (I think). This solution works if your Bapp is a normal windows application with a normal windows menu, not a fancy WPF app, or a ribbon. If this is the case, then what you see as menu probably is not a menu (technically anyway)
Are you sure this is the right way to get two applications talking to each other?
If you don't have source code for BApp, and it also doesn't have an API that you can use, then pretending to be an interactive user could be the only way to interact with it. Just be aware that it is fraught with issues, consider what will happen when
BApp isn't already running
BApp has a modal dialog open
BApp is in the middle of an operation (or hanging) and its menu is disabled
BApp is updated to a new version and its UI changes
An interactive user changes focus, in the middle of an operation.
An alternative to this would be to do the same thing that you do when you are unit testing an application with a UI. This is because you are doing the same thing, automating an application by making calls that execute its functions, in this case to test the results are as expected. Since this is a WPF post lets assume that you are writing an application with MVVM, and the best way (to avoid brittleness when we are change the UI) is to ignore the UI (View) and call the layer that sits underneath i.e. the VM (ViewModel).
In fact its quite easy just to add a self-hosted WCF connection inside your BApp application so that it can be called externally.
this._host = new ServiceHost(service);
this._host.AddServiceEndpoint(typeof(IContract), new NetTcpBinding(), address);
this._host.Open();
This would then enable you to get the two talking totally independently.
If your Bapp is able to somehow invoke Win32 API - then this can be achieved by sending a custom WM_USER message to your Fapp - using SendMessage(). In your Fapp you handle this message and take appropriate action.
I don't think getting handle to a control and invoking its handler is the right way.

Smoother Transition between application in windows app, c#

My application handles the startup and termination of external some application.
An inbuilt TaskBar is there to tab the applications that are started.
Say when three applications are started, i map those 3 applications to the tabs created. So that the user can use the task bar to switch between the application. The default windows task bar will be replaced by my applications taskbar because it is a Kiosk application.
All these works fine...
What i want to do is, the toggling between the applications is not smooth, which the user dosent like.
I want to make the change of applications smooth. I am using windows XP.
I am calling ShowWindowsAsync function to show the application when user clicks the respective tab.
How can i make the the visual effective using c# ? Is there any possibility ?
P/Invoke AnimateWindow() to get transitions. Visit pinvoke.net for the required declarations.

Automatically run programs on startup and stop applications from memory on PPC

I have to stop applications when the window is closed. The window stays in the memory when click the (x) button. How can i remove the app in memory? Also another question is that i want the application to be installed when hard restart the pocket pc, how can i do it?
Thanks
By default the form will just hide when you click the X in the top right. You need to set the "MinimizeBox" property of the form to "False" for the application to close instead.
Installing the application on hard restart (often referred to as cold boot) requires that you put a CAB file for the application on the flash (persistent) memory of the device. You will then normally have to write a script and place that somewhere to call the CAB. This can vary from device to device so you'll have to look that one up.
Setting the form's MinimizeBox property to False causes the OK button to appear.
The Ok button is for closing the application instead of minimalizing
To prevent to application from minimizing which is the default behavior for PocketPC applications, set the MinimizeBox option to false on the form.
As for your second option that might be trickier because it depends on the PDA. If it has persistent memory you could put it there, and find a way to run the CAB file during initialization.
[Update]
It would appear that there is a Startup Folder in the windows menu. Perhaps you could write a utility that checks if your application is installed and if not launches the CAB installer.
The default behavior is indeed minimizing the application. Using a custom task manager which replaces the [x] you can tune this behavior. Some vendors (for instance HTC) include a custom taskmanager exactly for this purpose.
A generic but good task manager is WkTASK, here is the relevant feature description:
X button
Using the X button, you can
use some actions as follows:
Tap: really close
Tap&Hold: show context menu
Drag: some gestures (To show a program launcher, drag toward down. To
show Today, drag toward left.)
WkTASK offers a lot more, but you can fine tune it to do only what you want.

Categories