private void Button_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();

            fd.Filter = "XML files (*.xml)|*.xml|All files (*.*)|*.*";

            if (fd.ShowDialog() == true)
            {
                XmlDocument parsedStream = new XmlDocument();
                parsedStream.Load(fd.FileName);
                XmlNodeList nodes = parsedStream.DocumentElement.SelectNodes("T");
                lstBox.Items.Clear();

                Thread[]  threads = new Thread[4];
                AStruct[] structs = new AStruct[4];
                for (int i = 0; i < threads.Count(); i++)
                {
                    ParameterizedThreadStart pm = new ParameterizedThreadStart(LoadXML);
                    threads[i]       = new Thread(pm);
                    structs[i]       = new AStruct();
                    structs[i].index = i;
                    structs[i].nodes = nodes;
                }

                for (int i = 0; i < threads.Count(); i++)
                {
                    System.Threading.Thread.Sleep(10);
                    threads[i].Start(structs[i]);
                }
            }
        }
Ejemplo n.º 2
0
        private void ParallelDecompress()
        {
            var workingThreads = new Thread[Environment.ProcessorCount];

            for (int i = 0; i < workingThreads.Count(); i++)
            {
                workingThreads[i]      = new Thread(Perform);
                workingThreads[i].Name = $"Reading thread {i}";
                workingThreads[i].Start();
            }

            var writingThread = new Thread(writer.WriteResult);

            writingThread.Name = "writing Thread";
            writingThread.Start();

            foreach (var item in workingThreads)
            {
                item.Join();
            }

            writer.IsProcessingCompleted = true;

            writingThread.Join();
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            Calculator[] calc = new Calculator[8];
            Writer[] io = new Writer[1];

            Thread[] threads = new Thread[calc.Count() + io.Count()];

            for (int i = 0; i < calc.Count(); i++)
            {
                calc[i] = new Calculator();
                Thread thread = new Thread(new ThreadStart(calc[i].Process));
                thread.Start();
                threads[i] = thread;
            }
            for (int i = 0; i < io.Count(); i++)
            {
                io[i] = new Writer();
                Thread thread = new Thread(new ThreadStart(io[i].Process));
                thread.Start();
                threads[i + calc.Count()] = thread;
            }

            for (int i = 0; i < threads.Count(); i++)
            {
                threads[i].Join();
            }
        }
Ejemplo n.º 4
0
        public void Test_QueueHandler_4_threads_test()
        {


            List<string> result = new List<string>();
            var queueHandler = new QueueHandler<string>((p, token) => {

                Thread.Sleep(10);
                result.Add(p);
            });

            Action addAction = () =>
            {
                var someValue = 0;
                for (int i = 0; i < 10; ++i)
                {
                    queueHandler.AddItem(i.ToString());
                    someValue += queueHandler.QueueSize;
                }
            };

            Thread[] threads = new Thread[4];

            for(int i = 0; i < threads.Count(); ++i)
                threads[i] = new Thread(() => addAction());

            foreach (var thread in threads)
                thread.Start();

            Thread.Sleep(2000);

            Assert.Equal(40, result.Count);


        }
Ejemplo n.º 5
0
        public void SnowflakeIdGeneratorThread()
        {
            try
            {
                int      workerIdMax = (int)Math.Pow(2, 5) - 1;
                Thread[] threads     = new Thread[workerIdMax];
                for (int i = 0; i < threads.Count(); i++)
                {
                    threads[i] = new Thread(new ParameterizedThreadStart(SnowflakeIdGeneratorTask));
                    threads[i].Start(i + 1);
                }

                while (true)
                {
                    int threadsStop = 0;
                    for (int i = 0; i < threads.Count(); i++)
                    {
                        if (!threads[i].IsAlive)
                        {
                            threadsStop++;
                        }
                    }
                    if (threadsStop >= threads.Count())
                    {
                        break;
                    }
                    Thread.Sleep(1);
                }

                idGeneratorList.Add(new KeyValuePair <string, long>("1", 2));
                idGeneratorList.Add(new KeyValuePair <string, long>("4", 2));
                var dataRepeat = (from a in idGeneratorList
                                  group a by a.Value into g
                                  where g.Count() > 1
                                  select g.Key).ToList();

                Console.WriteLine(JsonConvert.SerializeObject(dataRepeat, Formatting.Indented));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                throw;
            }
        }
