WINDOWS服务可以添加托盘吗?

colin666 2003-01-20 12:45:22
WINDOWS服务可以添加托盘吗?怎么样添加?
...全文
93 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
老大刘 2003-02-25
  • 打赏
  • 举报
回复
我是用消息队列来实现的。
jackyhzzjcn 2003-01-23
  • 打赏
  • 举报
回复


你想怎么交换?

交换定义接口呀,自己,

yourservice.exe -uninstall
yourservice.exe -install
yourservice.exe -config
colin666 2003-01-22
  • 打赏
  • 举报
回复
不用命令行可以吗?我希望用图形界面
colin666 2003-01-22
  • 打赏
  • 举报
回复
能不能简单的给我介绍一下?我希望有应用程序可以和服务进行数据的交换
jackyhzzjcn 2003-01-22
  • 打赏
  • 举报
回复
Console.WriteLine("Approximate round trip times in milli-seconds:");
if (lngPacketsReceived == 0)
{
Console.WriteLine(" Minimum = {0}ms, Maximum = {1}ms, Average = {2}ms", 0, 0, 0 );
}
else
{
Console.WriteLine(" Minimum = {0}ms, Maximum = {1}ms, Average = {2}ms", iMinTransmitTime, iMaxTransmitTime,(int) ((double) (lngTotalTransmitTime/lngPacketsReceived)) );
}
return;
}

/// <summary>
/// This method is used to get the Packet and calculates the total size
/// of the Pack by converting it to byte array
/// </summary>
public static int Serialize(IcmpPacket ThisPacket, byte[] Buffer, int PacketSize, int PingData )
{
int cbReturn = 0;
// serialize the struct into the array
int iIndex = 0;

byte [] b_type = new byte[1];
b_type[0] = ThisPacket.Type;

byte [] b_code = new byte[1];
b_code[0] = ThisPacket.SubCode;

byte [] b_cksum = BitConverter.GetBytes(ThisPacket.CheckSum);
byte [] b_id = BitConverter.GetBytes(ThisPacket.Identifier);
byte [] b_seq = BitConverter.GetBytes(ThisPacket.SequenceNumber);

// Console.WriteLine("Serialize type ");
Array.Copy( b_type, 0, Buffer, iIndex, b_type.Length );
iIndex += b_type.Length;

// Console.WriteLine("Serialize code ");
Array.Copy( b_code, 0, Buffer, iIndex, b_code.Length );
iIndex += b_code.Length;

// Console.WriteLine("Serialize cksum ");
Array.Copy( b_cksum, 0, Buffer, iIndex, b_cksum.Length );
iIndex += b_cksum.Length;

// Console.WriteLine("Serialize id ");
Array.Copy( b_id, 0, Buffer, iIndex, b_id.Length );
iIndex += b_id.Length;

Array.Copy( b_seq, 0, Buffer, iIndex, b_seq.Length );
iIndex += b_seq.Length;

// copy the data
Array.Copy( ThisPacket.Data, 0, Buffer, iIndex, PingData );
iIndex += PingData;
if( iIndex != PacketSize/* sizeof(IcmpPacket) */)
{
cbReturn = -1;
return cbReturn;
}

cbReturn = iIndex;
return cbReturn;
}
/// <summary>
/// Checksum -
/// Algorithm to create a checksup for a buffer
/// </summary>
public static ushort CheckSum( ushort[] BufferToChecksum )
{
int iCheckSum = 0;

for (uint iCount = 0; iCount < BufferToChecksum.Length; iCount++)
{
iCheckSum += Convert.ToInt32( BufferToChecksum[iCount] );
}

iCheckSum = (iCheckSum >> 16) + (iCheckSum & 0xffff);
iCheckSum += (iCheckSum >> 16);
return (ushort)(~iCheckSum);
}

/// <summary>
/// Class that holds the Pack information
/// </summary>
public class IcmpPacket
{
public byte Type; // type of message
public byte SubCode; // type of sub code
public ushort CheckSum; // ones complement checksum of struct
public ushort Identifier; // identifier
public ushort SequenceNumber; // sequence number
public byte[] Data; // byte array of data
} // class IcmpPacket
} // class ping
}
jackyhzzjcn 2003-01-22
  • 打赏
  • 举报
回复

// Convert the server IP_EndPoint to an EndPoint
IPEndPoint ipepServer = new IPEndPoint(PingTarget.AddressList[0],0);
EndPoint epServer = (ipepServer);

// Set the receiving endpoint to the client machine
PingSource = Dns.GetHostByName(Dns.GetHostName());
IPEndPoint ipEndPointFrom = new IPEndPoint(PingSource.AddressList[0],0);
EndPoint EndPointFrom = (ipEndPointFrom);

int iPacketSize = 0;
IcmpPacket PingPacket = new IcmpPacket();
// Construct the packet to send
PingPacket.Type = ICMP_ECHO; //8
PingPacket.SubCode = 0;
PingPacket.CheckSum = UInt16.Parse("0");
PingPacket.Identifier = UInt16.Parse("45");
PingPacket.SequenceNumber = UInt16.Parse("0");
PingPacket.Data = new Byte[iPingBytes];
//Initialize the Packet.Data
for (int iCount = 0; iCount < iPingBytes; iCount++)
{
PingPacket.Data[iCount] = (byte)'#';
}

//Variable to hold the total Packet size
iPacketSize = iPingBytes + 8;
byte [] bytPktBuffer = new byte[iPacketSize];
int iResult = 0;

//Call a Method Serialize which counts
//The total number of Bytes in the Packet
iResult = Serialize(PingPacket, bytPktBuffer, iPacketSize, iPingBytes );

//Error in Packet Size
if( iResult == -1 )
{
Console.WriteLine("Error in Making Packet");
return;
}

/* now get this critter into a ushort array */
ushort [] cksum_buffer = new ushort[Convert.ToInt32( Math.Ceiling( Convert.ToDouble(iResult) / 2))];

//Code to initialize the ushort array
int icmp_header_buffer_index = 0;
for( int iCount = 0; iCount < cksum_buffer.Length; iCount++ )
{
cksum_buffer[iCount] = BitConverter.ToUInt16(bytPktBuffer, icmp_header_buffer_index);
icmp_header_buffer_index += 2;
}
//Call a method which will return a checksum
//Save the checksum to the Packet
PingPacket.CheckSum = CheckSum(cksum_buffer);

// Now that we have the checksum, serialize the packet again
byte [] byteSendBuffer = new byte[ iPacketSize ];
//again check the PingPacket size
iResult = Serialize(PingPacket, byteSendBuffer, iPacketSize, iPingBytes );
//if there is a error report it
if( iResult == -1 )
{
Console.WriteLine("Error in Making Packet");
return;
}

Console.WriteLine("\nPinging {0} [{1}] with {2} bytes of data:\n", strPingHost,
ipepServer.Address.ToString(), iPingBytes);

//check for continuous
if (lngPingCount == 0) bContinuous = true;

//Loop the ping
long lngPacketsSent = 0, lngPacketsReceived = 0, lngTotalTransmitTime = 0;
int iMinTransmitTime = int.Maxvalue, iMaxTransmitTime = int.Minvalue;
do
{
bool bReceived = false ;

//Initialize a Socket of the Type ICMP
Socket PingSocket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
//Set socket timeout, but this doesn't seem to work...
PingSocket.SetSocketOption( SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, (int) lngPingTimeout);
PingSocket.SetSocketOption( SocketOptionLevel.Socket, SocketOptionName.SendTimeout, (int) lngPingTimeout);

// Initialize the buffers. The receive buffer is the size of the
// ICMP header plus the IP header (20 bytes)
byte [] ReceiveBuffer = new byte [MAX_PACKET_SIZE];

dwStart = System.Environment.TickCount; // Start timing
//Gather stats
lngPacketsSent ++;
//send the Pack over the socket
iResult = PingSocket.SendTo(byteSendBuffer, iPacketSize, SocketFlags.None , epServer);
if ((iResult) == SOCKET_ERROR)
{
Console.WriteLine("Socket Error cannot Send Packet");
}
//Receive the bytes
iBytesReceived = 0;
//loop while waiting checking the time of the server responding
while(!bReceived)
{
try
{
iBytesReceived = PingSocket.ReceiveFrom(ReceiveBuffer, MAX_PACKET_SIZE, SocketFlags.None, ref EndPointFrom);
}
catch //(Exception e)
{
//Console.WriteLine ("Request timed out. \n{0}", e.Message);
Console.WriteLine ("Request timed out. \n");
bReceived = false;
break;
}
if (iBytesReceived == SOCKET_ERROR)
{
Console.WriteLine("Host not Responding") ;
bReceived = false;
break;
}
else if (iBytesReceived > 0)
{
bReceived = true;
dwStop = System.Environment.TickCount - dwStart; // stop timing

//Check for timeout
if ( dwStop > lngPingTimeout)
{
Console.WriteLine ("Request timed out.");
bReceived = false;
System.Threading.Thread.Sleep(System.TimeSpan.FromMilliseconds(1000));
break;
}
if (dwStop < 10)
{
Console.WriteLine("Reply from {0}: bytes: {1} time:<10ms", ipepServer.Address.ToString(), iBytesReceived - 28);
}
else
{
Console.WriteLine("Reply from {0}: bytes: {1} time: {2}ms", ipepServer.Address.ToString(), iBytesReceived - 28, dwStop);
}
break;
}
}//while

//Gather stats
if (bReceived)
{
lngPacketsReceived++;
lngTotalTransmitTime += dwStop;
if (dwStop > iMaxTransmitTime) iMaxTransmitTime = dwStop;
if (dwStop < iMinTransmitTime) iMinTransmitTime = dwStop;
}
iLoop++;

if (bReceived &
(bContinuous | (iLoop < lngPingCount))) //not last ping
{
System.Threading.Thread.Sleep(System.TimeSpan.FromMilliseconds(1000));
}
//close the socket
PingSocket.Shutdown(SocketShutdown.Both);
PingSocket.Close();
} while (bContinuous | (iLoop < lngPingCount)); //Do


