473,320 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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";
threadHighSlice1.Start();
//high priority thread #2
Thread threadHighSlice2 = new Thread(new ThreadStart(sliceTS.Print));
threadHighSlice2.Priority = ThreadPriority.Highest;
threadHighSlice2.Name = "2";
threadHighSlice2.Start();
//high priority thread #2
Thread threadHighSlice3 = new Thread(new ThreadStart(sliceTS.Print));
threadHighSlice3.Priority = ThreadPriority.Highest;
threadHighSlice3.Name = "3";
threadHighSlice3.Start();
//low priority thread
Thread threadLowSlice = new Thread(new ThreadStart(sliceTS.Print));
threadLowSlice.Priority = ThreadPriority.Lowest;
threadLowSlice.Name = "Low";
threadLowSlice.Start();

// Keep the timer alive until the end of Main.
GC.KeepAlive(aTimer);

}


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


}

class TimeSlice : EventArgs
{

public void Print()
{
Thread current = Thread.CurrentThread;
int number = 0;
int num = 0;
while(num < 100000)
{
Console.Write(current.Name);
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 6407

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

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
6
by: sathyashrayan | last post by:
Following are the selected thread from the date:30-jan-2005 to 31-jan-2005. I did not use any name because of the subject is important. You can get the original thread by typing the subject...
22
by: Jeff Louie | last post by:
Well I wonder if my old brain can handle threading. Dose this code look reasonable. Regards, Jeff using System; using System.Diagnostics; using System.IO; using System.Threading;
3
by: mjheitland | last post by:
Hi, I like to know how many threads are used by a Threading.Timer object. When I create a Threading.Timer object calling a short running method every 5 seconds I expected to have one additional...
1
by: jeff | last post by:
Greetings; Newbie here, please forgive my ignorance of the vb.net threading model. I am developing a windows service which is driven by a variable number of timers. All timers use the same...
2
by: jeff | last post by:
Greetings; Newbie here, please forgive my ignorance of the vb.net threading model. I am developing a windows service which is driven by a variable number of timers. All timers invoke the same...
6
by: Dave | last post by:
I have a service that has 6 different threads. Each thread has a timer on it that elapses at about the same time (if not the same time). When the timer elapses I am trying to log a message by...
4
by: gsimmons | last post by:
I've been researching multi-threaded WinForms apps and thread synchronization stuff for a couple days since I'm working on refactoring a multi-threaded GUI app at work and want to be sure it's...
3
by: HONOREDANCESTOR | last post by:
I am modifying some code that uses 'events' to add to a buffer. Suppose I want to write a routine that will read from the buffer at timed intervals. Is there a danger that when I read from the...
2
by: Richard Cranium | last post by:
Hello, I have an interesting problem: I've got one thread in my program meant to maintain a timing subsystem and to do network I/O. It uses the select() system call to either wait for I/O or...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.