. NET implements an interactive Windows service instance Code _ practical Tips

Source: Internet
Author: User

These days want to do a file monitoring services, look at the Internet on the Windows services, the number of articles, are only about how to do a basic service, but did not tell how to interact with the user. See MSDN for a description of the service:

Windows Service applications run in a window area that is different from the interactive area of the logged-on user. A window area is a security object that contains a clipboard, a set of global atoms, and a set of desktop objects. Because the Windows Service's zone is not an interactive zone, the dialog box raised in the Windows Service application will be invisible and may cause the program to stop responding. Similarly, error messages should be recorded in the Windows event log, not in the user interface.

The. NET Framework supports Windows service classes that do not support interaction with the interactive zone, which is the logged-on user. At the same time, the. NET Framework does not contain classes that represent regions and desktops. If the Windows service must interact with other zones, you need access to the unmanaged Windows APIs.

In other words, we want to implement interactive services (such as we want to give the service to do some parameter setup at runtime, etc.), then we must have a using System.Runtime.InteropServices

So look at it if you can implement an interactive service. The steps are the same as implementing basic services (you can refer to MSDN or Google online).

When implementing OnStart, be aware that there is no pop-up form or anything. There is no reaction to this. We can run a thread in this method. The thread needs to access the window area object or the desktop object, and of course the framework does not provide these, to access unmanaged code.

Take a look at the code and run it again.

Copy Code code as follows:

Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Diagnostics;
Using System.ServiceProcess;
Using System.Threading;
Using System.Runtime.InteropServices;
Namespace Filewatchservice
{
Publicclass Service1:System.ServiceProcess.ServiceBase
{
///
The required designer variable.
///
Private System.ComponentModel.Container Components =null;
Thread Threadform =null;
Public Service1 ()
{
This call is required for the Windows.Forms Component Designer.
InitializeComponent ();

TODO: Add any initialization after the initcomponent call
}

        #region Component Designer generated code
       // /
       ///Designer supports the required methods-do not use the Code Editor
        ///Modify the contents of this method.
       ///
privatevoid InitializeComponent ()
         {
           //
            //Service1
            //
this. ServiceName = "Jadewatchservice";

}
#endregion
[STAThread]
Staticvoid Main ()
{
System.ServiceProcess.ServiceBase.Run (New Service1 ());

}
///
Clean up all resources that are in use.
///
Protectedoverridevoid Dispose (bool disposing)
{
if (disposing)
{
if (Components!=null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}

///
Set the specific action so that the service can perform its work.
///
Protectedoverridevoid OnStart (string[] args)
{
Threadform =new Thread (new ThreadStart (formshow));
Threadform.start ();
}

///
Stop this service.
///
Protectedoverridevoid OnStop ()
{
if (Threadform!=null)
{
if (threadform.isalive)
{
Threadform.abort ();
Threadform =null;
}
}
}

void Formshow ()
{

GetDesktopWindow ();
IntPtr Hwinstasave = Getprocesswindowstation ();
IntPtr dwThreadID = GetCurrentThreadID ();
IntPtr Hdesksave = Getthreaddesktop (dwThreadID);
IntPtr Hwinstauser = openwindowstation ("WinSta0", false, 33554432);
if (Hwinstauser = = IntPtr.Zero)
{
Rpcreverttoself ();
Return
}
SetProcessWindowStation (Hwinstauser);
IntPtr Hdeskuser = OpenDesktop ("Default", 0, False, 33554432);
Rpcreverttoself ();
if (Hdeskuser = = IntPtr.Zero)
{
SetProcessWindowStation (Hwinstasave);
Closewindowstation (Hwinstauser);
Return
}
SetThreadDesktop (Hdeskuser);

IntPtr dwguithreadid = dwThreadID;

Form1 F =new Form1 (); This FORM1 can be brought with NotifyIcon, can be displayed in the tray, the user can click on the tray icon to set
System.Windows.Forms.Application.Run (f);


Dwguithreadid = IntPtr.Zero;
SetThreadDesktop (Hdesksave);
SetProcessWindowStation (Hwinstasave);
Closedesktop (Hdeskuser);
Closewindowstation (Hwinstauser);
}

[DllImport ("user32.dll")]
Staticexternint GetDesktopWindow ();

[DllImport ("user32.dll")]
Staticextern IntPtr getprocesswindowstation ();

[DllImport ("kernel32.dll")]
Staticextern IntPtr GetCurrentThreadID ();

[DllImport ("user32.dll")]
Staticextern IntPtr getthreaddesktop (IntPtr dwthread);

[DllImport ("user32.dll")]
Staticextern IntPtr openwindowstation (string A, bool B, int c);

[DllImport ("user32.dll")]
Staticextern IntPtr opendesktop (string lpszdesktop, uint dwflags,
BOOL Finherit, uint dwdesiredaccess);

[DllImport ("user32.dll")]
Staticextern IntPtr closedesktop (IntPtr p);

[DllImport ("Rpcrt4.dll", SetLastError =true)]
Staticextern IntPtr rpcimpersonateclient (int i);


[DllImport ("Rpcrt4.dll", SetLastError =true)]
Staticextern IntPtr rpcreverttoself ();

[DllImport ("user32.dll")]
Staticextern IntPtr SetThreadDesktop (IntPtr a);

[DllImport ("user32.dll")]
Staticextern IntPtr setprocesswindowstation (IntPtr a);
[DllImport ("user32.dll")]
Staticextern IntPtr closewindowstation (IntPtr a);
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.