Click here to Skip to main content
15,220,077 members
   

C#

 
GeneralRe: User List Pin
Richard Andrew x6425-Mar-20 6:22
professionalRichard Andrew x6425-Mar-20 6:22 
GeneralRe: User List Pin
Randor 25-Mar-20 8:38
professional Randor 25-Mar-20 8:38 
GeneralRe: User List Pin
Richard Andrew x6425-Mar-20 8:57
professionalRichard Andrew x6425-Mar-20 8:57 
GeneralRe: User List Pin
Randor 25-Mar-20 9:18
professional Randor 25-Mar-20 9:18 
AnswerRe: User List Pin
Randor 25-Mar-20 13:27
professional Randor 25-Mar-20 13:27 
GeneralRe: User List Pin
Richard Andrew x6425-Mar-20 13:36
professionalRichard Andrew x6425-Mar-20 13:36 
GeneralRe: User List Pin
Richard Andrew x6427-Mar-20 14:37
professionalRichard Andrew x6427-Mar-20 14:37 
QuestionMutlithreading application threading and semaphores? Pin
auting8223-Mar-20 0:52
Memberauting8223-Mar-20 0:52 
Hi I am trying to develop a multitasking application in C# based on some code I have available that is depicted below. I have tried to do some changes but I am not sure if I put the WaitOne() and Release() methods at the correct places.
I am trying to include semaphore for a secure usage of common resources.
A semaphore is defined in C# by making a "Semaphore" variable, and use the methods WaitOne() and Release() to access the semaphore services.
I want to include name, student number and the semester year in the display information.
Sleep(0) statements can be used to test the sharing of common resources. I will use a console application.

The print out in console should be something like:

[T1]: Student no.
[T1]: Student name
[T1]: Semester year
[T2]: Student no.
[T2]: Student name
[T2]: Semester year
[T1]: Student no.
[T3]: Student no.
[T1]: Student name
[T2]: Student name



using System;
using System.Threading;
//Example in using sleep() and Threads in C#
namespace ThreadSys
{
    /// <summary>
    /// Threadclass
    /// </summary>
    class ThreadClass
    {
        int loopCnt, loopDelay;
        Thread cThread;
        public string studentname;
        static Semaphore semaphore = new Semaphore(1,1);
        public ThreadClass(string name, int delay)
        {
            loopCnt = 0;
            loopDelay = delay;
            cThread = new Thread(new ThreadStart(this.run));
            cThread.Name = name;
            
            cThread.Start();
        }
        // The main function in the ThreadClass
        void run()
        {
            Console.WriteLine(" Starting " + cThread.Name);
            do
            {
                loopCnt++;
                Thread.Sleep(loopDelay);
                Console.Write(" ");
                semaphore.WaitOne();
                Console.Write(cThread.Name);
                
                Console.Write(": ");
                Console.WriteLine("Loop=" + loopCnt);
                
                semaphore.Release();

            } while (loopCnt < 5);
            // Ending of the thread
            Console.WriteLine(" Ending " + cThread.Name);

        }
    }

    // The application
    class ThreadSys
    {
        /// <summary>
        /// Start of the main program
        /// </summary>
        static void Main(string[] args)
        {
            Console.WriteLine(" Start of main program ");
            // Making 3 threads ..
            ThreadClass ct1 = new ThreadClass("[T1]:", 95);            
            ThreadClass ct2 = new ThreadClass("[T2]:", 279);
            ThreadClass ct3 = new ThreadClass("[T3]:", 463);
            // Wait while the threads are running ...
            for (int cnt = 0; cnt < 30; cnt++)
            {
                Console.Write(".");
                Thread.Sleep(100);
            }

        }
    }
}

GeneralRe: Mutlithreading application threading and semaphores? Pin
Richard MacCutchan23-Mar-20 3:58
mveRichard MacCutchan23-Mar-20 3:58 
GeneralRe: Mutlithreading application threading and semaphores? Pin
auting8223-Mar-20 4:14
Memberauting8223-Mar-20 4:14 
GeneralRe: Mutlithreading application threading and semaphores? Pin
Dave Kreskowiak23-Mar-20 4:36
mveDave Kreskowiak23-Mar-20 4:36 
GeneralRe: Mutlithreading application threading and semaphores? Pin
Richard MacCutchan23-Mar-20 4:41
mveRichard MacCutchan23-Mar-20 4:41 
AnswerRe: Mutlithreading application threading and semaphores? Pin
Gerry Schmitz23-Mar-20 8:05
mveGerry Schmitz23-Mar-20 8:05 
AnswerRe: Mutlithreading application threading and semaphores? Pin
Bohdan Stupak24-Mar-20 1:14
professionalBohdan Stupak24-Mar-20 1:14 
QuestionRDLC printing without print preview Pin
Member 1419221622-Mar-20 21:20
MemberMember 1419221622-Mar-20 21:20 
AnswerRe: RDLC printing without print preview Pin
OriginalGriff22-Mar-20 21:34
mveOriginalGriff22-Mar-20 21:34 
GeneralRe: RDLC printing without print preview Pin
Member 1419221622-Mar-20 21:49
MemberMember 1419221622-Mar-20 21:49 
GeneralRe: RDLC printing without print preview Pin
OriginalGriff22-Mar-20 21:51
mveOriginalGriff22-Mar-20 21:51 
GeneralRe: RDLC printing without print preview Pin
Member 1419221622-Mar-20 22:08
MemberMember 1419221622-Mar-20 22:08 
SuggestionRe: RDLC printing without print preview Pin
Richard Deeming23-Mar-20 0:22
mveRichard Deeming23-Mar-20 0:22 
AnswerRe: RDLC printing without print preview Pin
Gerry Schmitz23-Mar-20 7:49
mveGerry Schmitz23-Mar-20 7:49 
QuestionNotify User From Server Pin
Kevin Marois22-Mar-20 16:11
professionalKevin Marois22-Mar-20 16:11 
AnswerRe: Notify User From Server Pin
Dave Kreskowiak22-Mar-20 18:41
mveDave Kreskowiak22-Mar-20 18:41 
GeneralRe: Notify User From Server Pin
Kevin Marois22-Mar-20 18:50
professionalKevin Marois22-Mar-20 18:50 
GeneralRe: Notify User From Server Pin
Dave Kreskowiak23-Mar-20 4:18
mveDave Kreskowiak23-Mar-20 4:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.