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 8524885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:48:51+00:00 2026-06-11T07:48:51+00:00

I am trying to understand threading concepts in .Net. I am unable to use

  • 0

I am trying to understand threading concepts in .Net.
I am unable to use Yield() method. I want the control to go to a parallel thread when i becomes divisible by 10.

Please help.

Below is my sample code:

class ThreadTest
{
    //Index i is declared as static so that both the threads have only one copy
    static int i;


    static void Main(string[] args)
    {
        Thread t = new Thread(WriteY);          
        i = 0;

        //Start thread Y    
        t.Start();                              
        //Do something on the main thread.
        for (; i < 100; i++)
        {
            if (i % 10 == 0)
            {
                //Simulate Yield() function
                Thread.Sleep(0);
                Console.WriteLine("The X thread");
            }
            Console.Write(i + ":X ");
        }
        Console.ReadKey(true);
    }

    static void WriteY()
    {
        for (; i < 100; i++)
        {
            if (i % 10 == 0)
            {
                //Simulate Yield() function
                Thread.Sleep(0);
                Console.WriteLine("The Y thread");
            }
            Console.Write(i + ":Y ");
        }
    }
}

I get the compile time error:

System.Threading.Thread does not contain a definition for ‘Yield’

Answered by Tudor. This method will only work on .Net 4.0 and upwards.

Ideally I would want one thread to start and want each thread to execute for 10 incremented of i each. With my current method, I either get all ‘X’ or all ‘Y’.

Edit:
With inputs from Tudor and TheHe – I have been able to get alternate X and Y. The crux of the problem was usage of lock object. But the output of this code is not predictable.

  • 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-11T07:48:52+00:00Added an answer on June 11, 2026 at 7:48 am

    Thread.Yield will simply enable the scheduler to select a different thread that is ready to run:

    Causes the calling thread to yield execution to another thread that is
    ready to run on the current processor. The operating system selects
    the thread to yield to.

    If other threads in your application are also waiting on that lock, you can yield all you want, they won’t get a chance to run.

    Btw, Yield is a .NET 4.0+ method. Make sure you’re not targeting an earlier version.

    Edit: IMO, to do what you want you should use events:

    class Test
    {
        //Index i is declared as static so that both the threads have only one copy
        static int i;
    
        static AutoResetEvent parentEvent = new AutoResetEvent(true);
        static AutoResetEvent childEvent = new AutoResetEvent(false);
    
        static void Main(string[] args)
        {
            Thread t = new Thread(WriteY);
    
            i = 0;
    
            //Start thread Y
            t.Start();
            // Print X on the main thread
            parentEvent.WaitOne();
            while (i < 100)
            {                
                if (i % 10 == 0)
                {
                    childEvent.Set();
                    parentEvent.WaitOne();
                }
                Console.Write(i + ":Y ");
                i++;
            }
            t.Join();
        }
    
        static void WriteY()
        {
            childEvent.WaitOne();
            while (i < 100)
            {
                if (i % 10 == 0)
                {
                    parentEvent.Set();
                    childEvent.WaitOne();
                }
                Console.Write(i + ":X ");
                i++;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand basic OS concepts Want to know if my understanding
I am new to thread programming in Java. To understand threading I'm trying to
I am new to mutli-threading in java and trying to understand the keyword synchronization
Trying to understand the options for will_paginate's paginate method: :page — REQUIRED, but defaults
I am trying to understand the advantages of multiprocessing over threading . I know
I am trying to make a WCF REST method entirely asynchronous (I don't want
I'm trying to understand multi-threading in TCP so I'm coding a basic telnet text
I am trying to understand the best method to speed up a little program
I am currently trying to achieve thread synchronization in C++ .net using only atomic
I'm trying to understand threading better. If I create a program that allows people

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.