.NETGURU
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




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification