Sunday 15 August 2010

.net - C# Set Variable in Thread -



.net - C# Set Variable in Thread -

i'm trying work threads private project , have question is, think easy answer.

is possible set variable in thread?

here little code illustration show i'm trying do:

public class partyclass { public boolean partytime = true; public void makeparty() { while(partytime) console.writeline("i'm making party here"); console.writeline("the party ended. please leave now"); } public void stopparty() { partytime = false; } } public class mainthread { public static int main(string[] args) { partyclass party = new partyclass(); thread partythread = new thread(new threadstart(party.makeparty())); partythread.start(); while (!partythread.isalive) ; system.threading.thread.sleep(5000); // want somehow phone call stopparty() method } }

i don't know if it's stupid i'm trying think nice way stop "partythread" in clean way.

is possible or there improve solution this? ideas.

(i didn't test code - wrote out of head)

you phone call stop method way called start method:

party.stopparty();

in order ensure changes made in thread aren't cached, partytime field should marked volatile well.

c# .net multithreading network-programming threadpool

No comments:

Post a Comment