471,092 Members | 1,011 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,092 software developers and data experts.

threads, timers, passing arguments, time-slicing, C#

i'm trying to demonstrate timeslicing among equal high priority threads without putting threads to sleep. to do this i'm trying to run a timer while executing a very long loop sequence in order to get the processor to round-robin through the high priority threads. here's what i have:

static void Main(string[] args)
System.Timers.Timer aTimer = new System.Timers.Timer();

// Hook up the Elapsed event for the timer.
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent)

// Set the Interval to 5 seconds (5000 milliseconds).
aTimer.Interval = 5000;
aTimer.Enabled = true;

Console.WriteLine( "\n \nThis demonstrates time-slicing among high priority threads.\nThe processor alternates between the two high priority threads first before letting the low priority thread run.\n \n");
//class time slice object
imeSlice sliceTS = new TimeSlice();
//high priority thread #1
Thread threadHighSlice1 = new Thread(new ThreadStart(sliceTS.Print));
threadHighSlice1.Priority = ThreadPriority.Highest;
threadHighSlice1.Name = "1";
//high priority thread #2
Thread threadHighSlice2 = new Thread(new ThreadStart(sliceTS.Print));
threadHighSlice2.Priority = ThreadPriority.Highest;
threadHighSlice2.Name = "2";
//high priority thread #2
Thread threadHighSlice3 = new Thread(new ThreadStart(sliceTS.Print));
threadHighSlice3.Priority = ThreadPriority.Highest;
threadHighSlice3.Name = "3";
//low priority thread
Thread threadLowSlice = new Thread(new ThreadStart(sliceTS.Print));
threadLowSlice.Priority = ThreadPriority.Lowest;
threadLowSlice.Name = "Low";

// Keep the timer alive until the end of Main.


// Specify what you want to happen when the Elapsed event is
// raised.
private static void OnTimedEvent(object aTimer, ElapsedEventArgs e)
Thread current = Thread.CurrentThread;


class TimeSlice : EventArgs

public void Print()
Thread current = Thread.CurrentThread;
int number = 0;
int num = 0;
while(num < 100000)
num = num + 1;


i'm trying to get the timer elapsed event to print the thread currently being executed in a message box. but i can't seem to figure out how to pass the value. is there an easier way to accomplish what i'm trying to do?
Oct 6 '06 #1
0 6234

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by domeceo | last post: by
6 posts views Thread by sathyashrayan | last post: by
22 posts views Thread by Jeff Louie | last post: by
4 posts views Thread by gsimmons | last post: by
2 posts views Thread by Richard Cranium | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.