Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8319299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:16:51+00:00 2026-06-08T22:16:51+00:00

This is my code: Thread _th1, _th2, _th3, _th4; int _i; void thMethod() {

  • 0

This is my code:

    Thread _th1, _th2, _th3, _th4;
int _i;

    void thMethod()
    {
        while(_i < 100){
            Thread.Sleep((new Random()).Next(1,500));
            Application.DoEvents();
            Console.WriteLine(_i);
            _i++;
        }
    }

    private void button4_Click(object sender, EventArgs e)
    {
        _th1 = new Thread(new ThreadStart(thMethod));
        _th1.Start();
        _th2 = new Thread(new ThreadStart(thMethod));
        _th2.Start();
        _th3 = new Thread(new ThreadStart(thMethod));
        _th3.Start();
        _th4 = new Thread(new ThreadStart(thMethod));
        _th4.Start();
    }

What i want to do is,
doing Console.WriteLine from 0 to 99 using multithread,
and the delay is random.

However, my code prints duplicated numbers

well, this is the result.

0
1
2
2
4
5
6
7
8
9
10
10
12
13
14
14
16
16
18
19
20
20
22
22
24
24
26
26
28
28
30
31
32
33
34
35
36
36
38
39
40
40
42
42
44
44
46
46
48
49
50
50
52
52
54
54
56
56
58
58
60
60
62
62
64
65
66
66
68
68
70
70
72
72
74
74
76
76
78
78
80
80
82
82
84
84
86
87
88
88
90
90
92
92
94
94
96
96
98
98
100
101
102

As you see, there are duplicated numbers printed,
and it does not even stop at 99.

So… How to correct this code in order to make it run properly?

====================== Do i have to change like..

    void thMethod()
    {
        while(_i < 100){
            Thread.Sleep((new Random()).Next(1,500));
            Application.DoEvents();
            Interlocked.Increment(ref _i);
            Console.WriteLine(_i);
        }
    }

this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:16:53+00:00Added an answer on June 8, 2026 at 10:16 pm

    Although you should be using Interlocked.Increment as mentioned by other posts. This isn’t the exact issue you’re having.

    The reason your numbers go past 100 is because the line you are incrementing the value comes after the Sleep. This causes threads to enter the loop, but by the time they write out the value the value has already changed to be above 100.

    For example t1, t2, and t3 enter the loop and wait for 500ms and i is 99. Then t4 exits and increments it to 100, now t1 will print 100, t2 will print 101, t3 will print 102.

    Interlocked.Increment wont actually solve the issue because the Thread.Sleep will still cause inconsistencies when the value is actually written out.

    To solve the problem you would need to use a thread lock
    declare a variable to lock on as you can’t lock on _i as its not a reference type:

    object obj = new object();
    
    void thMethod()
    {
        while (_i < 100)
        {
            lock (obj)
            {
                Console.WriteLine(_i);
                _i++;
            }
            Thread.Sleep((new Random()).Next(1, 500));
        }
    }
    

    This way you will get no duplicates and the value will stop at 99.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this code: Thread[] threadsArray = new Thread[4]; for (int i = 0;
Lets say we have this code: bool KeepGoing = true; DataInThread = new Thread(new
Given the following example code: new Thread(() => { for(int i = 0; i
I can't update my progressbar... this is my code Thread t=new Thread(new Runnable(){ public
I have this code: Thread t = new Thread(() => UpdateImage(origin)); t.Name = UpdateImageThread;
I have this in my code Thread.currentThread().sleep(x); Eclipse tells me to use the static
i have this code in C#: Thread t1 = new Thread(functionsActivations(3, 4000, 0, 4));
I have this code inside thread and when I call terminate it terminates fast
This code generates Low memory warning because background 5 thread is almostly loading images
If I run this code, will each AppDomain execute in a different thread? ThreadPool.QueueUserWorkItem(delegate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.