Tải bản đầy đủ

Chapter 2: Creating and managing Windows services - Nguyễn Đức Cương

' (
+

!
, -.
.

"# $ %&
) ' (
* '
*
* '


&RQWHQW
!
!
"

#!
!


'
%

%

$
'((

) ( &

+

&
*)
!

!
&



%&



,-

!


8QGHUVWDQGLQJ :LQGRZV 6HUYLFHV
,

-

,&
&
/
,



!
-

!
# ((

-

(

&

!





.


' HV F U L E H : L QG RZ V V HU Y L F HV
!
'
(

- 1

!

(

*
( )

((

(
(
!

!

+

&

2 +3

!
'





!

4

!

&

0


' HV F U L E H : L QG RZ V V HU Y L F HV



The Services window of the Administrative Tools component


5


' HV F U L E H : L QG RZ V V HU Y L F HV
!
(
!

7
((

!
!





&

((
+

6


' HV F U L E H : L QG RZ V V HU Y L F HV
!

-

#

! 9
1

# :

(

&
!
! %

&

! ;

! 9

%

" #<=$
> '$
=+' "% #? < #+?
? !
!
() (




)

+

)

!
8


' L I I HU HQWL D WH E HWZ HHQ : L QG RZ V V HU Y L F HV D QG
RWK HU  1 ( 7 D S S O L F D WL RQV
%

!
1
!

((

/

-&

,-

( A

)
!

!
:5) :BB1
-

(

#*

&
!

C
<

!

!
((

! -



! )
!



C

&

+
+

&

-*

@


3URJUDPPLQJ 0RGHO RI :LQGRZV 6HUYLFH $SSOLFDWLRQV
! 9

(
,

! ;

! 9
%
! %

<

!
! ;
!
((
&
(
!

(

&

((

!
!





D


3URJUDPPLQJ 0RGHO RI :LQGRZV 6HUYLFH $SSOLFDWLRQV
,

(
(

,
#! $

'
,-

!
!

(

( !
( (
*

(
&

9 &
F





BE


3URJUDPPLQJ 0RGHO RI :LQGRZV 6HUYLFH $SSOLFDWLRQV
+

&

! ;

+

,
( &

1
)
)( &

>
( &
(

>9

(
! ( &
F
) !

1

!

( &
-

1

!

( &

)

)

)
>

(

( &
(

!




BB


3URJUDPPLQJ 0RGHO RI :LQGRZV 6HUYLFH $SSOLFDWLRQV
+

&

! ;

+

,
( &

>
>

,

2
(

!
& (

( &
( &

3

1

-

!
!

(

*

>
>9

( &

#!

!
!

!





( &

(
-

B


3URJUDPPLQJ 0RGHO RI :LQGRZV 6HUYLFH $SSOLFDWLRQV
+

&

!

+

,

(

,

&
&

C

!
!

9

# !
9

!

C&

C&

!

#*

!

&

(

*

&

( (

!
(

(




!

(

!
B.


/HVVLRQ   & U HD W LQJ : LQG RZ V 6 HU Y LF HV
- 1
& ( (
!
((
' &
((





!

((
&
!

B0


& UHDWH D E ODQN :LQGRZV VHUYLFH DSSOLFDWLRQ





The Visual Studio .NET New Project dialog box
B5


& UHDWH D E ODQN :LQGRZV VHUYLFH DSSOLFDWLRQ





A Windows service solution

B6


& UHDWH D E ODQN :LQGRZV VHUYLFH DSSOLFDWLRQ
!
&
((

(
+

&

!

static void Main()
{ System.ServiceProcess.ServiceBase[ ] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[ ]
{ new Service1() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}





B8


6RPH WK H ' HIDX OW 3URSHUWLHV :6$
' $
!
(

9
"

'
9

#!
(

(

)





B@


$GGLQJ ) X QFWLRQDOLW\ WR D 6HUYLFH $SSOLFDWLRQ

protected override
override void
void OnStart(string[
OnStart(string[ ]] args)
args)
protected
{{
//Code here
here
//Code
}}
protected override
override void
void OnStop(string[
OnStop(string[ ]] args)
args)
protected
{{
//Code here
here
//Code
}}




BD


$ G G L QJ ) X QF WL RQD O L W\ WR D 6 HU Y L F H $ S S O L F D WL RQ
protectedoverride
overridevoid
voidOnStart(string[]
OnStart(string[]args)
args)
protected
{{
FileStreamfs
fs==new
newFileStream(@"c:\MyWindowsService_Start.txt",_
FileStream(@"c:\MyWindowsService_Start.txt",_
FileStream
FileMode.OpenOrCreate,FileAccess.Write);
FileAccess.Write);
FileMode.OpenOrCreate,
StreamWritersr
sr==new
newStreamWriter(fs);
StreamWriter(fs);
StreamWriter
sr.WriteLine("MyWindowsService_CSstarted");
started");
sr.WriteLine("MyWindowsService_CS
sr.Flush();
sr.Flush();
}}
protectedoverride
overridevoid
voidOnStop()
OnStop()
protected
{{
FileStreamfs
fs==new
newFileStream("C:\\Temp\\MyWindowsService_Stop.txt",_
FileStream("C:\\Temp\\MyWindowsService_Stop.txt",_
FileStream
FileMode.OpenOrCreate,FileAccess.Write);
FileAccess.Write);
FileMode.OpenOrCreate,
StreamWritersr
sr==new
newStreamWriter(fs);
StreamWriter(fs);
StreamWriter
sr.WriteLine("MyWindowsService_CSStopped");
Stopped"); sr.Flush();
sr.Flush();
sr.WriteLine("MyWindowsService_CS
sr.Close();
sr.Close();
}}




E


+ D QG O L QJ ( Y HQWV RI D : L QG RZ V 6 HU Y L F H
#!
(#!
9

#! 7
9

'

( (

>9

#! 7
9

'

( (

>




B


/ RJ J L QJ , QI RU P D WL RQ L Q 6 \ V WHP

( Y HQW / RJ V

!
(

!

!

'((
!
((





(


/ RJ J L QJ , QI RU P D WL RQ L Q 6 \ V WHP
,

(
'

'

$ ( (
&
( (





( Y HQW / RJ V

#! $

(
$ ( (

.


&U HD WL QJ &X V WRP

( Y HQW / RJ V
#!

9 &

&
(B7
( 7'

(7
'

$ ( (

:

#! $

(.7 ( & !
$ ( (

(

&

( (

)

!
!!





"

0


&U HD WL QJ &X V WRP

( Y HQW / RJ V

private System.Diagnostics.EventLog eventLog1;
…..
private void CreateEventLog()
eventLog1 = new System.Diagnostics.EventLog();
{

}

if (!System.Diagnostics.EventLog.SourceExists("Transaction Service"))
{
System.Diagnostics.EventLog.CreateEventSource("Transaction Service",_
"Transaction Log");
}
eventLog1.Source = "Transaction Service";

protected override void OnStart(string[] args)
{
CreateEventLog();
eventLog1.WriteEntry("MyWindowsService_CSharp Started",
EventLogEntryType.Information);
}




_

5


Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay

×