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

  • SEARCH
  • Home
  • 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 7822581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:57:30+00:00 2026-06-02T07:57:30+00:00

Right Now, it outputs 00, instead of 54. I’m learning with threads and I

  • 0

Right Now, it outputs 00, instead of 54. I’m learning with threads and I stuck here, I don’t know what to do now

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        Point[] array = new Point[20];
        public Form1()
        {
            for (int i = 0; i < 20; i++)
            {
                array[i] = new Point(); // I'm creating objects here
            }
            InitializeComponent();
        }

        void function1()
        {
            array[0].x = 5;
            array[0].y = 4;
        }

        void function2()
        {
            label1.Text = array[0].ToString();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Thread thread1 = new Thread(function1);
            thread1.Start();
            Thread.Sleep(500);
            function2();
        }
    }

    class Point
    {
        public int x;
        public int y;
        public override string ToString()
        {
            return x.ToString() + y.ToString();
        }
    }  
}

when Im doing it in that way (without another thread)

private void Form1_Load(object sender, EventArgs e)
{
    function1();
    function2();
}

It works fine, output is 54
thanks

  • 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-02T07:57:32+00:00Added an answer on June 2, 2026 at 7:57 am

    Actually your program is working on my computer as expected. So why it’s not working on your computer (or is working only sometimes)? This is because there is race condition in your program. This means that your program assumes that the second thread (that executes function1) will assign new values to array in less than 500ms. Never make any ASSUMPTION about how long it takes for the other thread to complete it’s task. Operating system is free to choose when, how often and how long individual threads executes and when it suspends them. Instead, use synchronization primitives (such as ManualResetEvent) to MAKE SURE that the second thread has done it’s task.

    This code shows, how you can change your program do thread synchronization properly:

        ManualResetEvent MRE = new ManualResetEvent(false);//Add this field to Form1
    
        void function1()
        {
            array[0].x = 5;
            array[0].y = 4;
            MRE.Set();//This notifies first thread, that values are set.
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            Thread thread1 = new Thread(function1);
            thread1.Start();
            MRE.WaitOne();//This makes this thread wait until the second thread calls MRE.Set()
            function2();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now my output is coming like that <p>text text text Continue reading →</p>
Right now I am writing a simulation program which output is formatted according to
Right now I am using std::pair to represent a 2d point in c++. However,
Right now I have this SQL query which is valid but always times out:
Right now I have def min(array,starting,ending) minimum = starting for i in starting+1 ..ending
Right now I have double numba = 5212.6312 String.Format({0:C}, Convert.ToInt32(numba) ) This will give
Right now I currently using transactional replication with updatable subscription. Is there any ways
Right now I convert the string containing the decimal number to an integer (ignoring
Right now I can use this URL to request a Google Static Maps image
Right now I've hard coded the whole xml file in my python script and

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.