internal void RunParallelExamples() { Stopwatch watch = new Stopwatch(); // -------------------------------------------------------------------------------------------- // Mit der Klasse Parallel können relativ einfach parallele Aufgaben erzeugt und abgearbeitet werden // // -------------------------------------------------------------------------------------------- // Gibt 0 bis 9 in beliebiger Reihenfolge aus, da jeder Thread // zu einem anderen Zeitpunkt ausgeführt werden könnte. Console.Write("[Parallel.For] Results = "); Parallel.For(0, 10, a => Console.Write("{0}, ", a)); Console.WriteLine(); // -------------------------------------------------------------------------------------------- //Parallel.For(0, 50, a => a). // -------------------------------------------------------------------------------------------- // Parallel.ForEach // With Parallel.ForEach() an IEnumerable can be used to create tasks easily. watch.Restart(); string text = StringData.CreateLongString(); Console.Write("[Parallel.ForEach.Simple] Results = "); List <string> searchWords = new List <string> { "Gott", "Mann", "Weib" }; Parallel.ForEach(searchWords, a => Console.Write("{0}({1}) ", a, text .Split(new char[] { ' ', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries) .Count(b => string.Compare(a, b, true) == 0))); Console.WriteLine("in {0:0.000}s", watch.Elapsed.TotalSeconds); // -------------------------------------------------------------------------------------------- // Parallel.ForEach Options // Additionally one can use the ParallelOptions to setup some stuff, here the max amount of Tasks used. // Others are using a different TaskFactory oder including a CancelationToken. watch.Restart(); Console.Write("[Parallel.ForEach.Complex] Results = "); Parallel.ForEach( searchWords, new ParallelOptions { MaxDegreeOfParallelism = 2, }, // Default for MaxDegreeOfParallelism is -1 (w, s) => { int count = text .Split(new char[] { ' ', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries) .Count(b => string.Compare(w, b, true) == 0); Console.Write("{0}({1}) ", w, count); }); Console.WriteLine("in {0:0.000}s", watch.Elapsed.TotalSeconds); // -------------------------------------------------------------------------------------------- // Parallel.ForEach Break // With Break the Parallel exceution can be stopped. If break was called the next task will not be started. watch.Restart(); searchWords = new List <string> { "Gott", "Mann", "Weib", "Mensch", "Teufel", // "Hass", "Gnade", "Brot", "Wein", "Hund", "Tot" }; Console.Write("[Parallel.ForEach.Break] Break results "); ParallelLoopResult result = Parallel.ForEach( searchWords, new ParallelOptions { MaxDegreeOfParallelism = 2, }, (w, s) => { int count = text .Split(new char[] { ' ', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries) .Count(b => string.Compare(w, b, true) == 0); if (count < 20) { s.Break(); } }); Console.Write("in {0:0.000}s ", watch.Elapsed.TotalSeconds); Console.WriteLine("while LowestBreakIteration = {0}, Completed = {1}", result.LowestBreakIteration, result.IsCompleted); }
public void RunAdoNetExamples() { // -------------------------------------------------------------------------------------------- // - ADO.NET https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/ // // - SqlConnection class https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection // // Creating the abstract base classes string path = Path.Combine(Environment.CurrentDirectory, "database.mdf"); DbConnection connection = new System.Data.SqlClient.SqlConnection( string.Format(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename={0};Integrated Security=True", path)); connection.Open(); DbProviderFactory factory = DbProviderFactories.GetFactory(connection); // -------------------------------------------------------------------------------------------- // ExecuteNonQuery using (DbCommand createCommand = factory.CreateCommand()) { createCommand.Connection = connection; createCommand.CommandText = "CREATE TABLE [Words] ([Id] INT NOT NULL PRIMARY KEY, [Word] NVARCHAR(50) NOT NULL, [Count] INT NOT NULL)"; createCommand.ExecuteNonQuery(); } // -------------------------------------------------------------------------------------------- // ExecuteNonQuery with parameters using (DbCommand insertCommand = factory.CreateCommand()) { DateTime current = DateTime.UtcNow; insertCommand.Connection = connection; insertCommand.CommandText = "INSERT INTO [Words] ([Id], [Word], [Count]) VALUES (@id, @word, @count)"; DbParameter idParameter = factory.CreateParameter(); idParameter.DbType = System.Data.DbType.Int32; idParameter.ParameterName = "@id"; idParameter.Size = 4; idParameter.Value = 0; insertCommand.Parameters.Add(idParameter); DbParameter wordParameter = factory.CreateParameter(); wordParameter.DbType = System.Data.DbType.String; wordParameter.ParameterName = "@word"; wordParameter.Size = 100; wordParameter.Value = 0; insertCommand.Parameters.Add(wordParameter); DbParameter countParameter = factory.CreateParameter(); countParameter.DbType = System.Data.DbType.Int32; countParameter.Size = 4; countParameter.ParameterName = "@count"; countParameter.Value = 0; insertCommand.Parameters.Add(countParameter); insertCommand.Prepare(); int id = 0; string text = StringData.CreateMediumString(); string[] words = text.Split(new char[] { ' ', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); foreach (string word in words.Distinct()) { idParameter.Value = id++; wordParameter.Value = word; countParameter.Value = words.Count(a => a == word); insertCommand.ExecuteNonQuery(); } Console.WriteLine("[DbCommand.Parameters] Inserted {0} rows in {1:0.000}s", id + 1, (DateTime.UtcNow - current).TotalSeconds); } // -------------------------------------------------------------------------------------------- // ExecuteScalar // TODO using (DbCommand countCommand = factory.CreateCommand()) { DateTime current = DateTime.UtcNow; countCommand.Connection = connection; countCommand.CommandText = "SELECT COUNT(*) FROM [Words]"; int count = (int)countCommand.ExecuteScalar(); Console.WriteLine("[DbCommand.ExecuteScalar] Result Count = {0} in {1:0.000}s", count, (DateTime.UtcNow - current).TotalSeconds); } }