Official Everybody Edits Forums

Do you think I could just leave this part blank and it'd be okay? We're just going to replace the whole thing with a header image anyway, right?

You are not logged in.

#1 2015-10-31 21:22:31

shadowda
Member
From: somewhere probably.
Joined: 2015-02-19
Posts: 1,015

[Question] multitheading with PlayerIOClient

During my attempts to optimize my bot, i came a cross a problem.

im trying to use mulitpe threads as below. (example, not directly next to each other in the full code.)

Thread x;

public void handlemsg(object sender, PlayerIOClient.Message m)
{

      If(what ever i want)
           {
                 x = new Thread(test);
                 x.Start();
           }
}

the problem is that within the new thread, i cant use things from PlayerIOClient.Message m .  I cant put the new thread "public void test()} inside "public void handlemsg(object sender, PlayerIOClient.Message m)" because that causes errors.

so for instance i cant use m.type and specifically ints form an m.type inside "public void register()"

how can i get around this. to multi thread but still being able to use m.types and its ints.


color = #1E1E1E       

latest?cb=20150604065609

Offline

#2 2015-10-31 21:38:24

Tomahawk
Forum Mod
From: UK
Joined: 2015-02-18
Posts: 2,847

Re: [Question] multitheading with PlayerIOClient

You could pass the entire PlayerIOClient.Message to your register() method as a parameter, and fiddle with it there.


One bot to rule them all, one bot to find them. One bot to bring them all... and with this cliché blind them.

Offline

#3 2015-10-31 22:22:41

shadowda
Member
From: somewhere probably.
Joined: 2015-02-19
Posts: 1,015

Re: [Question] multitheading with PlayerIOClient

um... can you explain that? please.


color = #1E1E1E       

latest?cb=20150604065609

Offline

#4 2015-10-31 23:26:57, last edited by Tomahawk (2015-10-31 23:33:22)

Tomahawk
Forum Mod
From: UK
Joined: 2015-02-18
Posts: 2,847

Re: [Question] multitheading with PlayerIOClient

shadowda wrote:

um... can you explain that? please.

I suggest you put down your bot for a few weeks and do most of this tutorial: http://www.homeandlearn.co.uk/csharp/csharp.html.

After that, you should be able to use your newly learned C# to actually code a bot by yourself.

Warning, rant inside:

Apologies, shadowda. Here's the spoonfeeding:

public void register(PlayerIOClient.Message m)
{
   - Use m as you would in the message event handler
}
 - And so:
public void handlemsg(object sender, PlayerIOClient.Message m)
{
   If (something)
   {
      new Thread(() => register(m)).Start();
   }
}

The syntax is strange, but it's the easiest and quickest way to run a method with arguments in a thread. I assume you're not doing anything with Thread x after it's started, so you don't need it at all.


One bot to rule them all, one bot to find them. One bot to bring them all... and with this cliché blind them.

Offline

Wooted by:

#5 2015-10-31 23:50:43

den3107
Member
From: Netherlands
Joined: 2015-04-24
Posts: 1,025

Re: [Question] multitheading with PlayerIOClient

A very ugly method to use (which I (sadly) use myself) is to make the Connection object public, and make your threads access the class containing the variable and then access the variable.

the reason why this is ugly is because it's VERY prone to crashing.

Imagine 2 threads doing stuff to a variable at the same time (which is likely to occur), what would happen?
No outcome is logical, no matter how you put it, meaning there's nothing left but to throw an exception, crashing your bot.


Long story short: First learn how to work with threads decently, something I didn't, resulting in terrible code and hundreds of bugs that were rather hard to fix (and are more hacks than fixes).

Offline

#6 2015-11-01 01:08:12, last edited by Tomahawk (2015-11-01 01:09:28)

Tomahawk
Forum Mod
From: UK
Joined: 2015-02-18
Posts: 2,847

Re: [Question] multitheading with PlayerIOClient

den3107 wrote:

Long story short: First learn how to work with threads decently, something I didn't, resulting in terrible code and hundreds of bugs that were rather hard to fix (and are more hacks than fixes).

Yeah, I found multithreading in a bot to be a terrible idea - not enough real benefit for the effort needed to get the thing to work. The only thing I use threads for now is to stop the UI from freezing, if for example a long bit of code is run in the form load event.

In the end with EE it's a little pointless because the message events are processed synchronously, though the timescales are too small anyway for "optimisation" to be worthwile.


One bot to rule them all, one bot to find them. One bot to bring them all... and with this cliché blind them.

Offline

#7 2015-11-01 02:02:50

shadowda
Member
From: somewhere probably.
Joined: 2015-02-19
Posts: 1,015

Re: [Question] multitheading with PlayerIOClient

Warning, rant inside:
agreement

color = #1E1E1E       

latest?cb=20150604065609

Offline

#8 2015-11-01 03:25:23

Hexagon
Member
Joined: 2015-04-22
Posts: 1,213

Re: [Question] multitheading with PlayerIOClient

What evidence do you have that you need to optimize? You could use a profiler to see what's going on, and whether another task is waiting on another to determine whether threads are a good option or not.

Offline

#9 2015-11-01 04:21:10

hummerz5
Member
From: wait I'm not a secret mod huh
Joined: 2015-08-10
Posts: 5,853

Re: [Question] multitheading with PlayerIOClient

I need to learn about this "profiler"... I've got a mess of spaghetti code giving me fits at times. I mean, it all works perfect. But I'd like to know how this "optimization" works just the same.

ANywho, I haven't met your crashing-bot-because-of-multithreading problem... what's that about again? It's always worked out for me.

Only problem I ever had was... if I don't terminate threads, closing the window doesn't terminate the process. lel

Offline

#10 2015-11-01 13:07:47

den3107
Member
From: Netherlands
Joined: 2015-04-24
Posts: 1,025

Re: [Question] multitheading with PlayerIOClient

Tomahawk wrote:
den3107 wrote:

Long story short: First learn how to work with threads decently, something I didn't, resulting in terrible code and hundreds of bugs that were rather hard to fix (and are more hacks than fixes).

Yeah, I found multithreading in a bot to be a terrible idea - not enough real benefit for the effort needed to get the thing to work. The only thing I use threads for now is to stop the UI from freezing, if for example a long bit of code is run in the form load event.

In the end with EE it's a little pointless because the message events are processed synchronously, though the timescales are too small anyway for "optimisation" to be worthwile.

.

Since all bots I wrote were gamemode bots, threads were kinda my only option. The bot is constantly in near-infinite loops and has to do stuff constantly, meaning it couldn't listen to stuff like 'b' (which is a crucial message to listen to).

Offline

#11 2015-11-01 13:12:09

madiik
Member
From: floor above Yuuta
Joined: 2015-02-26
Posts: 514

Re: [Question] multitheading with PlayerIOClient

i just use async threads.


shh i have returned

Offline

#12 2015-11-01 23:08:35

den3107
Member
From: Netherlands
Joined: 2015-04-24
Posts: 1,025

Re: [Question] multitheading with PlayerIOClient

Still, if you don't know how to properly work with threads and what you should and should not do with them your code can still get terribly ugly.

Offline

den31071446415715554713

Board footer

Powered by FluxBB

[ Started around 1732437842.6167 - Generated in 0.109 seconds, 13 queries executed - Memory usage: 1.56 MiB (Peak: 1.74 MiB) ]