Ejemplo n.º 6
0
        public void ConstroiTorres()
        {
            Random rnd = new Random();

            //Console.WriteLine("======MultiThreads======");
            Thread[] Threads = new Thread[3];

            for (int i = 0; i < Threads.Count(); i++)
            {
                Threads[i]      = new Thread(new ThreadStart(IniciarSimulador));
                Threads[i].Name = i.ToString();
            }

            foreach (Thread t in Threads)
            {
                t.Start();
            }
        }
Ejemplo n.º 7
0
        public void Test_QueueHandler_cancel_test()
        {

            List<string> result = new List<string>();

            QueueHandler<string> queueHandler = null;

            queueHandler = new QueueHandler<string>((p, token) => {

                if (token.IsCancellationRequested)
                    return;

                Thread.Sleep(10);

                if (token.IsCancellationRequested)
                    return;

                result.Add(p);

                if (result.Count == 20)
                    queueHandler.Dispose();

            });

            Action addAction = () =>
            {
                for (int i = 0; i < 10; ++i)
                    queueHandler.AddItem(i.ToString());
            };

            Thread[] threads = new Thread[4];

            for (int i = 0; i < threads.Count(); ++i)
                threads[i] = new Thread(() => addAction());

            foreach (var thread in threads)
                thread.Start();

            Thread.Sleep(2000);

            Assert.Equal(20, result.Count);


        }
Ejemplo n.º 8
0
        static void Main(string[] args)
        {
            Thread[] _threads = new Thread[3];
            for (int i = 0; i < _threads.Count(); i++)
            {
                _threads[i] = new Thread(ThreadRun);
                _threads[i].Start();
            }

            Thread.Sleep(TimeSpan.FromSeconds(10));
            Console.WriteLine("set");
            _mre.Set();

            Console.WriteLine("Reset");
            _mre.Reset();

            Console.WriteLine("end");
            Console.Read();
        }
Ejemplo n.º 9
0
        private void ParallelCompress()
        {
            var workingThreads = new Thread[Environment.ProcessorCount];

            for (int i = 0; i < workingThreads.Count(); i++)
            {
                workingThreads[i]      = new Thread(Perform);
                workingThreads[i].Name = $"Working thread {i}";
                workingThreads[i].Start();
            }

            foreach (var item in workingThreads)
            {
                item.Join();
            }

            if (isCanceled)
            {
                Console.WriteLine("operation canceled");
            }
        }
Ejemplo n.º 10
0
        public static void ObjectEqualMutiThreadTest()
        {
            int Length = 20;

            Student[] stus = new Student[Length];


            Action <object> createObjA = (i) =>
            {
                int index = Convert.ToInt32(i);
                stus[index] = SingleTemplate <Student> .Instance;  //Student.Instance;
                Thread.Sleep(1000 * 2);
                Console.WriteLine("#创建对象 " + index);
            };

            Thread[] tds = new Thread[Length];
            for (int i = 0; i < tds.Count(); i++)
            {
                tds[i]              = new Thread(new ParameterizedThreadStart(createObjA));
                tds[i].Name         = "#Thread " + i;
                tds[i].IsBackground = true;
            }

            for (int i = 0; i < Length; i++)
            {
                tds[i].Start(i);
                tds[i].Join();
            }

            for (int i = 0; i < Length - 1; i++)
            {
                bool isEqual = stus[i].Equals(stus[i + 1]) && stus[i].Age.Equals(stus[i + 1].Age);
                if (!isEqual)
                {
                    Console.WriteLine("单例构造失败");
                }
            }

            Console.WriteLine("主线程执行完毕");
        }
Ejemplo n.º 11
0
        private void B_Start_Click(object sender, RoutedEventArgs e)
        {
            if (SensorsRunning)
            {
                SensorsRunning  = false;
                B_Start.Content = "Start";
            }
            else
            {
                SensorsRunning  = true;
                B_Start.Content = "Stop";
                Label[]  labels  = { L_01, L_02, L_03, L_04, L_05, L_06, L_07, L_08, L_09, L_10 };
                Thread[] threads = new Thread[10];


                for (int i = 0; i < threads.Count(); i++)
                {
                    threads[i] = new Thread(Sensor);
                    threads[i].Start(labels[i]);
                }
            }
        }
