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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:06:36+00:00 2026-06-04T07:06:36+00:00

I have a problem using Semaphore in threading.. this is my situation, i want

  • 0

I have a problem using Semaphore in threading.. this is my situation, i want to change the ListViewItem‘s background color at the current 3 threads then turns into another color after a certain time passess using PauseForMilliSeconds then release another thread after 1 is done so i can limit the maximum thread execution into 3 threads only but the problem is the application will not respond.

this is my code

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Threading;

namespace _Sample__Using_Semaphore
{
public partial class frmMain : Form
{
    Semaphore semaphore = new Semaphore(0, 3);

    public frmMain()
    {
        InitializeComponent();
    }

    private void frmMain_Load(object sender, EventArgs e)
    {
        for (int i = 1; i <= 10; i++)
        {
            ListViewItem lvi = new ListViewItem(new string[] { i.ToString(), "Ready", "0" });
            lvItems.Items.Add(lvi);
        }
    }        

    private void btnStartStop_Click(object sender, EventArgs e)
    {
        semaphore.Release(3);

        for (int i = 0; i < lvItems.Items.Count; i++)
        {
            WorkerThread(i);
        }
    }

    private Thread WorkerThread(int startNum)
    {
        Thread t = new Thread(() => WorkerProcess(startNum));
        t.Start();

        return t;
    }

    private void WorkerProcess(int startNum)
    {
        Invoke((MethodInvoker)delegate()
        {
            ProcessMe(startNum);
        });
    }

    private void ProcessMe(int index)
    {
        Random rand = new Random();

        semaphore.WaitOne();

        lvItems.Items[index].BackColor = Color.Red;

        PauseForMilliSeconds(rand.Next(500, 5000));

        lvItems.Items[index].BackColor = Color.Yellow;

        semaphore.Release(1);
    }

    public DateTime PauseForMilliSeconds(int MilliSecondsToPauseFor)
    {
        DateTime ThisMoment = DateTime.Now;
        TimeSpan duration = new TimeSpan(0, 0, 0, 0, MilliSecondsToPauseFor);
        DateTime AfterWards = ThisMoment.Add(duration);

        while (AfterWards >= ThisMoment)
        {
            System.Windows.Forms.Application.DoEvents();
            ThisMoment = DateTime.Now;
        }

        return DateTime.Now;
    }
}
}

any help or solution to my problem?

SOLUTION:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Threading;

namespace _Sample__Using_Semaphore
{
public partial class frmMain : Form
{
    Semaphore semaphore = new Semaphore(0, 3);

    public frmMain()
    {
        InitializeComponent();
    }

    private void frmMain_Load(object sender, EventArgs e)
    {
        for (int i = 1; i <= 10; i++)
        {               
            ListViewItem lvi = new ListViewItem(new string[] { i.ToString(), "Ready", "0" });
            lvItems.Items.Add(lvi);
        }
    }        

    private void btnStartStop_Click(object sender, EventArgs e)
    {
        semaphore.Release(3);

        foreach (ListViewItem lvi in lvItems.Items)
        {
            WorkerThread(lvi.Index);
        }            
    }

    private Thread WorkerThread(int startNum)
    {
        Thread t = new Thread(() => WorkerProcess(startNum));
        t.Start();

        return t;
    }

    private void WorkerProcess(int startNum)
    {
        ProcessMe(startNum);            
    }

    private void ProcessMe(int index)
    {
        Random rand = new Random();

        semaphore.WaitOne();

        Invoke((MethodInvoker)delegate()
        {
            lvItems.Items[index].BackColor = Color.Red;
        });           

        PauseForMilliSeconds(rand.Next(500, 5000));

        Invoke((MethodInvoker)delegate()
        {
            lvItems.Items[index].BackColor = Color.Yellow;
        });

        semaphore.Release(1);
    }

    public DateTime PauseForMilliSeconds(int MilliSecondsToPauseFor)
    {
        DateTime ThisMoment = DateTime.Now;
        TimeSpan duration = new TimeSpan(0, 0, 0, 0, MilliSecondsToPauseFor);
        DateTime AfterWards = ThisMoment.Add(duration);

        while (AfterWards >= ThisMoment)
        {
            System.Windows.Forms.Application.DoEvents();
            ThisMoment = DateTime.Now;
        }

        return DateTime.Now;
    }
}
}
  • 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-04T07:06:37+00:00Added an answer on June 4, 2026 at 7:06 am

    I think your problem comes from the Invoke which is misplaced. By doing so, you are actually executing the Process me code in the main thread which is blocked by the sleep call.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
    
        public partial class Form1 : Form
        {
            Semaphore semaphore = new Semaphore(0, 3);
            public Form1()
            {
                InitializeComponent();
                myDelegate = new ChangeBack(ChangeBackMethod); 
            }
    
             private void frmMain_Load(object sender, EventArgs e)
        {
            for (int i = 1; i <= 10; i++)
            {
                ListViewItem lvi = new ListViewItem(new string[] { i.ToString(), "Ready", "0" });
                lvItems.Items.Add(lvi);
            }
        }        
    
        private void btnStartStop_Click(object sender, EventArgs e)
        {
            semaphore.Release(3);
    
            for (int i = 0; i < lvItems.Items.Count; i++)
            {
                WorkerThread(i);
            }
        }
    
        private Thread WorkerThread(int startNum)
        {
            Thread t = new Thread(new ParameterizedThreadStart(WorkerProcess));
            t.Start(startNum);
    
            return t;
        }
    
        private void WorkerProcess(object startNum)
        {
    
                ProcessMe((int)startNum);
    
        }
    
        private void ProcessMe(int index)
        {
            Random rand = new Random();
    
            semaphore.WaitOne();
    
    
            lvItems.BeginInvoke(myDelegate, index, Color.Red);
    
            Thread.Sleep(rand.Next(500, 5000));
    
            lvItems.BeginInvoke(myDelegate, index, Color.Yellow);
    
            semaphore.Release();
    
        }
        public delegate void  ChangeBack(int index, Color c);
        private ChangeBack myDelegate;
        private void ChangeBackMethod(int index, Color c)
        {
    
            lvItems.BeginUpdate();
            ((ListViewItem)(lvItems.Items[index])).BackColor = c;
            lvItems.EndUpdate();
        }
    
        public DateTime PauseForMilliSeconds(int MilliSecondsToPauseFor)
        {
            DateTime ThisMoment = DateTime.Now;
            TimeSpan duration = new TimeSpan(0, 0, 0, 0, MilliSecondsToPauseFor);
            DateTime AfterWards = ThisMoment.Add(duration);
    
            while (AfterWards >= ThisMoment)
            {
                //System.Windows.Forms.Application.DoEvents();
                ThisMoment = DateTime.Now;
            }
    
            return DateTime.Now;
        }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a problem using the Catch Clipboard Events code found on this link
I have a problem using scipy.weave.inline. I want to program a unitstep function centered
I have problem using arabic font for iOS. All fonts have the same render,
i have problem using LIKE structure in DB2 : for example: select * from
I have problem while using jquery maskedinput with asp.net textbox. I have a check
I have a problem using Linq to NHibernate to load an object and eagerly
I have a problem using the SSIS. I try to import data from database
I have a problem using JSON and arrays. Here is my code: while($row =
I have a problem using the string function erase with iterators. The function below
I have a problem using a simple AppleScript on Mac OSX 10.7.3. With the

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.