473,324 Members | 2,535 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,324 software developers and data experts.

Getting garbled data into thread

Hello Everybody,

I have this following code with me:
Expand|Select|Wrap|Line Numbers
  1. public class Program
  2. {
  3.    static int numThreads = 10;
  4.    static ManualResetEvent resetEvent = new ManualResetEvent(false);
  5.    static int toProcess = numThreads;
  6.  
  7.    static void Main(string[] args)
  8.    {
  9.        // Start workers.
  10.        for (int i = 0; i < numThreads; i++)
  11.        {
  12.           new Thread(() => MyFunction(i)).Start();
  13.        }
  14.  
  15.        // Wait for workers.
  16.        resetEvent.WaitOne();
  17.        Console.WriteLine("All threads finished.");
  18.        Console.ReadLine();
  19.    }
  20.  
  21.    public static void MyFunction(int i)
  22.    {
  23.       Console.WriteLine("Thread " + (i) + "  started......., sleeping for " + (i * 1000) + " seconds");
  24.  
  25.       Thread.Sleep(i * 1000);
  26.  
  27.       Console.WriteLine("Thread " + (i ) + " ended.......");
  28.  
  29.       // If we're the last thread, signal
  30.       if (Interlocked.Decrement(ref toProcess) == 0)
  31.          resetEvent.Set();
  32.    }
  33. }
  34.  

When I execute this code, instead of getting messages like this:

Thread 1 started......., sleeping for 1000 seconds
Thread 2 started......., sleeping for 2000 seconds
Thread 3 started......., sleeping for 3000 seconds
Thread 4 started......., sleeping for 4000 seconds
.
.
.
Thread 10 started......., sleeping for 10000 seconds
Thread 1 ended.......
Thread 2 ended.......
Thread 3 ended.......
Thread 4 ended.......
.
.
.
Thread 10 ended.......
All threads finished.


I get messages like:

Thread 2 started......., sleeping for 2000 seconds
Thread 3 started......., sleeping for 3000 seconds
Thread 3 started......., sleeping for 3000 seconds
Thread 4 started......., sleeping for 4000 seconds
Thread 9 started......., sleeping for 9000 seconds
Thread 9 started......., sleeping for 9000 seconds
Thread 9 started......., sleeping for 9000 seconds
Thread 9 started......., sleeping for 9000 seconds
Thread 10 started......., sleeping for 10000 seconds
Thread 10 started......., sleeping for 10000 seconds
Thread 2 ended.......
Thread 3 ended.......
Thread 3 ended.......
Thread 4 ended.......
Thread 9 ended.......
Thread 9 ended.......
Thread 9 ended.......
Thread 9 ended.......
Thread 10 ended.......
Thread 10 ended.......
All threads finished.


Could you please tell me where I am making mistake...

If the code is write, does this mean that when we create thread, data doesn't get passed correctly & it should be sent by some other mechanism....

your help is really appreciated........

Thanks in advance......

Zack
Jun 17 '13 #1
0 1162

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

Similar topics

1
by: steve | last post by:
Hi, My Windows machine running PHP Version 4.3.4, Apache, mysql, Zend IDE has developed a strange problem whereas the html code coming out of php sometimes gets garbled in unpredicatable ways,...
4
by: projecktzero | last post by:
Well, I've managed to get an image into a postgre database, but now I'm having trouble getting it out. #! /usr/bin/env python from pyPgSQL import PgSQL def main(): connectdb =...
2
by: N. Graves | last post by:
I'm planning to develop a process to import data from an external flat file to a new table automatically. As usual I'm having a hard time getting started on the solution because of the Access's...
1
by: Antonio. kim | last post by:
Hello~ ^^ I have a question about getting excel data with oledbconnection. I entered '23190000' to Excel Cell and changed Column width to small, then the value chaged to 2E+07 or ####. And I...
14
by: John J. Hughes II | last post by:
Using the below code I am send multiple sterilized object across an IP port. This works fine if only one object is received at a time but with packing sometimes there is more then one object or...
2
by: bil.shah | last post by:
Hi, I am listening to a port for data but I am not able to recieve whole data, I only get truncated data. Client sends me data that exceeds 40K and the data I recieve in my callback function is...
36
by: Chuck Faranda | last post by:
I'm trying to debug my first C program (firmware for PIC MCU). The problem is getting serial data back from my device. My get commands have to be sent twice for the PIC to respond properly with...
3
by: ist | last post by:
Hi, I am trying to get (and transfer over ASP.NET) some encrypted data from some MySQL fields. Since the data contains many unicode characters, I tried to get the data as a series of ASCII...
1
by: =?Utf-8?B?dmJTdHVkZW50?= | last post by:
Dear All, I have textbox in my form .. I enter the student name in the text e.x (vbstudent) I want to sign textbox data to array, each letter in one index. ex array(0)=v array(1)=b
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
1
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: 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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.