473,810 Members | 3,102 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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.T imer aTimer = new System.Timers.T imer();

// Hook up the Elapsed event for the timer.
aTimer.Elapsed += new ElapsedEventHan dler(OnTimedEve nt)

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


Console.WriteLi ne( "\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 threadHighSlice 1 = new Thread(new ThreadStart(sli ceTS.Print));
threadHighSlice 1.Priority = ThreadPriority. Highest;
threadHighSlice 1.Name = "1";
threadHighSlice 1.Start();
//high priority thread #2
Thread threadHighSlice 2 = new Thread(new ThreadStart(sli ceTS.Print));
threadHighSlice 2.Priority = ThreadPriority. Highest;
threadHighSlice 2.Name = "2";
threadHighSlice 2.Start();
//high priority thread #2
Thread threadHighSlice 3 = new Thread(new ThreadStart(sli ceTS.Print));
threadHighSlice 3.Priority = ThreadPriority. Highest;
threadHighSlice 3.Name = "3";
threadHighSlice 3.Start();
//low priority thread
Thread threadLowSlice = new Thread(new ThreadStart(sli ceTS.Print));
threadLowSlice. Priority = ThreadPriority. Lowest;
threadLowSlice. Name = "Low";
threadLowSlice. Start();

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

}


// Specify what you want to happen when the Elapsed event is
// raised.
private static void OnTimedEvent(ob ject aTimer, ElapsedEventArg s e)
{
Thread current = Thread.CurrentT hread;
MessageBox.Show (current.Name);
}


}

class TimeSlice : EventArgs
{

public void Print()
{
Thread current = Thread.CurrentT hread;
int number = 0;
int num = 0;
while(num < 100000)
{
Console.Write(c urrent.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 6478

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

Similar topics

3
14962
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) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
6
2144
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 "string" in google comp.lang.c archives.Hope this helps.Hope I am not bothering any one. am I? =================================Start=========================== subject: Return to Start of Line? Question: I'd like printf, the next printf, to return...
22
4076
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
5976
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 ThreadPool thread. And that is exactly what MS VIsual Studio shows. But when I run Processexplorer or Taskmanager I see 2 additional threads, after a while another 2 additional threads. With the 3 threads at start time we have totally 7 threads.
1
1130
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 event handler, and each invocation sends a command to another service. What are the threading considerations given this scenario?
2
1351
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 event handler, and each invocation sends a command to another service. What are the threading considerations given this scenario?
6
4225
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 writing to a text file. The problem is that not every thread is able to log its message. I thought it was because each thread is trying to use the same resource. So i put in mutex.waitone() and released the mutex at the end of the method. It...
4
4022
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 rock solid/thread-safe. I've seen all the posts about using BeginInvoke to have worker threads interact with the UI. My question is this: I created a plain old Windows Form application (VS.NET 2005) with a blank form, built it (release build), ran...
3
1148
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 buffer, an event will suddenly be raised in the middle of my read, and that event will modify the buffer? And would using threads prevent this? Thanks, -- HA Note: My read actually should remove info from the buffer as it reads - so its modifying...
2
3410
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 "time out" and "handle" some arbitrary "event". The timeout value for select() is calculated off of a list of "active timers" which contains all the necessary information to correctly guess
0
9722
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10644
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10393
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10124
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.