c# - How to asynchronously run 3 processes, when one returns desired value stop the other two and continue with program? -


I have a method that uses the afgh.net.net framework templatematch (similarity to image against another image Check different images against any area of ​​the screen This task can take a long time or can be instantly based on the number of images, the size of the images and the field check.

Only 1 image in the list of images will ever return a match so I would like to test all the images against the screen at the same time and cancel the remaining processes immediately after 1 point of that point is correct Is given and my program runs on its next step.

Now, for example, I'm getting an integer value that depends on the match's right but the concept is always the same. The number of images tested against screenshots. 1 will come true, will not be left Sometimes the returns are correct first and for the second time it corresponds to the template for 30 and 30 images in the list and takes a lot of time compared to 1.

A warning to note about my code which is as follows .. I will not always return an integer, I usually return a boolean value as the image was found, but this code is here The easiest and common principle applies (i.e. if i read the following as my (synchronous) code as follows ... how can i do this asynchronous call what i can do, What is described If possible, please expand your answer as if I want to know that in future I can easily do this type of thing. I understand the concept of async but for some reason my head is absolutely fine The way I want to.

  Public Zero fight () {var myGuysTurn = WhosTurn (); // Other logic here.} Private Ent WhosTurn () {var whosTurn = 0; Var whosTurnCheck = _templateMatch.Match (_tabula.BattleHeroTurn1 ()); if (whosTurnCheck) {whosTurn = 1; return whosTurn;} wh OsTurnCheck = _templateMatch.Match (_tabula.BattleHeroTurn2 ()); if (whosTurnCheck) {whosTurn = 2; WhosTurn return; WhosTurnCheck = _templateMatch.Match (_tabula.BattleHeroTurn3 ()); If (whosTurnCheck) {whosTurn = 3; WhosTurn return; } Return whosTurn; }    

I was using the task. WaitAny () is combined with a cancellation token . Basically, start each task in parallel and no one is complete. If the whole work was successful, cancel others if not, keep waiting for other tasks to continue.

I have changed the _templateMatch.Match (_tabula.BattleHeroTurnX ()) with a static method for summarizing with BattleHeroTurnX :

  Personalized WhosTurn () {// Create a cancellation token. Other threads will be used to inform that they should cancel cancellation immediately Cancel token resource cts = new cancellationTokenSource (); // Collection of works in parallel lists & lt; Work & lt; Int & gt; & Gt; Work = new list & work; Task & lt; Int & gt; & Gt; () {Tasks. Run & lieutenant; Int & gt; (() => {return BattleHortorn 1 (CTS. token) 1: 0;}), action.run < Int & gt; () = & Gt; {Return Bethelhorne Turn 2 (CTS.Token) 2: 0;}), Tasks. Run and lieutenant; Integrated; ((=) = & Gt; {BattleHarrowTiturn3 (CTS.Token) 3: 0;})}; // Wait for any work to be done and if it succeeds, then cancel other tasks and return (work.) (Any)) // // Get the complete completion of the task TaskIndex = Task.WaitAny (tasks.ToArray)); Intel turn = work [fulltask index]. result; If (turn> 0) {cts.Cancel (); Return return; } Taskss.RemoveAt (fullslash index); } // has completed all the tasks, but no Bethelhortienne did not return the correct return; } Static Bull BattleHarator1 (Cancellation Tokens Token) {// Processing images are processed after each, make sure token (Int i = 0; i & lt; i ++) {thread. Sleep (50) has not been canceled; If (token. ICC cancellation request) {return false; }} Be true; } Static Bull BattleHoratronic 2 (Cancellation token token) {// Processing images are processed after each, make sure token (int i = 0; i <10; i ++) {thread. Sleep (70); If (token. ICC cancellation request) {return false; }} Be true; } Static Bull BattleHarator 3 (Cancellation Token Tokens) {// Processing images are processed after each, make sure token (int i = 0; i   

See more for more information.

Comments

Post a Comment

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -