static async System.Threading.Tasks.Task Main(string[] args)
        {
            Console.WriteLine("Hello World!");


            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            Bos        bos1   = new Bos(50, 50, 1000);
            Bos        bos2   = new Bos(50, 50, 500);
            List <Bos> bossen = new List <Bos>()
            {
                bos1, bos2
            };

            //Apen maken
            Aap        Tijs   = new Aap("Tijs");
            Aap        Bart   = new Aap("Bart");
            Aap        Bram   = new Aap("Bram");
            Aap        Gust   = new Aap("Gust");
            Aap        Charel = new Aap("Charel");
            List <Aap> apen1  = new List <Aap> {
                Tijs, Bart, Bram
            };
            List <Aap> apen2 = new List <Aap> {
                Gust, Charel
            };

            //Apen aan het bos toevoegen
            bos1.VoegApenToe(apen1);
            bos2.VoegApenToe(apen2);


            //Alle apen ontsnappen asynchroon
            DoeAsynchroon da = new DoeAsynchroon();
            List <Task>   ontsnappingTasks = new List <Task>();

            foreach (Bos bos in bossen)
            {
                ontsnappingTasks.Add(da.LaatApenOntsnappen(bos));
            }
            Task.WaitAll(ontsnappingTasks.ToArray());

            //Alle data die je nodig hebt voor de rest is geschreven, de rest kan allemaal asynchroon.
            await da.DatabankBitmapsBestandenDoen(bossen);

            stopwatch.Stop();
            Console.WriteLine($"Time elamsed : {stopwatch.Elapsed}");
        }
        public async Task VoegMonkeyRecordToe(Bos bos)
        {
            Console.WriteLine($"start toevoegen monkeyrecords voor bos {bos.BosId}");
            SqlConnection connection  = getConnection();
            string        queryMonkey = "INSERT INTO dbo.MonkeyRecords(monkeyId, monkeyName, woodId, seqnr, treeId, x, y) VALUES(@monkeyId, @monkeyName, @woodId, @seqnr, @treeId, @x, @y)";

            using (SqlCommand command = connection.CreateCommand())
            {
                connection.Open();
                try
                {
                    command.Parameters.Add(new SqlParameter("@monkeyId", SqlDbType.Int));
                    command.Parameters.Add(new SqlParameter("@monkeyName", SqlDbType.NVarChar));
                    command.Parameters.Add(new SqlParameter("@woodId", SqlDbType.Int));
                    command.Parameters.Add(new SqlParameter("@seqnr", SqlDbType.Int));
                    command.Parameters.Add(new SqlParameter("@treeId", SqlDbType.Int));
                    command.Parameters.Add(new SqlParameter("@x", SqlDbType.Int));
                    command.Parameters.Add(new SqlParameter("@y", SqlDbType.Int));
                    command.CommandText = queryMonkey;

                    for (int aapTeller = 0; aapTeller < bos.Apen.Count; aapTeller++)
                    {
                        Aap aap = bos.Apen[aapTeller];
                        for (int boomTeller = 0; boomTeller < aap.bezochteBomen.Count; boomTeller++)
                        {
                            Boom boom = aap.bezochteBomen[boomTeller];
                            command.Parameters["@monkeyId"].Value   = aap.Id;
                            command.Parameters["@monkeyName"].Value = aap.Naam;
                            command.Parameters["@woodId"].Value     = bos.BosId;
                            command.Parameters["@seqnr"].Value      = boomTeller;
                            command.Parameters["@treeId"].Value     = boom.Id;
                            command.Parameters["@x"].Value          = boom.X;
                            command.Parameters["@y"].Value          = boom.Y;
                            command.ExecuteNonQuery();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
                finally
                {
                    connection.Close();
                }
                Console.WriteLine($"stop toevoegen monkeyrecords voor bos {bos.BosId}");
            }
        }
Exemple #3
0
        public async Task StartOntsnappingAap(Aap aap)
        {
            Console.WriteLine($"{aap.Naam} start ontsnapping");
            bool   ontsnapt     = false;
            Boom   huidigeBoom  = aap.bezochteBomen[0];
            Boom   dichtsteBoom = null;
            double minimum      = Xmax * Ymax;

            while (!ontsnapt)
            {
                //zoeken naar dichtsbijzijnde boom
                //Alle bomen overlopen, minimum vervangen door nieuw minimum.
                for (int teller = 0; teller < Bomen.Count; teller++)
                {
                    if (!aap.bezochteBomen.Contains(Bomen[teller]))
                    {
                        double afstandTotBoom = Math.Sqrt(Math.Pow(huidigeBoom.X - Bomen[teller].X, 2) + Math.Pow(huidigeBoom.Y - Bomen[teller].Y, 2));
                        if (afstandTotBoom < minimum)
                        {
                            minimum      = afstandTotBoom;
                            dichtsteBoom = Bomen[teller];
                        }
                        //Als de boom niet dichter staat dan het minimum wordt hij geskipped
                    }
                    //Als de aap wel al op deze boom is geweest wordt deze boom geskipped!
                }

                double afstandTotRand = (new List <double>()
                {
                    Ymax - huidigeBoom.Y, Xmax - huidigeBoom.X, huidigeBoom.Y - 0, huidigeBoom.X - 0
                }).Min();
                if (afstandTotRand < minimum)
                {
                    ontsnapt = true;
                }
                else
                {
                    huidigeBoom = dichtsteBoom;
                    aap.bezochteBomen.Add(huidigeBoom);
                    minimum = Xmax * Ymax;
                }
            }
            Console.WriteLine($"{aap.Naam} is ontsnapt");
        }
        public async Task voegLogsToe(Bos bos)
        {
            Console.WriteLine($"start toevoegen logs voor bos {bos.BosId}");
            SqlConnection connection = getConnection();
            string        queryLogs  = "INSERT INTO dbo.logs(woodId, monkeyId, message) VALUES(@woodId, @monkeyId, @message)";

            using (SqlCommand command = connection.CreateCommand())
            {
                connection.Open();
                try
                {
                    command.Parameters.Add(new SqlParameter("@woodId", SqlDbType.Int));
                    command.Parameters.Add(new SqlParameter("@monkeyId", SqlDbType.Int));
                    command.Parameters.Add(new SqlParameter("@message", SqlDbType.NVarChar));
                    command.CommandText = queryLogs;
                    for (int aapTeller = 0; aapTeller < bos.Apen.Count; aapTeller++)
                    {
                        Aap aap = bos.Apen[aapTeller];
                        for (int boomTeller = 0; boomTeller < aap.bezochteBomen.Count; boomTeller++)
                        {
                            Boom boom = aap.bezochteBomen[boomTeller];
                            command.Parameters["@monkeyId"].Value = aap.Id;
                            command.Parameters["@woodId"].Value   = bos.BosId;
                            command.Parameters["@message"].Value  = $"{aap.Naam} is now in tree {boom.Id} at location ({boom.X},{boom.Y})";
                            command.ExecuteNonQuery();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
                finally
                {
                    connection.Close();
                }
            }
            Console.WriteLine($"stop toevoegen logs voor bos {bos.BosId}");
        }