Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

ive never multithreaded an app before.

i have a bot class. this bot is supposed to navigate its way through a corridor. the corridor is drawn onto a forum. all the bots are created on the forum, given various parameters, added to an arraylist, and their processing methods are called through a loop. im trying to time the duration of processing for each bot, but the last bot always has the accumulated time for all bots. if i multithread i imagine i could get rid of this. can anyone explain how i might go about this?

Posted

Create some sort of class that accepts a particular bot. All of the logic that operates on the bot could also be encapsulated in this class as well. Inside the class you will have a timestamp for when the class was instantiated. You will also have a public getter for retrieving the current time invested by that bot. After instantiating a instance of this class, you will start the bot in its own thread:

 

BotMonitor m = new BotMonitor(someBot);

Thread t = new Thread(new ThreadStart(m.StartBot));
t.Start();

// use later at some point
Console.WriteLine( m.TimeInvested );

Keep in mind that if you plan to access any shared resources, such as the bot itself, you will need to make sure the manner in which you do so is thread safe.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...