Click here to Skip to main content
15,889,808 members
Home / Discussions / C#
   

C#

 
GeneralRe: canonicalize a URL Pin
Blake Coverett24-Sep-03 13:39
Blake Coverett24-Sep-03 13:39 
Questionglobal debug variable? Pin
sharkfish24-Sep-03 5:03
sharkfish24-Sep-03 5:03 
AnswerRe: global debug variable? Pin
Jim Stewart24-Sep-03 5:34
Jim Stewart24-Sep-03 5:34 
AnswerRe: global debug variable? Pin
scadaguy24-Sep-03 8:42
scadaguy24-Sep-03 8:42 
AnswerRe: global debug variable? Pin
Alvaro Mendez24-Sep-03 8:52
Alvaro Mendez24-Sep-03 8:52 
GeneralRe: global debug variable? Pin
sharkfish24-Sep-03 9:11
sharkfish24-Sep-03 9:11 
QuestionHow to create TCP Listener windows service usingC# Pin
Baqer24-Sep-03 4:52
Baqer24-Sep-03 4:52 
AnswerRe: How to create TCP Listener windows service usingC# Pin
James Simpson25-Sep-03 2:19
James Simpson25-Sep-03 2:19 
You will need to create a thread to listen on, its a simple task,

Create a function in your service class which has the listneing socket and the loop in it, eg:

private void ListeningThread(void)<br />
{<br />
     TcpListener oListener = new TcpListener();<br />
  <br />
     _mnThreadCount ++;<br />
<br />
     oListener.Listen(nWhateverPortYouWant)<br />
     while(!_mbCloseThreads)<br />
     {<br />
       // .. do the work here<br />
     }<br />
<br />
     _mnThreadCount -- ;<br />
}


In OnStart, create your thread

public override OnStart()<br />
{<br />
    _mbCloseThreads = false;<br />
    _mnThreadCount = 0;<br />
<br />
    ThreadStart oThreadStart = new ThreadStart((void)this.ListeningThread);<br />
    Thread oThread = new Thread(oThreadStart);<br />
<br />
    oThread.Start();<br />
}


and in your OnStop you need to signal the thread to close (_mbCloseThreads) and wait for the threads to shut down (_mnThreadCount)

public override OnStop()<br />
{<br />
    _mbCloseThreads = true;<br />
    while(_mnThreadCount > 0)<br />
        Thread.Sleep(1);<br />
}


Things to remember:

If the thread crashed, always decrement the threadcount, put it in a finally block and that should be ok.
Use Thread.Sleep(1); at the beginning of each continuous loop so it fress up CPU time else your CPU will run at 100%

The code above might not be 100% correct as its strtaight out of my head, but I am sure you will get the general idea of it.

Thanks - James

James Simpson
Web Developer
imebgo@hotmail.com
GeneralRe: How to create TCP Listener windows service usingC# Pin
Baqer25-Sep-03 5:42
Baqer25-Sep-03 5:42 
GeneralRe: How to create TCP Listener windows service usingC# Pin
James Simpson25-Sep-03 22:36
James Simpson25-Sep-03 22:36 
GeneralRe: How to create TCP Listener windows service usingC# Pin
Baqer27-Sep-03 0:50
Baqer27-Sep-03 0:50 
GeneralRe: How to create TCP Listener windows service usingC# Pin
Baqer28-Sep-03 22:36
Baqer28-Sep-03 22:36 
GeneralRe: How to create TCP Listener windows service usingC# Pin
James Simpson29-Sep-03 6:24
James Simpson29-Sep-03 6:24 
Generaldisable scrollbars of MDI Parent Form Pin
peenu24-Sep-03 1:29
peenu24-Sep-03 1:29 
GeneralRe: disable scrollbars of MDI Parent Form Pin
Gary Kirkham9-Oct-03 10:53
Gary Kirkham9-Oct-03 10:53 
QuestionHow to use AppDomain?!? Pin
ich_bins24-Sep-03 0:58
ich_bins24-Sep-03 0:58 
AnswerRe: How to use AppDomain?!? Pin
Nick Parker24-Sep-03 2:29
protectorNick Parker24-Sep-03 2:29 
GeneralRe: How to use AppDomain?!? Pin
ich_bins24-Sep-03 23:54
ich_bins24-Sep-03 23:54 
GeneralUML Designer with enteprise version of VS Pin
Andrew Torrance24-Sep-03 0:30
Andrew Torrance24-Sep-03 0:30 
GeneralVisual Studio Design time Pin
Velichko Sarev23-Sep-03 23:42
Velichko Sarev23-Sep-03 23:42 
GeneralA very odd problem Pin
morefalt23-Sep-03 23:18
morefalt23-Sep-03 23:18 
GeneralRe: A very odd problem Pin
scadaguy24-Sep-03 2:49
scadaguy24-Sep-03 2:49 
GeneralRe: A very odd problem Pin
morefalt24-Sep-03 5:16
morefalt24-Sep-03 5:16 
GeneralRe: A very odd problem Pin
scadaguy24-Sep-03 5:56
scadaguy24-Sep-03 5:56 
Generalinstaller conditions: ((CHECKBOXA1 And CHECKBOXA2) And Not (CHECKBOXA3)) is not working Pin
Chris Richner23-Sep-03 22:51
Chris Richner23-Sep-03 22:51 

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.