Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

[.net] Code needs to be rewritten

Started by Hermes 2 replies 1.2k views
Original Post
Hermes
Hermes
Hi! I have the following code that needs to be rewritten. I need to use another way of waithing the next thread to finish not using wile loops....I want something like WaitForSingleObject ....is there a way to do it in C# without dllImport...?? THX a lot


				Thread ExecThread = new Thread(new ThreadStart(SendDataToRemoteServer));
				ExecThread.Start();
				int TimeToWait=0;
				int IncomingTimeOut=120000;
				
				
				while ((ExecThread.IsAlive)&&(TimeToWait<IncomingTimeOut))
				{
					Thread.Sleep(100);
					TimeToWait=TimeToWait+100;
				}
				if(ExecThread.IsAlive)
				{
					ExecThread.Abort();
					ExecThread.Join();
					string LogMsg="";
					LogMsg="Thread Aborted ";
					if(Subject!=null)
					{
						LogMsg=LogMsg+"Subject     :";
						LogMsg=LogMsg+Subject;
					}
					LogClass.WriteToLog(InfoLevel1,LogMsg);
		
				}



DrGUI
DrGUI
Hi

I've never had to do this myself, but I think you do something with Monitor.Begin/Pulse/Wait/Exit, so you could look into those.

I saw that in the the article in the SDK help on asynchronous IO so you might want to look at that; it waits for all the threads to finish before exiting.


EDIT: and to be picky about your code, it would be more readable to use x += y rather than x = x + y. I think it's best practise for local variables to be in camelCase, to me at least it looks like you're setting a property rather than a local variable :p The framework SDK has pages on coding standards :P


Hope that helps,
Andrew
RipTorn
RipTorn
Take a look at the ManualResetEvent class. it should be able to do what you want.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.