Click here to Skip to main content
15,845,212 members
Home / Discussions / C#
   

C#

 
AnswerRe: Check 2 rows in different tables Pin
AFSEKI7-May-07 5:50
AFSEKI7-May-07 5:50 
QuestionBad Request! Pin
Sabry19053-May-07 3:47
Sabry19053-May-07 3:47 
AnswerRe: Bad Request! Pin
Guffa3-May-07 4:42
Guffa3-May-07 4:42 
GeneralRe: Bad Request! Pin
Sabry19053-May-07 5:32
Sabry19053-May-07 5:32 
AnswerRe: Bad Request! Pin
Stefan Prodan3-May-07 13:22
Stefan Prodan3-May-07 13:22 
GeneralRe: Bad Request! Pin
Sabry19056-May-07 2:24
Sabry19056-May-07 2:24 
Questionintegrate help V2.0 into winforms app Pin
Giorgi Dalakishvili3-May-07 3:36
mentorGiorgi Dalakishvili3-May-07 3:36 
QuestionWindows Services Pin
Sumanta Rout3-May-07 3:28
Sumanta Rout3-May-07 3:28 
Hi I have created a a service project in C#
It was installed perfectly. When the service started, It is giving error as "Could not perform requested operation for the Service "ServiceName" in local machine.

The Module is like this:

using System;
using System.IO;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Collections.Specialized;

namespace QuoteServer
{
///
/// Summary description for Class1.
///

public class QuoteServer
{
private TcpListener listener;
private int port;
private string filename;
private StringCollection quotes;
private Random random;
private Thread listenerThread;
public QuoteServer(): this ("quotes.txt")
{

}
public QuoteServer(string filename): this (filename,7890)
{

}
public QuoteServer(string filename, int port)
{
this.filename=filename;
this.port=port;
}
protected void ReadQuotes()
{
quotes=new StringCollection();
Stream stream=File.OpenRead(filename);
StreamReader streamReader=new StreamReader(stream);
string quote;
while((quote=streamReader.ReadLine())!=null)
{
quotes.Add(quote);
}
streamReader.Close();stream.Close();
random=new Random();
}
protected string GetRandomQuoteOfTheDay()
{
int index=random.Next(0,quotes.Count);
return quotes[index];
}
public void Start()
{
/*ReadQuotes();
listenerThread=new Thread(new ThreadStart(this.Listener));
listenerThread.Start();*/
}
protected void Listener()
{

try
{
//ReadQuotes();
IPAddress ipAddress=Dns.Resolve("localhost").AddressList[0];
listener=new TcpListener(ipAddress,port);
listener.Start();
while(true)
{
Socket socket=listener.AcceptSocket();//if the processor will take more time,
//we can create a new thread for each client will perform
string message=GetRandomQuoteOfTheDay();
UnicodeEncoding encoder=new UnicodeEncoding();
byte[] buffer=encoder.GetBytes(message);
socket.Send(buffer,buffer.Length,0);
socket.Close();
}
}
catch(SocketException e)
{
Console.WriteLine(e.Message);
}
}
public void Stop()
{
//listener.Stop();
}
public void Suspend()
{
listenerThread.Suspend();
}
public void Resume()
{
listenerThread.Resume();
}
public void RefreshQuotes()
{
ReadQuotes();
}



}
}
////////////////////////
The Service Module is like this:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;
//using QuoteServer;


namespace QuoteService
{
public class QuoteService : System.ServiceProcess.ServiceBase
{
///
/// Required designer variable.
///

private System.ComponentModel.Container components = null;
private QuoteServer.QuoteServer quoteServer;
private Thread t;

public QuoteService()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();

// TODO: Add any initialization after the InitComponent call
}

// The main entry point for the process
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;

// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = new System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new QuoteService() };

System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}

///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
//
// QuoteService
//
this.CanPauseAndContinue = true;
this.ServiceName = "QuoteService";

}

///
/// Clean up any resources being used.
///

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

///
/// Set things in motion so your service can do its work.
///

protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
/*t=new Thread(new ThreadStart(this.StartQuoteServer));
t.Start(); */

quoteServer=new QuoteServer.QuoteServer(@"C:\Documents and Settings\sumantar\Desktop\C#\C# Experiment\QuoteServer\quotes.txt",4567);
quoteServer.Start();
}

/*protected void StartQuoteServer()
{
quoteServer=new QuoteServer.QuoteServer(@"C:\Documents and Settings\sumantar\Desktop\C#\C# Experiment\QuoteServer\quotes.txt",4567);
quoteServer.Start();
}
*/
///
/// Stop this service.
///

protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
quoteServer.Stop();
}

protected override void OnPause()
{
// TODO: Add QuoteService.OnPause implementation
//base.OnPause ();
//quoteServer.Suspend();
}

protected override void OnContinue()
{
// TODO: Add QuoteService.OnContinue implementation
//base.OnContinue ();
//quoteServer.Resume();
}

protected override void OnShutdown()
{
// TODO: Add QuoteService.OnShutdown implementation
//base.OnShutdown ();
//OnStop();
}
public const int commandRefresh=128;
protected override void OnCustomCommand(int command)
{
// TODO: Add QuoteService.OnCustomCommand implementation
//base.OnCustomCommand (command);
switch(command)
{
case commandRefresh:
//quoteServer.RefreshQuotes();
break;
default:
break;
}
}
}
}
//////
The problem lies:

protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.

quoteServer=new QuoteServer.QuoteServer(@"C:\Documents and Settings\sumantar\Desktop\C#\C# Experiment\QuoteServer\quotes.txt",4567);
quoteServer.Start();
}

If I remove the 2 line:
The service is installed and starting and stoping.

If I put this 2 line,m Service is installed but not starting. It give the following error:

"Could not perform requested operation for the Service "ServiceName" in local machine.

Please help me. Thanks in advance.
AnswerRe: Windows Services Pin
kubben3-May-07 6:07
kubben3-May-07 6:07 
GeneralRe: Windows Services Pin
Sumanta Rout3-May-07 6:40
Sumanta Rout3-May-07 6:40 
GeneralRe: Windows Services Pin
kubben3-May-07 6:41
kubben3-May-07 6:41 
GeneralRe: Windows Services Pin
Sumanta Rout3-May-07 6:44
Sumanta Rout3-May-07 6:44 
GeneralRe: Windows Services Pin
kubben3-May-07 6:45
kubben3-May-07 6:45 
GeneralRe: Windows Services Pin
Sumanta Rout3-May-07 19:41
Sumanta Rout3-May-07 19:41 
GeneralRe: Windows Services Pin
kubben4-May-07 2:49
kubben4-May-07 2:49 
AnswerRe: Windows Services Pin
justintimberlake3-May-07 20:06
justintimberlake3-May-07 20:06 
QuestionUserControl like in TaskPane of MyComputer Pin
freshonlineMax3-May-07 3:21
freshonlineMax3-May-07 3:21 
AnswerRe: UserControl like in TaskPane of MyComputer Pin
freshonlineMax3-May-07 20:55
freshonlineMax3-May-07 20:55 
QuestionReminder Pin
soneliso3-May-07 3:21
soneliso3-May-07 3:21 
AnswerRe: Reminder Pin
Giorgi Dalakishvili3-May-07 3:32
mentorGiorgi Dalakishvili3-May-07 3:32 
AnswerRe: Reminder Pin
CPallini3-May-07 3:47
mveCPallini3-May-07 3:47 
GeneralRe: Reminder Pin
soneliso3-May-07 4:31
soneliso3-May-07 4:31 
AnswerRe: Reminder Pin
AFSEKI7-May-07 6:01
AFSEKI7-May-07 6:01 
Questionhowto get all netbios resources of specified IP in the LAN? Pin
n0vice3-May-07 3:07
n0vice3-May-07 3:07 
QuestionParse and download from xml file. [modified] Pin
mhm0013-May-07 2:28
mhm0013-May-07 2:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.