Jump to content
  • Advertisement
Sign in to follow this  
Winegums

C# Multithreading Loop Problem

This topic is 4075 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm trying to write a basic multi threaded application in C#. I've written one in C++ before. My question/issue stems from having threads loop indefinatley. I want to keep my threads generating numbers from a number and setting a variable to this number. in C++ i just had each threads function have some code that said while(!dead) { //do thread stuff } but doing the same in c# seems to cause the program to not do anything, even if it's the last bit of code called. is there a way to loop threads properly in C#?

Share this post


Link to post
Share on other sites
Advertisement
using System.Threading;
// ...
Thread t = new Thread
delegate()
{
while (Thread.CurrentThread.IsAlive)
{
// Do thread stuff...
}
}
t.Start();
// ...


Seems to work as expected. What were you doing instead?

Unrelated to your actual problem: I assume you've used proper synchronization techniques for the threads where appropriate (particularly in the 'setting a variable to this number part' and possible the 'generating numbers from a number' part)?

Share this post


Link to post
Share on other sites
when I do that it doesn't work, i don't get so much as a window (can only asume that the infinatley looping threads are choking the program?). annoyingly it works fine on other example code, but in my code even if nothings in the loop it freezes up.

heres the thread constructor and 'doing things' functions


public theThread(string name, int size)
{
this.mName = name;
this.mSize = size;
this.mId = GetId();
this.mPages = GetPages(mSize);
//Begin getting data
lockObject = new Object();
this.mThread = new Thread(new ThreadStart(GetData));
this.mThread.Start();
this.mThread.Join();
this.mRequests = this.mFaults = 0;
this.mState = 0;
}

public void GetData()
{
while (this.mThread.IsAlive)
{

}
}




the threads are in a list, could that be causing problems somehow?

Share this post


Link to post
Share on other sites
I might be wrong, but I think the key is that Join doesn't behave as you expect. More code might be useful.

Share this post


Link to post
Share on other sites
I'm normally somewhat wary of disagreeing with others - but I don't know if Join() is the problem (in that the code provided seems to set up a thread, wait for it to terminate, then do some thread-local cleanup).

However, more code is definitely needed. If the window doesn't appear, then that's interesting: which thread is the window being created on (the creator thread or the created thread); and where is the window creation, temporally relative to the creation of the new thread?

I think more code, and a little more description as to the structure of the system (and even, at the risk of being obtuse, why it needs to be multithreaded?).

Share this post


Link to post
Share on other sites
Indeed. Might not be a problem. The key point is 'wait for it to terminate'. If the thread doesn't terminate (infinitely looping or waiting for some trigger which never comes) then the thread calling the join (the main/window thread?) won't ever continue... which is the problem behavior described.

Share this post


Link to post
Share on other sites
Quote:
Original post by Telastyn
Indeed. Might not be a problem. The key point is 'wait for it to terminate'. If the thread doesn't terminate (infinitely looping or waiting for some trigger which never comes) then the thread calling the join (the main/window thread?) won't ever continue... which is the problem behavior described.


... and that just goes to reinforce my previous rule of not disagreeing with those with apparently more experience than me. I didn't think about the possibility of the spawned thread not terminating. rate++

Share this post


Link to post
Share on other sites
the code is multithreaded purley because i'm learning to do multithreading :)

the threads are created after the window and some of the labels are, though it's all done in the main app constructor.

even without using Join() i get a freezeup problem, infact i only put in join to fix the synchronisation problems i was having.

so how do you have indefinatley looping threads and still have the ability for the app to run?

Share this post


Link to post
Share on other sites
Quote:
Original post by Winegums
so how do you have indefinatley looping threads and still have the ability for the app to run?


Without more code and/or a description of the setup it'll be tough to guess at your case.

Personally, I've done the

while( !dead ){
// stuff
Thread.Sleep(0);
// edit: yes, Sleep(0). 'tis what I get for trying
// to recant code from memory before 8am...
}


thread loop in C# without any problem. Never on a single proc machine though, and I imagine the Sleep might be a bit more important on that sort of rig...

[Edited by - Telastyn on April 23, 2007 9:39:06 AM]

Share this post


Link to post
Share on other sites
Blocking the thread by which a windows was created freezes the window as it doesn't get any time to update it. Calling methods and/or setting properties of the windows from within an other thread than the creator freezes the window also.

BTW: Perhaps Sleep(0) might be a better option than Sleep(1). See the docs for an explanation. Sleep(1) might lead to race conditions.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!