有人可以帮我穿线吗? [英] Can someone help me with threading.

查看:119
本文介绍了有人可以帮我穿线吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些无法运行的代码.我已经提交了代码以供审核.预先感谢您的帮助.

I have some code that I can not get to run. I have submitted the code for review. Thank you in advance for your help.

<pre>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace IT481_Unit6_Assignment
{
    using System;
    using System.Threading;

    public class DressingRoom
    {
        private static Semaphore available;
    
        public static void Main()
        {
            int ClothingsItemsGenerator = 0;
            int GenNumberOfItems;
            int max = 10;
            int min = 1;

            //Create a random number of available rooms
            //create random class for number generation
            Random rndNum = new Random();
            int dressing = rndNum.Next(1, 20);
            //Program is only displaying this random number
            Console.WriteLine("The average number of items being tried on is " + dressing);
            Console.ReadKey();
            //create random number for customers
            Random rndCust = new Random();
            int cust = rndNum.Next(30, 50);
            Console.WriteLine("The average number of customers requesting rooms is " + cust);
            Console.ReadKey();
            Random rndSleep = new Random();
            int SleepTimer = rndSleep.Next(60000, 180000);
            Console.WriteLine("The average time spent in a dressing room is " + SleepTimer + "milliseconds.");
            Console.ReadKey();
            available = new Semaphore(min, max);

            for (int i = 1; i <= cust; i++)
            {
                if (ClothingsItemsGenerator == dressing)
                {
                    GenNumberOfItems = ClothingsItemsGenerator.Next((max - min) + 1) + min;
                    Console.WriteLine(ClothingsItemsGenerator + " itmes.");
                    NewMethod(GenNumberOfItems);
                }
                Console.WriteLine("Checking for available rooms...");

                Console.WriteLine("Customer {0} enters the dressing room", i);
                Console.WriteLine("Customer {0} leaves the dressing room", i);

                available.WaitOne();
                available.Release();

                Console.WriteLine("There are now {0} dressing rooms left.", dressing, available.Release());
                Console.Read();

                Thread t = new Thread(new ParameterizedThreadStart(DressingRoom));

                //Start letting customers use dressing rooms.
                DressingRoom.Start(i);
            }
        }

        private static void NewMethod(int GenNumberOfItems)
        {
            Customer cust = new Customer(GenNumberOfItems, CustNumCount);
        }

        private Semaphore avaliable;
        private static int ClothingsItmes = 0;
        private static readonly Random ClothingItemsGenerator = new Random();
        private static int sleepTime;
        private static int GenNumbersOfItems;
        private static readonly Random sleepTimeGenerator = new Random();
        private static int max = 20;
        private static int min = 1;
        private static int sleepMin = 60000;
        private static int sleepMax = 180000;
        private static int CustNumCount;

        public DressingRoom(int MAX_ROOMS_AVAILABLE, int CustNum, int NumberOfItems)
        {
            available = new Semaphore(CustNumCount, MAX_ROOMS_AVAILABLE);
            Console.WriteLine("Did it get here?");
            Console.WriteLine(CustNum);

            try
            {
                Console.WriteLine(CustNum);
                RequestRoom(NumberOfItems, CustNum);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                Console.WriteLine(e.StackTrace);
            }
        }

        public virtual void RequestRoom(int NumberOfItems, int CustNum)
        {
            Console.WriteLine("Is a customer requesting a room?");
            Console.WriteLine(CustNum);
            for (int i = 0; i < CustNum; i ++)
            {
                Console.WriteLine("How many itmes does the customer have");
                if (NumberOfItems == 0)
                {
                    GenNumbersOfItems = ClothingItemsGenerator.Next((max - min) + 1) + min;
                    Console.WriteLine(NumberOfItems + " itmes.");
                    Customer cust = new Customer(this, GenNumbersOfItems, CustNum);
                }
            }
        }

        public class Customer
        {
            private DressingRoom dressingRoom;
            private int genNumberOfItmes;
            private int custNum;
            private object custNum1;

            public Customer(int NumberOfItems)
            {
                int max = 20;
                int min = 1;
                Console.WriteLine("Does it make instantiate customer?");
                Console.WriteLine("Here is the total number items " + NumberOfItems);

                if (NumberOfItems ==0)
                {
                    NumberOfItems = ClothingItemsGenerator.Next(max - min + 1) + min;
                    Console.WriteLine(NumberOfItems);
                }
                else
                {
                    Console.WriteLine(NumberOfItems);
                }
            }

            public Customer(int NumberOfItmes, object custNum1)
                : this(NumberOfItmes)
            {
                this.custNum1 = custNum1;
            }

            public Customer(DressingRoom dressingroom, int genNumberOfItems, int custNum)
            {
                this.dressingRoom = dressingRoom;
                this.genNumberOfItmes = genNumberOfItmes;
                this.custNum = custNum;
            }
        }
        public virtual void Scenario()
        {
            try
            {
                Console.WriteLine("There are a total number of " + CustNumCount + " customer");
                available.WaitOne();
            }
            catch (ThreadInterruptedException)
            {
                Console.WriteLine("received thread interuption exception");
            }
            Console.WriteLine("Customer " + CustNumCount + " starts to use the room");
            Console.WriteLine(GenNumbersOfItems + " here is the number of itmes");

            for (int i = 0; i < GenNumbersOfItems; i++)
            {
                try
                {
                    sleepTime = sleepTimeGenerator.Next((sleepMax - sleepMin) + 1) + sleepMin;
                    Console.WriteLine("{0} goint to sleeo for {1:D} milliseconds. \n", sleepTime);
                    Thread.Sleep(sleepTime);
                }
                catch (ThreadInterruptedException)
                {
                    Console.WriteLine("Received thread interupted exception");
                }
                Console.WriteLine("Customer " + CustNumCount + " left the room");
                available.Release();
                
            }
        }
    }
}



我尝试过的事情:

我尝试过更改代码以使其正常工作.



What I have tried:

I have tried changing the code around to make it work.

推荐答案

我们无济于事:我们不知道您的代码应该做什么,它做什么是在做您没想到的事情,还是没有在做您想做的事情.

因此,这取决于您.
幸运的是,您拥有一个可用的工具,该工具可以帮助您查找正在发生的事情:调试器.如果您不知道如何使用它,那么快速的"Visua Studio调试器" Google应该会为您提供所需的信息.

在函数的第一行上放置一个断点,然后通过调试器运行代码.然后查看您的代码和数据,并确定应该手动执行的操作.然后,单行检查每一行,以确保您期望发生的事情确实是正确的.如果不是,那就是您遇到问题时,可以回溯(或再次运行并仔细查看)以找出原因.
抱歉,但是我们不能为您做到这一点-是时候让您学习一种新的(也是非常非常有用的)技能:调试!
We can''t help: we have no idea what your code is supposed to do, what it is doing that you didn''t expect, or not doing that you did.

So, it''s going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don''t know how to use it, then a quick Google for "Visua Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn''t, that''s when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can''t do that for you - time for you to learn a new (and very, very useful) skill: debugging!


这篇关于有人可以帮我穿线吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