drewness 26 Posted September 14, 2017 Report Share Posted September 14, 2017 (edited) 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 September 14, 2017 by drewness Quote Link to post Share on other sites
HelloInsomnia 1103 Posted September 14, 2017 Report Share Posted September 14, 2017 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.com/watch?v=5qYDmmFIJgQv 2 Quote Link to post Share on other sites
drewness 26 Posted September 15, 2017 Author Report Share Posted September 15, 2017 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! 1 Quote Link to post Share on other sites
Frank 177 Posted October 4, 2017 Report Share Posted October 4, 2017 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 Quote Link to post Share on other sites
drewness 26 Posted October 5, 2017 Author Report Share Posted October 5, 2017 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 Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.