Threading on FW1.1

G

Giulio Petrucci

Hello everybody,

I'm quite new to .NET FW programming, and I started just few months ago, on
FW2.0, knowing actually _nothing_ about FW1.1, so my question may sound
silly...

Anyway, what I need to know is: does Threading on FW1.1 behave exactly the
same than on FW2.0?

In particular my problem is this: I wrote a windows service and compiled it
on FW2.0 and now I had to performe a "backporting" to FW1.1, because of
some customers' request. My service is quite simple: it implements a server
listening for incoming connections on a specified port. In order to avoid
a timeout exception starting the server (that occours after 30 seconds, if
I'm right...) I've overriden the "OnStart()" method from the "ServiceBase"
class, and set inside it (I hope it's enough self-exlaining):

MyServer server = new MyServer();
protected void OnStart(String[] args) {

Thread t = new Thread(new ThreadStart(server.StartListening));
t.Start();
}

so that the OnStart() method returns in a little time.
The version compiled for the FW2.0 works wihtout any problem but with the
one compiled for FW1.1 I get a timeout and cannot start the service.
Can anyone help me?

Thanks in advance.
Kind regards,
Giulio - Italia
 
G

Guest

Giulio,
Set the t thread's IsBackground property to true.
You may need to post more code. In your StartListening method, How do you
track that server is supposed to keep running? Do you have a "while
(isRunning) " type of boolean that gets set to false in the StopListening
method?
Peter
 
W

Willy Denoyette [MVP]

message | Hello everybody,
|
| I'm quite new to .NET FW programming, and I started just few months ago,
on
| FW2.0, knowing actually _nothing_ about FW1.1, so my question may sound
| silly...
|
| Anyway, what I need to know is: does Threading on FW1.1 behave exactly the
| same than on FW2.0?
|
| In particular my problem is this: I wrote a windows service and compiled
it
| on FW2.0 and now I had to performe a "backporting" to FW1.1, because of
| some customers' request. My service is quite simple: it implements a
server
| listening for incoming connections on a specified port. In order to avoid
| a timeout exception starting the server (that occours after 30 seconds, if
| I'm right...) I've overriden the "OnStart()" method from the "ServiceBase"
| class, and set inside it (I hope it's enough self-exlaining):
|
| MyServer server = new MyServer();
| protected void OnStart(String[] args) {
|
| Thread t = new Thread(new ThreadStart(server.StartListening));
| t.Start();
| }
|
| so that the OnStart() method returns in a little time.
| The version compiled for the FW2.0 works wihtout any problem but with the
| one compiled for FW1.1 I get a timeout and cannot start the service.
| Can anyone help me?
|

What's the exact error message you get?
Are you sure the thread has started (succesfully), that is, that your
'server.StartListening' has been entered?
Did you try to run this in the debugger?

Willy.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi ,

I think that the code is correct.

Maybe you do not need a MyServer class, and just use a method of the
instance.

You shoudl not be getting any timeout at all

do you know fi thyou get any message in the Application log?
 
G

Giulio Petrucci

Hi Peter,
Hi Willy,
Hi Ignacio,
Hi everybody

First of all, thanks for your replies, I'll answer here, "all in one" :)

Peter Bromberg [C# MVP] ha scritto:
Set the t thread's IsBackground property to true.

Ok, I'll try...
You may need to post more code. In your StartListening method, How do you
track that server is supposed to keep running? Do you have a "while
(isRunning) " type of boolean that gets set to false in the StopListening
method?

class MyService : ServiceBase {

MyServer server;

MyService() {
//new instance of MyServer class;
server = new MyServer();
}

protected void OnStart(String[] args) {
Thread t = new Thread(new ThreadStart(server.StartLisrening));
t.Start();

}
}

class MyServer
{
private ManualResetEvent allDone = new ManualResetEvent(false);
public void StartListening() {
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
try {
listener.Bind(localEndPoint);
listener.Listen(100);
while(true) {
acceptDone.Reset();
listener.BeginAccept(new
AsyncCallback(AcceptCallback),listener);
//using dbgviewer and setting dozens of tracelines //I see that the
execution arrives 'till here...
**acceptDone.WaitOne();**
}
}
catch (Exception e) {
//...some code...
}
}
private void AcceptCallback(IAsyncResult ar) { ... }
}


Ok, that's the "core" code. This, compiled on the FW2.0 works greatly. I
set in the code lots of tracelines, writing on the DefaultTraceListener and
I could realize that the server starts "regulary" and the executions gets
the line marked as **...** here above. Now I'll try to set the thread
spawned in the OnStart service method as a background one and let you know.

Thanks again,
Giulio - Italia
 
G

Giulio Petrucci

Hi Peter,
Hi everybody,

Peter Bromberg [C# MVP] ha scritto:
Giulio,
Set the t thread's IsBackground property to true.

I've just tested it... nothing changed :-(

Thanks,
Giulio - Italia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top