K
Krupa
Hi All,
I am developing a multithreaded application in c# on vs 2005 (compact
framework 2.0). I want to have a socket server in a background thread
and want to continue with the windows form code in the main thread.
In my main windows form (called 'default'), I have instantiated a class
called 'Interop'. The 'interop' class has a function called
'instantiateSocketServer' that instantiates the SocketServer class. I
call the 'instantiateSocketServer' function on a background thread in
the 'default' form.
------------code in default form------------------------
default()
{
interop = new Interop();
Thread backgroundThread = new Thread(interop.initiateSocketServer);
backgroundThread.IsBackground = true;
backgroundThread.Start();
}
------------code in default form------------------------
------------code in Interop class------------------------
public void initiateSocketServer()
{
SocketServer socket = new SocketServer();
}
------------code in Interop class------------------------
------------code in SocketServer class------------------------
public SocketServer()
{
StartListening()
private StartListening()
{
PHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
listener.Bind(localEndPoint);
listener.Listen(1000);
Socket handler = listener.Accept();
}
}
------------code in SocketServer class------------------------
When I run my application, the StartListening() function gets executed
at the line:
Thread backgroundThread = new Thread(interop.initiateSocketServer);
When it comes to backgroundThread.Start();, it tries to execute the
StartListening() function again and I get a SocketException that says,
"only one usage of each socket address is normally permitted".
Can someone help?
Thanks,
Krupa
PS: Also, is there a way where I can pass an object instead of a method
to the backgroundThread?
I am developing a multithreaded application in c# on vs 2005 (compact
framework 2.0). I want to have a socket server in a background thread
and want to continue with the windows form code in the main thread.
In my main windows form (called 'default'), I have instantiated a class
called 'Interop'. The 'interop' class has a function called
'instantiateSocketServer' that instantiates the SocketServer class. I
call the 'instantiateSocketServer' function on a background thread in
the 'default' form.
------------code in default form------------------------
default()
{
interop = new Interop();
Thread backgroundThread = new Thread(interop.initiateSocketServer);
backgroundThread.IsBackground = true;
backgroundThread.Start();
}
------------code in default form------------------------
------------code in Interop class------------------------
public void initiateSocketServer()
{
SocketServer socket = new SocketServer();
}
------------code in Interop class------------------------
------------code in SocketServer class------------------------
public SocketServer()
{
StartListening()
private StartListening()
{
PHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
listener.Bind(localEndPoint);
listener.Listen(1000);
Socket handler = listener.Accept();
}
}
------------code in SocketServer class------------------------
When I run my application, the StartListening() function gets executed
at the line:
Thread backgroundThread = new Thread(interop.initiateSocketServer);
When it comes to backgroundThread.Start();, it tries to execute the
StartListening() function again and I get a SocketException that says,
"only one usage of each socket address is normally permitted".
Can someone help?
Thanks,
Krupa
PS: Also, is there a way where I can pass an object instead of a method
to the backgroundThread?