private void SomeJobBusy(Object obj) { Semaphore sem = Semaphore.OpenExisting("1A9191BF-AA26-46E1-BB85-BDA396BC6469"); MyTasksParam tobj = (MyTasksParam)obj; sem.WaitOne(); }
private void button1_Click(object sender, EventArgs e) { if (Index < maxsize) { string mess = "Задача " + (Index + 1) + " --> создана"; string mess2 = "Задача " + (Index + 1) + " --> ожидает"; MyTasksParam obj = new MyTasksParam(mess2); tasklist.Add(new Task(SomeJob, obj)); listBox3.Items.Add(mess); Index++; } else { MessageBox.Show("Achieved MaxSize of 100 tasks"); } }
private void SomeJob(Object obj) { try { Semaphore sem = Semaphore.OpenExisting("1A9191BF-AA26-46E1-BB85-BDA396BC6469"); MyTasksParam tobj = (MyTasksParam)obj; string str = tobj.TskMessage; curcontext.Send(t => listBox2.Items.Add(str), null); sem.WaitOne(); curcontext.Send(t => listBox2.Items.Remove(str), null); int counter = 0, idx = 0; str = str.Substring(0, (str.IndexOf('>') + 2)) + counter; curcontext.Send(t => listBox1.Items.Add(str), null); workingtasks.Add(tobj); while (counter < 101) { curcontext.Send(t => { idx = listBox1.Items.IndexOf(str); if (idx > -1) { str = str.Substring(0, (str.IndexOf('>') + 2)) + counter + '%'; listBox1.Items.RemoveAt(idx); listBox1.Items.Insert(idx, str); } }, null); Thread.Sleep(150); if (tobj.isCancelTask) { break; } counter++; } curcontext.Send(t => listBox1.Items.Remove(str), null); if (!tobj.isCancelTask) { sem.Release(); } workingtasks.Remove(tobj); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void numericUpDown1_ValueChanged(object sender, EventArgs e) { int nvalue = (int)numericUpDown1.Value; if (nvalue - curSize == 1) { semaphore.Release(1); } else if (workingtasks.Count > nvalue) { workingtasks[0].isCancelTask = true; } else { MyTasksParam obj = new MyTasksParam("IdleTask"); disposetasks.Add(Task.Factory.StartNew(SomeJobBusy, obj)); } curSize = nvalue; }