Author Topic: c# and multi-threading  (Read 4081 times)

0 Members and 1 Guest are viewing this topic.

Offline smittyTopic starter

  • Jr. Member
  • **
  • Posts: 28
  • Activity:
    0%
  • Reputation Power: 1
  • smitty has no influence.
  • Respect: +1
  • Referrals: 0
    • View Profile
c# and multi-threading
« on: December 07, 2017, 07:32:43 PM »
0
im back with another question!
So i have been messing around with multi-threading with a hunter script for stealth. I have an autoHealer thread, and a monsterAttacker thread. Im having problems with the two not working together. If my character starts getting low on health, he will stop attacking the monster(I pause the monster attacker thread), but when i get healed back to full health and resume the monster attacker thread, i start attacking myself ( I have died many times haha). suggestions? Im thinkin multi-threading is not the way to go  :\'(

Offline BobOzarius

  • Full Member
  • ***
  • Posts: 103
  • Activity:
    0%
  • Reputation Power: 2
  • BobOzarius has no influence.
  • Respect: +26
  • Referrals: 0
    • View Profile
Re: c# and multi-threading
« Reply #1 on: December 07, 2017, 07:35:32 PM »
0
haha Multi threading is always the way to go. What you need to do is show code. You're just doing something incorrectly upon returning to the attacking thread. Show your work. Or the teachers can't help.

Offline smittyTopic starter

  • Jr. Member
  • **
  • Posts: 28
  • Activity:
    0%
  • Reputation Power: 1
  • smitty has no influence.
  • Respect: +1
  • Referrals: 0
    • View Profile
Re: c# and multi-threading
« Reply #2 on: December 07, 2017, 08:09:33 PM »
0
okidoki
heres the main loop
Code: [Select]
            aHealer.Start();
            mTracker.Start();
            while(hunting)
            {
                //check weight
                if(player.Weight >= player.Maxweight - 10)
                {
                    var gold = Scanner.Find<Item>(typeof(items.gold), player.Backpack.Serial.Value, true);
                    if(gold.Count > 0)
                    {
                        Console.WriteLine("Sending gold...");
                        bos.useBag(gold[0]);
                    }
                }
                if(player.Hits < player.MaxHits)
                {
                    mTracker.Pause();
                    while(player.Hits < player.MaxHits)
                    {
                        //wait
                    }
                    mTracker.Resume();
                }
                if (myPet.Hits < (myPet.MaxHits - 5))
                {
                    mTracker.Pause();
                    while(myPet.Hits <  myPet.MaxHits)
                    {
                        //wait till pet is at fill health
                        player.Cast("heal");
                        player.Targeting.WaitForTarget(1500);
                        player.Targeting.TargetTo(myPet.Serial);

                    }
                    mTracker.Resume();
                }
                //check position
                //if(player.Location.X != startLocX && player.Location.Y != startLocY)
                //{
                //    player.Movement.MoveXY((ushort)startLocX, (ushort)startLocY, true, 1, true);
                //}
            } 

And heres the main part of my monster attacker thread
Code: [Select]
       private void attack(enemy item)
        {
            while(!_cancel && item.Hits > 0)
            {
                if (petKill && item.isAllKilled == false)
                {
                    if (Stealth.Client.ClientTargetResponsePresent() == true)
                        Stealth.Client.CancelTarget();
                    //uses pet
                    //Stealth.Client.SendTextToUO("All kill");
                    player.SendText("All kill", 0);
                    allKillRetical = true;
                    player.Targeting.WaitForTarget(1500);
                    player.Targeting.AutoTargetTo(item.Serial);
                    allKillRetical = false;
                    item.isAllKilled = true;
                }
                if (necroKill && item.isCorpsed == false)
                {
                    if (Stealth.Client.ClientTargetResponsePresent() == true)
                        Stealth.Client.CancelTarget();
                    player.Cast("corpse skin");
                    player.Targeting.WaitForTarget(1500);
                    player.Targeting.TargetTo(item.Serial);
                    item.isCorpsed = true;
                }
                if (mageryKill)
                {
                    if (Stealth.Client.ClientTargetResponsePresent() == true)
                        Stealth.Client.CancelTarget();
                    //uses eBolt
                    player.Cast("fireball");
                    player.Targeting.WaitForTarget(1500);
                    player.Targeting.TargetTo(item.Serial);
                }
            }
        }

and heres the main part of my heal thread

Code: [Select]
        public void HealOnce()
        {
           
            if (Stealth.Client.GetDeadStatus())
            {
                return;
            }
           
            //_ct = new CancellationTokenSource();
            if (player.Hits < player.MaxHits)
            {
                Console.WriteLine($"[Information] healonce");
                if (Stealth.Client.ClientTargetResponsePresent() == true)
                    Stealth.Client.CancelTarget();
            }
            else
            {
                //_sendConsoleMessage?.Invoke("No need in heals");
                return;
            }

            Stealth.Client.ClilocSpeech += Client_ClilocSpeech;

            //=================================================================================================================
            //if using magery
            if(useMageryHeal)
            {
                if(player.MaxMana > 5)
                {
                    player.Cast("heal");
                    player.Targeting.WaitForTarget(1500);
                    player.Targeting.TargetTo(targ.Serial);
                }
                else
                {
                    Console.WriteLine("[Error] Not enough mana!");
                    return;
                }

            }
            //==================================================================================================================
        }

hope this is enough!

Offline smittyTopic starter

  • Jr. Member
  • **
  • Posts: 28
  • Activity:
    0%
  • Reputation Power: 1
  • smitty has no influence.
  • Respect: +1
  • Referrals: 0
    • View Profile
Re: c# and multi-threading
« Reply #3 on: December 07, 2017, 09:29:10 PM »
0
Ooooo! look at me learnin things!!! I think i have solved the problem with a Mutex. One of the coolest words i have ever heard!

Offline Crome969

  • Moderator
  • *
  • *****
  • Posts: 2098
  • Activity:
    0%
  • Reputation Power: 25
  • Crome969 is on the verge of being accepted.Crome969 is on the verge of being accepted.Crome969 is on the verge of being accepted.Crome969 is on the verge of being accepted.Crome969 is on the verge of being accepted.
  • Gender: Male
  • UO Enthusiast
  • Respect: +211
  • Referrals: 10
    • View Profile
    • ScriptSDK
Re: c# and multi-threading
« Reply #4 on: December 08, 2017, 01:00:13 AM »
0

Tags: