կոդտ տուր նայեմ կարողա մի բան դուրս գա
Կարամ այսքան մասը տալ:HTML կոդ:using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace AGamer.Gamer { public sealed class GenericGamer<TConnector,TGamer> where TConnector : IConnector where TGamer : Gamer { private TConnector connector_; private TGamer gamer_; private bool exit_; private AutoResetEvent waitForOutput = new AutoResetEvent(false); private AutoResetEvent waitForInput = new AutoResetEvent(false); public GenericGamer(TConnector connector, TGamer gamer) { connector_ = connector; gamer_ = gamer; } public void Run() { Thread inputThread = new Thread(new ThreadStart(InputThreadFunction)); Thread outputThread = new Thread(new ThreadStart(OutputThreadFunction)); Thread gamerThread = new Thread(new ThreadStart(GamerThreadFunction)); inputThread.Start(); outputThread.Start(); gamerThread.Start(); while(!gamer_.ExitState) Thread.Sleep(100); exit_ = true; Thread.Sleep(100);//give some time inputThread.Abort(); outputThread.Abort(); gamerThread.Abort(); } private void InputThreadFunction() { while (!exit_) { connector_.Input(Console.ReadLine());//The problem is here waitForInput.Set(); waitForOutput.Set(); } } private void OutputThreadFunction() { while (!exit_) { waitForOutput.WaitOne(); Console.Write(connector_.Output()); // end line will return connector_.Output } } private void GamerThreadFunction() { while (!exit_) { switch (gamer_.DoJob()) { case 0: exit_ = true; break; case 1: waitForOutput.Set(); break; case 2: waitForInput.WaitOne(); break; } } } } }
սենց դեպքի c++-ով եմ հանդիպել իչքան հիշում եմ abort-ի տեղը teminate գրեցի դզվեց
բայ վոր ամբողջ կոդը տաիր ավելի լավ կլիներ
Ավելացվել է 9 րոպե անց
while (!gamer_.ExitState)
ստեղ ExitState-ը ինչ փոփոխականա ?
երևի ստուգումա gamer_-ը վերջացնումա աշխատանքը?
Վերջին խմբագրող՝ AMD: 22.03.2008, 15:48: Պատճառ: Գրառման ավելացում
1.Վերջին հարցի պատասխանը այո է, ստուգում է թե երբ է gamer-ը վերջացնում իր գործը:
2.C#-ում տրեդը տերմինեյտ ֆունկցիա չունի:
3.Լրիվ կոդը տալը անհնար է և անիմաստ է այն պատճայով , որ շատ երկար է և դեր չի խաղում այս հարցում: Ինչ պետք է այստեղ կա: Եթե ուզում եք աշխատացնել ուղղակի ջնջեք ավելորդ մասերը և main-ից կանչեք Run մեթոդը:
Օրինակ
Երբ գրում ես quit, հիմնական տրեդը ցիկլից դուրս է գալիս, աբորտ է անում inputThread-ը և outputThread-ը և պրծնում է, սակայն inputThread-ը աբորտ չի լինում, այդ իսկ պատճառով ծրագիրը չի փակվում մինչև կոնսոլին մի բան enter չանես:HTML կոդ:using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Threading; namespace ZDraft { class Program { private string str; private bool exit_; private bool exitState_; private AutoResetEvent waitForOutput = new AutoResetEvent(false); private AutoResetEvent waitForInput = new AutoResetEvent(false); public void Run() { Thread inputThread = new Thread(new ThreadStart(InputThreadFunction)); Thread outputThread = new Thread(new ThreadStart(OutputThreadFunction)); inputThread.Start(); outputThread.Start(); while(!exitState_) Thread.Sleep(100); exit_ = true; Thread.Sleep(100);//give some time inputThread.Abort(); outputThread.Abort(); } private void InputThreadFunction() { while (!exit_) { str = Console.ReadLine(); if (str == "quit") exitState_ = true; waitForInput.Set(); waitForOutput.Set(); } } private void OutputThreadFunction() { while (!exit_) { waitForOutput.WaitOne(); Console.Write(str); } } static void Main(string[] args) { (new Program()).Run(); } } }
Փորձեք debuger-ով աշխատացնել և quit գրելուց հետո պաուզա տվեք և կտեսնեք, որ միակ ապրող տրեդը inputThread-ն է, որը կանքնած է str = Console.ReadLine(); տողում:
Վերջին խմբագրող՝ Pchuk: 22.03.2008, 18:57:
դզել եմ.իմ մոտ սենց աշխատումաԿոդ:using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Threading; namespace ZDraft { class Program { private string str; private bool exit_; private bool exitState_; private AutoResetEvent waitForOutput = new AutoResetEvent(false); private AutoResetEvent waitForexit = new AutoResetEvent(false); public void Run() { Thread inputThread = new Thread(new ThreadStart(InputThreadFunction)); Thread outputThread = new Thread(new ThreadStart(OutputThreadFunction)); inputThread.Start(); outputThread.Start(); waitForexit.WaitOne(); exit_ = true; Thread.Sleep(100);//give some time inputThread.Abort(); outputThread.Abort(); } private void InputThreadFunction() { while (!exit_) { str = Console.ReadLine(); if (str == "quit") // exitState_ = true; waitForexit.Set(); waitForOutput.Set(); } } private void OutputThreadFunction() { while (!exit_) { waitForOutput.WaitOne(); Console.Write(str); } } static void Main(string[] args) { (new Program()).Run(); } } }
պատճառը while (!exitState_)
Thread.Sleep(100);
exit_ = true;
ստեղ էր մինչևվ exit_ = true ես քայլը կհասցներ կատարել մի հատ ցիկլ էր գնում
ստեղ while (!exit_)
{
str = Console.ReadLine();
if (str == "quit")
exitState_ = true;
waitForInput.Set();
waitForOutput.Set();
}
Վերջին խմբագրող՝ AMD: 22.03.2008, 21:13:
Այո , այս ձևով կաշխատի,բայց
էլի իմ ուզածը չի:
Բացատրեմ
Այս կարճ օրինակը այդքան էլ հաջող ձևով չէի գրել: Ըստ այս գրածի միայն կոնսոլից կարելի է ծրագիրը փակել, իսկ ես ուզում եմ նաև որ մեկ ուրիշ տրեդից էլ հնարավոր լինի այն փակել: Օրիգինալում կա նաև GamerThread-ը, որը նույնպես կարող է ծրագիրը փակել կախված նրանից թե gamer_.DoJob()-ը ինչ կվերադարձնի: Ոնց ֆռցնենք մեկա այդ րեադլայնի վրա կանգնած տրեդին փակելու խնդրին ենք գալիս:
Երևի այսպես,
random-ի պատճառով մեկ quit հրամանով կփակվի, մեկ մեկ էլ GamerThread-ում կորոշվի , որ ծրագիրը պիտի փակվի և այդ ժամանակ էլ ի հայտ կգա պրոբլեմը:HTML կոդ:using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Threading; namespace ZDraft { class Program { private Random rand = new Random(); private string str; private bool exit_; private bool exitState_; private AutoResetEvent waitForOutput = new AutoResetEvent(false); private AutoResetEvent waitForInput = new AutoResetEvent(false); public void Run() { Thread inputThread = new Thread(new ThreadStart(InputThreadFunction)); Thread outputThread = new Thread(new ThreadStart(OutputThreadFunction)); Thread gamerThread = new Thread(new ThreadStart(GamerThreadFunction)); inputThread.Start(); outputThread.Start(); gamerThread.Start(); while(!exitState_) Thread.Sleep(100); exit_ = true; Thread.Sleep(100);//give some time inputThread.Abort(); outputThread.Abort(); gamerThread.Abort(); } private void InputThreadFunction() { while (!exit_) { str = Console.ReadLine(); if (str == "quit") exitState_ = true; waitForInput.Set(); waitForOutput.Set(); } } private void OutputThreadFunction() { while (!exit_) { waitForOutput.WaitOne(); Console.WriteLine(str); } } private void GamerThreadFunction() { while (!exit_) { switch (rand.Next(3))//instead of gamer_.DoJob() { case 0: exitState_ = true; break; case 1: waitForOutput.Set(); break; case 2: waitForInput.WaitOne(); break; } } } static void Main(string[] args) { (new Program()).Run(); } } }
ես մասը սենց գրի . պտի որ քո ուզածը լինիԿոդ:while (!exit_) { connector_.Input(Console.ReadLine());//The problem is here Thread.Sleep(100);//tog mi 100ms spasi vor hascni anjati waitForInput.Set(); waitForOutput.Set(); }
մի հատ փորձի
ես են փոքր օրինակի վրա արեցի օգնեց
Ա՞յս օրինակում:Կոդ:using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Threading; namespace ZDraft { class Program { private Random rand = new Random(); private string str; private bool exit_; private bool exitState_; private AutoResetEvent waitForOutput = new AutoResetEvent(false); private AutoResetEvent waitForInput = new AutoResetEvent(false); public void Run() { Thread inputThread = new Thread(new ThreadStart(InputThreadFunction)); Thread outputThread = new Thread(new ThreadStart(OutputThreadFunction)); Thread gamerThread = new Thread(new ThreadStart(GamerThreadFunction)); inputThread.Start(); outputThread.Start(); gamerThread.Start(); while(!exitState_) Thread.Sleep(100); exit_ = true; Thread.Sleep(100);//give some time inputThread.Abort(); outputThread.Abort(); gamerThread.Abort(); } private void InputThreadFunction() { while (!exit_) { str = Console.ReadLine(); if (str == "quit") exitState_ = true; Thread.Sleep(100); waitForInput.Set(); waitForOutput.Set(); } } private void OutputThreadFunction() { while (!exit_) { waitForOutput.WaitOne(); Console.WriteLine(str); } } private void GamerThreadFunction() { while (!exit_) { switch (rand.Next(3))//instead of gamer_.DoJob() { case 0: exitState_ = true; break; case 1: waitForOutput.Set(); break; case 2: waitForInput.WaitOne(); break; } } } static void Main(string[] args) { (new Program()).Run(); } } }
ստեղԿոդ:using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Threading; namespace ZDraft { class Program { private string str; private bool exit_; private bool exitState_; private AutoResetEvent waitForOutput = new AutoResetEvent(false); private AutoResetEvent waitForInput = new AutoResetEvent(false); public void Run() { Thread inputThread = new Thread(new ThreadStart(InputThreadFunction)); Thread outputThread = new Thread(new ThreadStart(OutputThreadFunction)); inputThread.Start(); outputThread.Start(); while (!exitState_) Thread.Sleep(100); exit_ = true; Thread.Sleep(100);//give some time inputThread.Abort(); outputThread.Abort(); } private void InputThreadFunction() { while (!exit_) { str = Console.ReadLine(); if (str == "quit") exitState_ = true; Thread.Sleep(100); waitForInput.Set(); waitForOutput.Set(); } } private void OutputThreadFunction() { while (!exit_) { waitForOutput.WaitOne(); Console.Write(str); } } static void Main(string[] args) { (new Program()).Run(); } } }
Այս պահին թեմայում են 1 հոգի. (0 անդամ և 1 հյուր)
Էջանիշներ