Deprecated: The behavior of unparenthesized expressions containing both '.' and '+'/'-' will change in PHP 8: '+'/'-' will take a higher precedence in /home/iano/public_html/tpforums-vb5/forum/includes/class_core.php on line 5842

PHP Warning: Use of undefined constant MYSQL_NUM - assumed 'MYSQL_NUM' (this will throw an Error in a future version of PHP) in ..../includes/init.php on line 165

PHP Warning: Use of undefined constant MYSQL_ASSOC - assumed 'MYSQL_ASSOC' (this will throw an Error in a future version of PHP) in ..../includes/init.php on line 165

PHP Warning: Use of undefined constant MYSQL_BOTH - assumed 'MYSQL_BOTH' (this will throw an Error in a future version of PHP) in ..../includes/init.php on line 165

PHP Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in ..../includes/functions_navigation.php on line 588

PHP Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in ..../includes/functions_navigation.php on line 612

PHP Warning: Use of undefined constant misc - assumed 'misc' (this will throw an Error in a future version of PHP) in ..../global.php(29) : eval()'d code(6) : eval()'d code on line 1

PHP Warning: Use of undefined constant index - assumed 'index' (this will throw an Error in a future version of PHP) in ..../global.php(29) : eval()'d code(6) : eval()'d code on line 1

PHP Warning: Use of undefined constant misc - assumed 'misc' (this will throw an Error in a future version of PHP) in ..../includes/class_bootstrap.php(1422) : eval()'d code(4) : eval()'d code on line 1

PHP Warning: Use of undefined constant index - assumed 'index' (this will throw an Error in a future version of PHP) in ..../includes/class_bootstrap.php(1422) : eval()'d code(4) : eval()'d code on line 1

PHP Warning: Use of undefined constant onlinestatusphrase - assumed 'onlinestatusphrase' (this will throw an Error in a future version of PHP) in ..../includes/class_core.php(4684) : eval()'d code on line 6

PHP Warning: Use of undefined constant onlinestatusphrase - assumed 'onlinestatusphrase' (this will throw an Error in a future version of PHP) in ..../includes/class_core.php(4684) : eval()'d code on line 6

PHP Warning: Use of undefined constant onlinestatusphrase - assumed 'onlinestatusphrase' (this will throw an Error in a future version of PHP) in ..../includes/class_core.php(4684) : eval()'d code on line 6

PHP Warning: Use of undefined constant onlinestatusphrase - assumed 'onlinestatusphrase' (this will throw an Error in a future version of PHP) in ..../includes/class_core.php(4684) : eval()'d code on line 6

PHP Warning: Use of undefined constant onlinestatusphrase - assumed 'onlinestatusphrase' (this will throw an Error in a future version of PHP) in ..../includes/class_core.php(4684) : eval()'d code on line 6

PHP Warning: Use of undefined constant onlinestatusphrase - assumed 'onlinestatusphrase' (this will throw an Error in a future version of PHP) in ..../includes/class_core.php(4684) : eval()'d code on line 6

PHP Warning: Use of undefined constant onlinestatusphrase - assumed 'onlinestatusphrase' (this will throw an Error in a future version of PHP) in ..../includes/class_core.php(4684) : eval()'d code on line 6
Doubt with BackgroundWorker
Results 1 to 7 of 7

