473,668 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# threads array

1 New Member
Hi,

I'm traying to make a thread array to a function with one variable, but i get everytime an error. I trayed to put the variable by the call, but the compiler doen't like that too.



Expand|Select|Wrap|Line Numbers
  1. static void Main(string[] args)
  2. {
  3. Thread[] threads = new Thread[5];
  4.  
  5. for(int i = 0; i < threads.Length; i++)
  6. {
  7. threads[i] = new Thread(new ThreadStart(DoWork));
  8. }
  9.  
  10. for(int i = 0; i < threads.Length; i++)
  11. {
  12. threads[i].Start();
  13. }
  14.  
  15. for(int i = 0; i < threads.Length; i++)
  16. {
  17. threads[i].Join();
  18. }
  19.  
  20. Console.WriteLine("Complete");
  21. }
  22.  
  23. public void DoWork(String myStr)
  24. {
  25. Console.WriteLine("DoWork"+myStr);
  26. }
Aug 24 '08 #1
1 2906
DonBytes
25 New Member
Expand|Select|Wrap|Line Numbers
  1. static void Main(string[] args)
  2.         {
  3.             Thread[] threads = new Thread[5];
  4.  
  5.             for (int i = 0; i < threads.Length; i++)
  6.             {
  7.                 threads[i] = new Thread(new ParameterizedThreadStart(DoWork));
  8.             }
  9.  
  10.             for (int i = 0; i < threads.Length; i++)
  11.             {
  12.                 threads[i].Start(i.ToString()); // or whatever you want to send to your thread
  13.             }
  14.  
  15.             for (int i = 0; i < threads.Length; i++)
  16.             {
  17.                 threads[i].Join();
  18.             }
  19.  
  20.             Console.WriteLine("Complete");
  21.             Console.ReadKey();
  22.         }
  23.  
  24.         public static void DoWork(Object StringObject)
  25.         {
  26.             String myStr = (StringObject as String);
  27.             Console.WriteLine(String.Format("{0} DoWork", myStr));
  28.         }
Aug 24 '08 #2

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

Similar topics

1
4103
by: Dennis Gavrilov | last post by:
Hi, All! I have two questions: strategic and technical. Technical one first: I need to share an array of objects (implemented as hashes, having references to other objects and hashes, sharing done after blessing) between all of the mod_perl2 threads. The structure can grow quite big - tenths of thousands of array elements. It can grow as system operates (not possible to construct at apache startup). Sharing array is OK, but inserting...
6
2742
by: Max Adams | last post by:
Threads and ThreadPool's If I use a ThreadPool how can I tell when a thead in the threadpool has exited? I don't want to set a global member variable I would much rather be able to act on an event Also (failing this ThreadPool issue) is it possible to create an array of Worker Threads, can anyone illustrate with code please Thanks
9
2374
by: Tpool | last post by:
hi, How to create the more than 25 threads in threadpool. i tried more than 25 threads in web request, but it shows an error. how can i have my threads to some 25 or more ,check the status of the threads, and assigning the task to completed threads in a loop. Thanks
3
22170
by: bygandhi | last post by:
Hi - I am writing a service which will check a process and its threads for their state ( alive or dead ). The process has 5 .net managed threads created using thread.start and each have been assigned a name. As in .net there is no way we can get the managed threads, I am thinking of making a win32 call and check their status.
11
1445
by: mareal | last post by:
I am trying to write a basic load balancer (in our web service) solution. The purpose of this load balancer is to keep an array updated with server status. We have several servers that can be accessed in order to retrieve information. From within the “Application_Start” (Global.asax), I create as many threads as servers we have available. The idea is that each thread will query each server and update a flag in the public array. When a...
10
1753
by: Darian | last post by:
Is there a way to find all the thread names that are running in a project? For example, if I have 5 threads T1, T2, T3, T4, T5...and T2, T4, and T5 are running...I want to be able to know that T2, T4 and T5 are already running. Thanks, Darian
7
3100
by: Michael | last post by:
I'm writing an application that decodes a file containing binary records. Each record is a particular event type. Each record is translated into ASCII and then written to a file. Each file contains the same events. At the moment each record is processed one after the other. It taks about 1m40s to process a large file containing 70,000 records. Would my application benifit from multiple threads and mmap? If so what is the best way to...
4
1578
by: chelfter | last post by:
I need to synchronize all the threads to add all the numbers. Threads are created, but not started immediately. When the threads do start, they must start simultaneously, without preferential treatment. That is to say, when the threads are finally allowed to start, Thread-1 should not always be the one to be scheduled on the CPU first. This should be addressed using threading constructs (mutex, semaphore, monitors, etc), and never ever...
1
1619
by: DR | last post by:
what are the memory caps for threads running as a CLR stored procedure executed by sql server 2005? is it limited by OS only or also by sql servers memory limits? e.g. lets say my clr stored procedure is executed by sql server 2005 then it creates 10 threads and each thread builds a giant array on the stack, is the limits of each array limited by sql server or the os? do these clr stored procedures run inside the sql server address space or...
5
1458
by: Antoninus Twink | last post by:
On 25 May 2008 at 20:15, Aggelidis Nikos wrote: There's a good reason for that. If a variable with automatic storage could be accessed by any thread, when would you suggest it gets destroyed? Create your array on the heap with malloc() instead, protect accesses to it using a mutex, and decide for yourself when the right time is to free() it.
0
8378
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8890
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
8653
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...
0
7398
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6206
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
5677
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4376
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2786
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2018
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.