whileloop in winform cause app to crash ! why ?
Messages   Related Types
This message was discovered on microsoft.public.dotnet.languages.csharp.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

Tom
Hello friends,

for some reason when I execute this piece of code in a winform it
immediately crashes the app.

baseically I'm trying to manage a thread pool for a client/server based app.

while(true)

{

while (!client.Pending())

{

Thread.Sleep(1000);

}

ConnectionThread newconnection = new
ConnectionThread();

newconnection.threadListener = this.client;

ThreadPool.QueueUserWorkItem(new

WaitCallback(newconnection.HandleConnection));

}

after debugging it seems to crash as soon as it enters the while loop.

any crumbs of wisdom is appreciated

thankyou

Tom

Reply to this message...
 
    
Nicholas Paldino [.NET/C# MVP] (VIP)
Tom,

What is the exception that you are getting? Also, without knowing the
code that is called back into as a result of the thread pool thread being
called, it's hard to tell.

--
- Nicholas Paldino [.NET/C# MVP]
- Click here to reveal e-mail address

"Tom" <Click here to reveal e-mail address> wrote in message
news:4141a2d3$0$4046$Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Tom
I actually don't get any exceptions... it just crash completely
the problem is not the actual thread pool it just seems that when the
application enters the while(true) stage the entire application freezes.. no
exceptions no errors... everything just stops.

so I think the problem is more with the while(true) loop ? whats your
opinion ?

public class ThreadPoolTcpSrvr

{

private TcpListener client;

public ThreadPoolTcpSrvr(System.Windows.Forms.StatusBar
status)

{

IPAddress ip = IPAddress.Parse("211.30.133.94");

client = new TcpListener(ip, 9050);

client.Start();

status.Text = "Waiting for clients...";

while(true)

{

while (!client.Pending())

{

Thread.Sleep(1000);

}

ConnectionThread newconnection = new
ConnectionThread();

newconnection.threadListener = this.client;

ThreadPool.QueueUserWorkItem(new

WaitCallback(newconnection.HandleConnection));

}

}

}

Thanks nicholas
Tom

"Nicholas Paldino [.NET/C# MVP]" <Click here to reveal e-mail address> wrote in
message news:#o#Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Nicholas Paldino [.NET/C# MVP] (VIP)
Tom,

Well, yes, absolutely. Because you do this on the UI thread, what
happens is that the while loop just continues cycling forever, and windows
messages back up, and nothing gets processed.

--
- Nicholas Paldino [.NET/C# MVP]
- Click here to reveal e-mail address

"Tom" <Click here to reveal e-mail address> wrote in message
news:4141a84c$0$725$Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Tom
I don't understand there's a
while (!client.Pending())
{

Thread.Sleep(1000);

}

which makes sure that if there're no clients connection to sleep for 1
second... 1 second should enough for windows not to backup the messages ?

otherwise how can I resolve this ?

Thanks
Tom

"Nicholas Paldino [.NET/C# MVP]" <Click here to reveal e-mail address> wrote in
message news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Dennis Myrén
The while loop is an infinite loop.
while (true)
requires you to manually cancel the loop by using break keyword;

while (true)
{
Something();
if (IsFinished())
{
break;
}
}

The while (true) is somewhat popular.
Personally, i never use a such statement unless i really have to
because i dont think it is very clean.
Instead, i form a condition in the while statement.
while (! IsFinished())
{
Something();
}

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Tom" <Click here to reveal e-mail address> wrote in message
news:4141a2d3$0$4046$Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Tom
yes but I am doing socket programming

I need to wait on incoming connection thus the while loop

unless you have some other way of doing this ?

thanks
Tom

"Dennis Myrén" <Click here to reveal e-mail address> wrote in message
news:Luh0d.6098$Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
James Curran
"Tom" <Click here to reveal e-mail address> wrote in message
news:4141a8a4$0$4990$Click here to reveal e-mail address...
[Original message clipped]

That is handled by the loop:

while (!client.Pending())
{
Thread.Sleep(1000);
}

However I believe "client.AcceptSocket();" will handle that.

As far as I can see, after your receive an incoming connection, your
code queue a separate thread to read that connection. However, since this
thread need yields, that thread never starts, so next time through the loop,
client still has a connection pending, and so it creates a second thread to
read it, which also never starts, and so it then creates a third thread and
so on and so on.

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

Reply to this message...
 
    
Tom
Hi james I have a class ConnectionThread() which takes care of accepting the
client question..

but I think my question is no longer about threading its about what to do
with windows form when there's a while(true) loop ?

appreciate it
Tom

"James Curran" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
 
System.Net.IPAddress
System.Net.Sockets.TcpListener
System.Threading.Thread
System.Threading.ThreadPool
System.Threading.WaitCallback
System.Windows.Forms.StatusBar




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com