Ejemplo n.º 12
0
        public void ObjectEqualMutiThreadTest()
        {
            int Length = 20;

            Student[] stus = new Student[Length];


            Action <object> createObjA = (i) =>
            {
                int index = Convert.ToInt32(i);
                stus[index] = SingleTemplate <Student> .Instance;
            };

            Thread[] tds = new Thread[Length];
            for (int i = 0; i < tds.Count(); i++)
            {
                tds[i]              = new Thread(new ParameterizedThreadStart(createObjA));
                tds[i].Name         = "#Thread " + i;
                tds[i].IsBackground = true;
            }

            for (int i = 0; i < Length; i++)
            {
                tds[i].Start(i);
                tds[i].Join();
            }

            for (int i = 0; i < Length - 1; i++)
            {
                bool isEqual = stus[i].Equals(stus[i + 1]) && stus[i].Age.Equals(stus[i + 1].Age);
                if (!isEqual)
                {
                    Assert.IsTrue(isEqual, "单例构造失败!");
                }
            }

            Assert.Pass("单例构造成功!");
        }
Ejemplo n.º 13
0
        static void Main(string[] args)
        {
            int chunks = 64;
            int width  = chunks * 16;
            int height = chunks * 16;

            OverworldGeneratorV2 gen = new OverworldGeneratorV2();

            gen.ApplyBlocks = true;

            bool done = false;
            //  ChunkColumn[] generatedChunks = new ChunkColumn[chunks * chunks];
            ConcurrentQueue <ChunkCoordinates> chunkGeneratorQueue = new ConcurrentQueue <ChunkCoordinates>();

            long average = 0;
            long min     = long.MaxValue;
            long max     = long.MinValue;

            int chunskGenerated = 0;

            Thread[] threads = new Thread[Environment.ProcessorCount - 1];
            for (int t = 1; t < threads.Length; t++)
            {
                threads[t] = new Thread(() =>
                {
                    Stopwatch timing = new Stopwatch();
                    while (true)
                    {
                        if (chunkGeneratorQueue.TryDequeue(out var coords))
                        {
                            timing.Restart();

                            ChunkColumn column = gen.GenerateChunkColumn(coords);
                            // generatedChunks[(coords.X * chunks) + coords.Z] = column;
                            Finished.Enqueue(column);
                            chunskGenerated++;

                            timing.Stop();

                            average += timing.ElapsedMilliseconds;
                            if (timing.ElapsedMilliseconds < min)
                            {
                                min = timing.ElapsedMilliseconds;
                            }

                            if (timing.ElapsedMilliseconds > max)
                            {
                                max = timing.ElapsedMilliseconds;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                });
            }

            threads[0] = new Thread(() => { GenerateBiomeMap(chunks); });

            for (int x = 0; x < chunks; x++)
            {
                for (int z = 0; z < chunks; z++)
                {
                    chunkGeneratorQueue.Enqueue(new ChunkCoordinates(x, z));
                }
            }

            Stopwatch timer = Stopwatch.StartNew();

            foreach (var thread in threads)
            {
                thread.Start();
            }

            int threadsAlive = 0;

            do
            {
                threadsAlive = threads.Count(x => x.IsAlive);

                Console.Clear();

                Console.WriteLine($"Threads: {threadsAlive} Queued: {chunkGeneratorQueue.Count} Generated: {chunskGenerated} Avg: {average / Math.Max(1, chunskGenerated)}ms Min: {min}ms Max: {max}ms");
                Console.WriteLine($"Processed: {Imaged} Remaining: {Finished.Count}");

                Thread.Sleep(100);
            } while (threadsAlive > 0);

            timer.Stop();

            Console.Clear();

            Console.WriteLine($"Generating {chunks * chunks} chunks took: {timer.Elapsed}");
            Console.WriteLine($"Min Height: {gen.MinHeight} Max Height: {gen.MaxHeight}");
        }