473,386 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Thread Deadlock

Ok I'm currently experimenting with Sockets and Threads. I have a multithreaded server that opens a listening socket, and spawns a seperate worker thread to handle each connection made to it. To send data back to the Gui it uses a simple void sendmessage(string) delegate which calls the combination EndInvoke(Beginvoke(new sendmessage(thefunction), new object[]{thestring}))
if invokerequired is true.

what happens is, if multiple client connections are accepted and then the client closes out, it deadlocks and doesn't recover.. heres the thread code..

private void LiveThread(Object myso)
{

DateTime lastheard = DateTime.Now;
DateTime nextcheck = lastheard.AddSeconds(10);
Socket ConnSocket = (Socket)myso;
ConnSocket.Blocking=false;
IPEndPoint Rem = (IPEndPoint) ConnSocket.RemoteEndPoint;

SendMessage("Accepted Connection from : " + Rem.Address);



while ((ConnSocket.Connected) && (!stopitall))
{
if (ConnSocket.Available > 0)
{

try
{
byte[] buff = new byte[255];
int NumBytes = ConnSocket.Receive(buff);
lastheard = DateTime.Now;
nextcheck = lastheard.AddSeconds(10);
SendMessage("recvd:" + Encoding.ASCII.GetString(buff, 0, NumBytes));

}
catch (SocketException e)
{
}

}


if (nextcheck.CompareTo(DateTime.Now) <=0 )
{
try
{
ConnSocket.Send(Encoding.ASCII.GetBytes(":PING".To CharArray()));
SendMessage("Sending Ping");
lastheard = DateTime.Now;
nextcheck = lastheard.AddSeconds(10);

}
catch (SocketException)
{
}
}
}

SendMessage("Closing Connection..");
ConnSocket.Shutdown(SocketShutdown.Both);
ConnSocket.Disconnect(false);
ConnSocket.Close();


}


and the delegate and function..

delegate void amsg(string s);

public void SendMessage(string S)
{
if (InvokeRequired)
{
try
{
EndInvoke(BeginInvoke(new amsg(SendMessage), new Object[] { S }));
}
catch (ObjectDisposedException)
{
}
catch (InvalidOperationException)
{
}
}
else
{
textBox3.Text+="\r\n"+S;
textBox3.Select(textBox3.Text.Length, 0);
textBox3.ScrollToCaret();

}

I imagine it has something to do with the invoke.. but I'm not sure... please help.

Xen.
Jan 24 '08 #1
0 889

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: phil | last post by:
And sorry I got ticked, frustrating week >And I could help more, being fairly experienced with >threading issues and race conditions and such, but >as I tried to indicate in the first place,...
6
by: Robert Speck | last post by:
Hi there, Can anyone shed anymore light on why "Thread.Suspend()" has been deprecated by MSFT beyond what MSDN says about it. I'm not sure if I quite appreciate the various pitfalls they discuss...
5
by: PiotrKolodziej | last post by:
Hi. I have a form. This form calls thread. Inside this thread i have while loop: while ((bufferCount = FileStream.Read(buffer, 0, 64000)) 0 && !Stop) { store += bufferCount;...
22
by: Morpheus | last post by:
Hi, I have been coding in Windows for many years so have a mindset to it, so forgive any stupid questions. Is it possible to create a multithread application in C++ that is portable...
6
by: Zytan | last post by:
I have code running in the debugger as I type. I press pause, and it pauses on: Application.Run(new myForm()); *I believe* a worker thread is in deadlock (it's in a lock, but calls another...
2
by: Amit Dedhia | last post by:
Hi I am developing a scientific application which has moderate level image processing involved. In my application, there is a main application form which invokes another form. When this form...
20
by: cty0000 | last post by:
I have some question.. This is my first time to use thread.. Following code does not have error but two warring The warring is Warning 2 'System.Threading.Thread.Suspend()' is obsolete:...
2
by: Maqsood Ahmed | last post by:
Hello, I asked a question regarding thread synchronization on this usergroup some days back. I was using the AutoResetEvent class for signalling between two threads. The problem with...
34
by: Creativ | last post by:
Why does Thread class not support IDisposable? It's creating quite some problem. Namely, it can exhaust the resource and you have not control over it.
10
by: Paul E Collins | last post by:
I want to fill an ImageList with bitmaps for a ListView from another thread, because it's a time-consuming process. I expect the ListViewItems' images to "load" one by one, as in a Web browser. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.