The online racing simulator
Delay in C Sharp insim
(8 posts, started )
#1 - PoVo
Delay in C Sharp insim
Hello, i am making an insim for a drift server, in C Sharp, i want to make a comand that will count (in message output 3 2 1 Go)


I want to make a delay from 3 to 2 to 1 to Go. But dunno how to do it, when i do it normaly it just send all the numbers at the same time
Thanks!
#2 - PoVo
Thanks! Very big thanks
While Sleep() works, it's generally not a good idea to sleep the whole program thread, it's better to use the System.Timers.Timer class instead. Here's an example.

using System;
using System.Timers;

class Program
{
static int count = 0;
static string[] countdown = new string[] { "3", "2", "1", "GO!"};

static void Main()
{
Timer timer = new Timer();
timer.Interval = 1000; // Milliseconds.
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.Start();

// Stop program from exiting.
Console.ReadLine();
}

static void timer_Elapsed(object sender, ElapsedEventArgs e)
{
if (count < countdown.Length)
Console.WriteLine(countdown[count++]);
else
((Timer)sender).Stop();
}
}

Quote from morpha :fixed it for you :lovies:

Lol!

Seems some people are in love with that name. Can't blame them ... DarkTimes ... Nice job on the code and the name .
#7 - PoVo
Wow, i love this thread, its still alive.
I'm bored so I rewrote my old example using Spark. You type !cd or !countdown and it shows a countdown in the middle of the screen.

using System.Timers;
using Spark;
using Spark.Packets;

namespace CountDown
{
class Program
{
InSim _insim;
Timer _timer;
int _count;
string[] _countdown = new string[] { "3", 2", "1", "GO!" };

public Program()
{
using (_insim = new InSim())
{
// Connect to InSim
_insim.Bind<IS_MSO>(MessageReceived);
_insim.Connect("127.0.0.1", 29999);
_insim.Send(new IS_ISI { IName = "^3CountDown", Prefix = '!' }); // Set prefix to '!'

// Initialize countdown timer.
_timer = new Timer();
_timer.AutoReset = true;
_timer.Interval = 1000; // Milliseconds
_timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);

// Go!
_insim.Run();
}
}

void MessageReceived(IS_MSO mso)
{
if (mso.UserType == UserType.MSO_PREFIX)
{
var message = mso.Msg.Substring(mso.TextStart);
if (message.StartsWith("!cd") || message.StartsWith("!countdown"))
{
// Start countdown!
_timer.Start();
}
}
}

void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
if (_count < _countdown.Length)
{
_insim.Send("/rcm {0}", _countdown[_count++]);
_insim.Send("/rcm_all");
}
else
{
_count = 0;
_timer.Stop();
_insim.Send("/rcc_all");
}
}

static void Main()
{
new Program();
}
}
}


Delay in C Sharp insim
(8 posts, started )
FGED GREDG RDFGDR GSFDG