public void DepartmentAdd() { Thread[] threads = new Thread[12]; //run it 30 times. for (int x = 0; x < 30; x++) { //get a unique value per run for computer detail string deptValue = DateTime.Now.Ticks.ToString(); for (int i = 0; i < threads.Length; i++) { threads[i] = new Thread(new ParameterizedThreadStart(CreateDepartment)); } for (int i = 0; i < threads.Length; i++) { Thread thread = threads[i]; MyParams p2 = new MyParams(i, deptValue); thread.Start(p2); } //join all of them (wait) foreach (Thread thread in threads) { thread.Join(); } if (ErrorOnThread) { Assert.Fail("Dupe Test failed"); } } }
void CreateDepartment(object p) { SQLDataLayer dl = null; MyParams par = (MyParams)p; try { dl = new SQLDataLayer(ResourceSettings.Instance.GetDBConnString()); dl.BeginTransaction(); DataSet ds = dl.ExecuteSP("dbo.sp_Department_Add", dl.CreateParam("piDepartment", par.DeptValue), dl.CreateParam("piIsIT", true), dl.CreateParam("piActive", true)); dl.Commit(); if (ds.Tables[0].Rows[0]["Department_SID"] != DBNull.Value) { TestContext.WriteLine(string.Format("INSERTED for {0}", par.ThreadNum)); } else { TestContext.WriteLine(string.Format("Already exists for {0}", par.ThreadNum)); } } catch (Exception ex) { dl.Rollback(); ErrorOnThread = true; TestContext.WriteLine(string.Format("Exception for {0}:{1}", par.ThreadNum, ex.Message)); } finally { dl.Dispose(); } }