whileloop in winform cause app to crash ! why ?

  • Thread starter Thread starter Tom
  • Start date Start date
T

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
 
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();
}
 
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.
 
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 said:
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]
- (e-mail address removed)

Tom said:
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
 
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
 
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]
- (e-mail address removed)

Tom said:
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



in
message news:#o#[email protected]...
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]
- (e-mail address removed)

Tom said:
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
 
Tom said:
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 ?

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.
 
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 said:
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]
- (e-mail address removed)

Tom said:
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



in
message news:#o#[email protected]...
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]
- (e-mail address removed)

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
 
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
 
Back
Top