Jump to content



Photo

Cancelling Out Of Multithreading Before It Finishes

mulitthreading threading multithreading

  • Please log in to reply
4 replies to this topic

#1 drewness

drewness

    Advanced Member

  • Members
  • PipPipPip
  • 136 posts
  • OS:Windows 10
  • Total Memory:More Than 9Gb
  • Framework:v4.5+, unsure
  • License:Developer Edition

Posted 14 September 2017 - 06:35 AM

Hi there,

 

I have multithreading working well in my bot, but I'm trying to enhance the functionality to allow for a user to cancel the process before the multithreading completes and having some issues.

 

This is some code from the multithreaded proxy checker part of the bot, as it's the most simple threading, and wanted to get it working first before trying to implement the same enhancements on the main bot process.

 

Approach #1: First i tried changing it from looping through the number of proxies to check to using a loop while #cancelProxyTesting = false, and added a button that sets #cancelProxyTesting to true when clicked. That stopped the proxy testing, but no matter how many threads it was supposed to run, it only ran one thread at a time and after experimenting quite a bit I have no idea why.

 

Approach #2 (Code below): I changed the loop total back to the proxy total, and set a check for #cancelProxyTesting inside the main work - if set to true (by user clicking the button) it would return blank, and the code was supposed to set the live thread count back to 0 and update the UI. Clicking the stop button does seem to stop the testing, but the live thread count stays above 0 and the UI never updates (the last 3 lines of code in the sample below). Also, sometimes when the stop button is clicked it starts throwing a bunch of errors over and over, the main one I remember is index out of range, which is odd because I never get that error when using the code without the stopTesting check in place.

 

Anyways, sample code is below, can anyone see how I might be able to effectively achieve cancelling multithreading gracefully when the user requests it? If you'd rather post clean different code that shows the same result, that would be amazing too, whatever is easiest.

 

Thanks in advance for your time.

 

define testProxies {
                plugin command("XAMLUI.dll", "xaml control state", "btnCancelProxyTest", "Visible", "True")
                set(#liveThreads,0,"Global")
                set(#proxyTestMode,$table cell(&proxiesToTest,0,1),"Global")
                remove from list(%proxiesToTest,0)
                plugin command("TableCommands.dll", "delete from table", &proxiesToTest, "Row", 0)
                clear list(%proxyTestCountList)
                set(#proxyTotal,$list total(%proxiesToTest),"Global")
                set(#proxyTestCountListCurrent,0,"Global")
                loop while($comparison(#proxyTestCountListCurrent,"< Less than",#proxyTotal)) {
                    add item to list(%proxyTestCountList,#proxyTestCountListCurrent,"Don\'t Delete","Global")
                    increment(#proxyTestCountListCurrent)
                }
                set(#cancelProxyTesting,$false,"Global")
                loop(#proxyTotal) {
                    loop while($comparison(#liveThreads,"=",#maxThreadCount)) {
                        wait(1)
                    }
                    increment(#liveThreads)
                    thread {
                        plugin command("LocalDictionary.dll", "init local dictionary")
                        plugin command("HTTP post.dll", "http auto redirect", "Yes")
                        plugin command("HTTP post.dll", "http max redirects", 10)
                        if(#cancelProxyTesting) {
                            then {
                                return("")
                                set(#liveThreads,0,"Global")
                            }
                            else {
                                checkProxies()
                            }
                        }
                        decrement(#liveThreads)
                        increment(#count)
                    }
                }
                loop while($comparison(#liveThreads,"!=",0)) {
                    wait(1)
                }
                plugin command("XAMLUI.dll", "xaml control state", "btnCancelProxyTest", "Visible", "False")
                set(#proxiesToTest,$nothing,"Global")
                plugin command("XAMLUI.dll", "xaml control value set", "txtNewProxies", $nothing)
            }
        }


Edited by drewness, 14 September 2017 - 06:36 AM.


#2 HelloInsomnia

HelloInsomnia

    Advanced Member

  • Moderators
  • 3139 posts
  • OS:Windows 10
  • Total Memory:More Than 9Gb
  • Framework:v4.5+, unsure
  • License:Developer Edition

Posted 14 September 2017 - 10:56 AM

I have a video on this exact topic you can watch it below, regarding your code I would take the part out of that custom command and put it in its own custom command and call that.

 

https://www.youtube....?v=5qYDmmFIJgQv



#3 drewness

drewness

    Advanced Member

  • Members
  • PipPipPip
  • 136 posts
  • OS:Windows 10
  • Total Memory:More Than 9Gb
  • Framework:v4.5+, unsure
  • License:Developer Edition

Posted 15 September 2017 - 06:05 AM

Fantastic video, exactly what I was needing, thank you so much!

 

Got it all recreated in my bot, now trying to add my proxy functions into the new threading framework.

 

Thank you again!



#4 Frank

Frank

    Advanced Member

  • Fellow UBotter
  • PipPipPip
  • 1097 posts
  • OS:Windows 8
  • Total Memory:8Gb
  • Framework:v3.5
  • License:Developer Edition

Posted 04 October 2017 - 09:24 AM

The big caution I offer is be careful when initiating threads. Knowing what I know in C# and threading, you can run into real issues if you create thousands of threads.

 

Frank


The sign of a good programmer is when they can create a simple solution to a complex problem;
not when they create a complex solution to a simple problem.

 


#5 drewness

drewness

    Advanced Member

  • Members
  • PipPipPip
  • 136 posts
  • OS:Windows 10
  • Total Memory:More Than 9Gb
  • Framework:v4.5+, unsure
  • License:Developer Edition

Posted 04 October 2017 - 06:32 PM

Hi Frank, thanks for the response. I'm pretty familiar with threading in C#, I have my bots limited to 50 threads right now and they're all running like champs, preciate the tip though, definitely good to keep in mind!

Sent from my SAMSUNG-SM-N920A using Tapatalk





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users