//Report stats
Console.WriteLine("\nPing statistics for {0}", ipepServer.Address.ToString());
if (lngPacketsSent == 0)
{
Console.WriteLine(" Packets: Sent = {0}, Received = {1}, Lost = {2} ({3}% loss),", lngPacketsSent, lngPacketsReceived, lngPacketsSent-lngPacketsReceived, 0 );
}
else
{
Console.WriteLine(" Packets: Sent = {0}, Received = {1}, Lost = {2} ({3}% loss),", lngPacketsSent, lngPacketsReceived, lngPacketsSent-lngPacketsReceived, ((double) (lngPacketsSent-lngPacketsReceived)/lngPacketsSent)* 100 );
}
jackyhzzjcn 2003-01-22
  • 打赏
  • 举报
回复
传信息到服务就要在你的服务里写了 :)

都是要通过命令行来执行的。

给你一段PING的代码,其实都在里面了 :)
Ping.cs
//----------------------------------------+----------------------------------------
// Ping.cs - Ping utility written in C# and .Net similar to the classic Ping.exe
//
// Copyright?2002, Christopher G. Lewis - HTTP://www.ChristopherLewis.com
//
// Supports
// -t Ping the specified host until stopped.
// -a Resolve addresses to hostnames.
// -n count Number of echo requests to send.
// -l size Send buffer size.
// -w timeout Timeout in milliseconds to wait for each reply.
//
// Added code to reports ping stats.
//
//
// Original version of this code by Lance Olson of MSDN Magazine
// http://msdn.microsoft.com/msdnmag/issues/01/02/netpeers/netpeers.asp
// and Peter A. Bromberg, Ph.D. ( pbromberg@yahoo.com) made it a web service
// acording to http://www.eggheadcafe.com/articles/20020209.asp
//
//----------------------------------------+----------------------------------------

using System;
using System.Net;
using System.Net.Sockets;

namespace Ping
{
/// <summary>
/// The Main Ping Class
/// </summary>
class Ping
{
//Declare some Constant Variables
const int SOCKET_ERROR = -1;
const int ICMP_ECHO = 8;

/// <summary>
/// The Starting Point of the Class
/// It Takes the Hostname parameter
/// </summary>
public static void Main(string[] args)
{
string strHostToPing = "";
bool bLoop = false;
bool bAddressResolve = false;
int iBytes = 32;
uint lngCount = 4;
uint lngTimeout = 1000;

if(args.Length == 0)
{
//If user did not enter any Parameter inform him
Console.WriteLine("Usage: Ping [-t] [-a] [-l size] [-n count] [-w timeout] <hostname> ") ;
Console.WriteLine("<hostname> The name of the Host who you want to ping");
Console.WriteLine("Options:");
Console.WriteLine(" -t Ping the specified host until stopped.") ;
Console.WriteLine(" -a Resolve addresses to hostnames.") ;
Console.WriteLine(" -n count Number of echo requests to send.") ;
Console.WriteLine(" -l size Send buffer size.") ;
Console.WriteLine(" -w timeout Timeout in milliseconds to wait for each reply.");
return;
}

for (int iCount=0; iCount < args.Length; iCount++)
{
string temp = args[iCount];
switch (temp.ToLower())
{
case "-t":
bLoop = true;
break;

case "-a":
bAddressResolve = true;
break;

case "-l":
iCount++;
try {
//Next arg s/b an int
iBytes = Int32.Parse(args[iCount]);
if (iBytes < 0 | iBytes > 65500)
{
Console.WriteLine("Bad value for option -l, valid range is from 0 to 65500.");
return;
}
}
catch {
Console.WriteLine("Bad value for option -l, valid range is from 0 to 65500.");
return;
}
break;

case "-n":
iCount++;
try {
//Next arg s/b an int
lngCount = UInt32.Parse(args[iCount]);
if (lngCount < 0 | lngCount > UInt32.Maxvalue)
{
Console.WriteLine("Bad value for option -n, valid range is from 0 to {0}.",UInt32.Maxvalue);
return;
}
}
catch {
Console.WriteLine("Bad value for option -n, valid range is from 0 to {0}.",UInt32.Maxvalue);
return;
}
break;

case "-w":
iCount++;
try
{
//Next arg s/b an int
lngTimeout = UInt32.Parse(args[iCount]);
if (lngTimeout < 1 | lngTimeout > UInt32.Maxvalue)
{
Console.WriteLine("Bad value for option -w, valid range is from 1 to {0}.",UInt32.Maxvalue);
return;
}
}
catch
{
Console.WriteLine("Bad value for option -w, valid range is from 1 to {0}.",UInt32.Maxvalue);
return;
}
break;
default:
strHostToPing = temp;
break;
}
}

//Check for continuous
if (bLoop) lngCount = 0;

//Do the ping
PingHost(strHostToPing, iBytes, lngCount, lngTimeout );
}

/// <summary>
/// This method takes the "hostname" of the server
/// and then it ping's it and shows the response time
/// </summary>
public static void PingHost(string strPingHost, int iPingBytes, uint lngPingCount, uint lngPingTimeout)
{
const int MAX_PACKET_SIZE = 65535;
//Declare the IPHostEntry
IPHostEntry PingTarget, PingSource;
int iBytesReceived = 0;
int dwStart = 0, dwStop = 0;
uint iLoop = 0;
bool bContinuous = false;

// Get the server endpoint
try
{
PingTarget = Dns.GetHostByName(strPingHost);
}
catch(Exception)
{
Console.WriteLine("Unkown host {0}" , strPingHost); // fail
return;
}
jackyhzzjcn 2003-01-22
  • 打赏
  • 举报
回复
在图形模式下执行嘛 :)

你不是写了服务控制程序了嘛 :)

Process myProcess = new Process();
myProcess.StartInfo.FileName = "yourservice.exe";
myProcess.StartInfo.Arguments = program;
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
tansm 2003-01-21
  • 打赏
  • 举报
回复
qiujoe(迷糊) 说得对,楼主你把基本问题都搞错了,服务是不应该有界面的,你应该使用另外一个exe控制这个服务,例如开始、停止等任务
colin666 2003-01-21
  • 打赏
  • 举报
回复
是呀
jackyhzzjcn 2003-01-21
  • 打赏
  • 举报
回复
服务的回传信息吗?
colin666 2003-01-21
  • 打赏
  • 举报
回复
如果我希望传信息到服务去呢?有没有什么好方法?
colin666 2003-01-21
  • 打赏
  • 举报
回复
这样做?有没有更好的解决方法?
colin666 2003-01-21
  • 打赏
  • 举报
回复
信息传出来是与托盘没有关系,但我想知道怎么传出来?用REMOTING 吗?有没有其他的办法?
jackyhzzjcn 2003-01-21
  • 打赏
  • 举报
回复
可以将程序的执行结果输出到TXT :)

然后你的程序(控制程序)读这个文本文件就可以了。

比如:

install >install.txt
uninstall >uninstall.txt

然后你的程序去读对应的txt文件就可以了 :)
sundy26 2003-01-21
  • 打赏
  • 举报
回复
http://www.pconline.com.cn/pcedu/empolder/gj/vc/10211/111759.html
sundy26 2003-01-21
  • 打赏
  • 举报
回复
http://www.dotnetcoders.com/web/Articles/ShowArticle.aspx?article=32
colin666 2003-01-21
  • 打赏
  • 举报
回复
我现在是想知道,我的服务和我的另外的应用程序之间通讯,是不是要用到REMOTING,或者是有更好的方法??
qiujoe 2003-01-21
  • 打赏
  • 举报
回复
1.服务是无界面的,你可以做一个管理程序,控制启动...操作。参照sql server就有一个跟你要求相似的管理程序
2.信息即时传出来与托盘好象没有关系
colin666 2003-01-20
  • 打赏
  • 举报
回复
可以给服务添加托盘吗?服务管理程序我不是太懂,能不能给介绍一下?
加载更多回复(8)

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