Thread: Doubt with BackgroundWorker

  1. #1

    Doubt with BackgroundWorker

    I have the following scenario: two Backgroundworker, one for cavebot (Walker) and the other for targeting. I did so to have the option of working with the two functions independently. can enable only the cavebot, only targeting or both simultaneously.

    My question is: How do I pause or stop the execution of cavebot through a call from targeting?

    Or what would be the best method to have two different methods running simultaneously, but still depended on one another.

    Sorry for the google translator...

  2. #2
    Super Moderator
    Join Date
    May 2007
    Posts
    1,191

    RE: Doubt with BackgroundWorker

    How about running both on one thread?

  3. #3

    RE: Doubt with BackgroundWorker

    Running separately

  4. #4
    Super Moderator
    Join Date
    May 2007
    Posts
    1,191

    RE: Doubt with BackgroundWorker

    Quote Originally Posted by Tabgyn
    Running separately
    I know they do, and running the code one a single thread would solve your issue
    If you insist on using 2 BackgroundWorkers, post the code relevant to your BackgroundWorker objects

  5. #5

    RE: Doubt with BackgroundWorker

    You say the two BackgroundWorker run in one thread?

    My two BackgroundWorker:

    [code=csharp]
    public class Walker
    {
    private readonly Client _client = Program.MyClient;
    private readonly BackgroundWorker _walkerThread = new BackgroundWorker();
    private List<Waypoint> _wpList = new List<Waypoint>();

    public Walker()
    {
    _walkerThread.DoWork += WalkerExecute;
    _walkerThread.WorkerSupportsCancellation = true;
    }

    private void WalkerExecute(object sender, DoWorkEventArgs e)
    {
    _wpList = (List<Waypoint>)e.Argument;
    var _player = _client.GetPlayer();

    while (true)
    {
    Thread.Sleep(100);

    if (!_client.LoggedIn || e.Cancel) return;

    foreach (var _waypoint in _wpList)
    {
    _player.GoTo = _waypoint.Location;
    }

    if (!_walkerThread.CancellationPending) continue;
    e.Cancel = true;
    return;
    }
    }

    public bool IsRunning()
    {
    return _walkerThread.IsBusy;
    }

    public void Start(List<Waypoint> waypoints)
    {
    _walkerThread.RunWorkerAsync(waypoints);
    }

    public void Stop()
    {
    _walkerThread.CancelAsync();
    }
    }
    [/code]

    [code=csharp]
    public class Target
    {
    private readonly Client _client = Program.MyClient;
    public BackgroundWorker TargetThread = new BackgroundWorker();

    public Target()
    {
    TargetThread.DoWork += TargetExecute;
    TargetThread.WorkerSupportsCancellation = true;
    }

    private void TargetExecute(object sender, DoWorkEventArgs e)
    {
    var _targetList = (List<TargetObject>) e.Argument;
    var _player = _client.GetPlayer();
    while (true)
    {
    try
    {
    if(_player.Target_ID == 0)
    {
    var _creature = _client.BattleList.GetCreatures().FirstOrDefault(c => !c.IsSelf() && c.IsReachable() && c.DistanceTo(_player.Location) < 10);
    if (!(_creature == null))
    {
    foreach (var _t in _targetList)
    {
    if(_creature.Name.ToLower() == _t.Name.ToLower())
    {
    _player.Stop();
    _creature.Attack();
    }
    }
    }
    }

    Thread.Sleep(100);
    }
    catch (Exception)
    {
    return;
    }
    }
    }

    public bool IsRunning()
    {
    return TargetThread.IsBusy;
    }

    public void Start(List<TargetObject> targetList)
    {
    TargetThread.RunWorkerAsync(targetList);
    }

    public void Stop()
    {
    TargetThread.CancelAsync();
    }
    }
    [/code]

    I want him to stop walking if it's attacking.
    And walk again after that

  6. #6
    Super Moderator
    Join Date
    May 2007
    Posts
    1,191

    RE: Doubt with BackgroundWorker

    First off, you need to optimize your code.
    Your walker will walk to a different waypoint every 0.1 seconds, which it shouldn't.
    Your targetting is also horribly ineffective, it's much better to first get a list of creatures that match the names of which you want to attack, THEN find out if they are close enough and reachable.

    If you want to keep it simple and not having to dabble with cancelling threads left and right, put both targetting and walking in a single dedicated thread (or a BackgroundWorker, they're pretty much the same) and use some boolean variables to determine what the thread should do (i.e. player is currently killing something so don't walk, or player is not walking and no targets in sight, go to waypoint)

  7. #7

    RE: Doubt with BackgroundWorker

    The code snippets are just for testing so far.
    I will study your suggestion to place within the same thread.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •