Пример #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim() == "")
            {
                return;
            }
            if (selectedString != null)
            {
                selectedString.Selected = false;
            }
            DrawString ds = new DrawString(textBox1.Text, label7.BackColor, (float)numericUpDown1.Value, (float)numericUpDown2.Value, (float)numericUpDown3.Value, (int)numericUpDown4.Value, panel1);

            selectedString = ds;
            drawStrings.Add(ds);
            panel1.Refresh();


            /*try
             * {
             * semafor.WaitOne();*/
            ThreadStart startDelegate = new ThreadStart(ds.Start);
            Thread      t             = new Thread(startDelegate);

            t.IsBackground = true;
            t.Start();

            /* }
             * finally
             * {
             *    semafor.Release();
             * }*/
        }
Пример #2
0
        public Form1()
        {
            semafor = new Semaphore(1, 1);
            InitializeComponent();
            drawStrings = new ArrayList();
            Random rnd   = new Random();
            int    speed = rnd.Next(10) + 1;
            int    x     = rnd.Next(panel1.Width);
            int    y     = rnd.Next(panel1.Height);
            int    angle = rnd.Next(360);
            int    red   = rnd.Next(256);
            int    green = rnd.Next(256);
            int    blue  = rnd.Next(256);

            DrawString ds = new DrawString("pierwszy napis", Color.FromArgb(red, green, blue), angle, x, y, speed, panel1);

            drawStrings.Add(ds);
            selectedString = ds;

            ThreadStart startDelegate = new ThreadStart(ds.Start);
            Thread      t             = new Thread(startDelegate);

            t.IsBackground = true;
            t.Start();
        }
Пример #3
0
        public Form1()
        {
            semafor = new Semaphore(1, 1);
            InitializeComponent();
            drawStrings = new ArrayList();
            Random rnd = new Random();
            int speed = rnd.Next(10) + 1;
            int x = rnd.Next(panel1.Width);
            int y = rnd.Next(panel1.Height);
            int angle = rnd.Next(360);
            int red = rnd.Next(256);
            int green = rnd.Next(256);
            int blue = rnd.Next(256);

            DrawString ds = new DrawString("pierwszy napis", Color.FromArgb(red, green, blue), angle, x, y, speed, panel1);
            drawStrings.Add(ds);
            selectedString = ds;

            ThreadStart startDelegate = new ThreadStart(ds.Start);
            Thread t = new Thread(startDelegate);
            t.IsBackground = true;
            t.Start();
        }
Пример #4
0
        private void button1_Click(object sender, EventArgs e)
        {
            
                if (textBox1.Text.Trim() == "") return;
                if (selectedString != null) selectedString.Selected = false;
                DrawString ds = new DrawString(textBox1.Text, label7.BackColor, (float)numericUpDown1.Value, (float)numericUpDown2.Value, (float)numericUpDown3.Value, (int)numericUpDown4.Value, panel1);
                selectedString = ds;
                drawStrings.Add(ds);
                panel1.Refresh();

            
            /*try
            {
               semafor.WaitOne();*/
                ThreadStart startDelegate = new ThreadStart(ds.Start);
                Thread t = new Thread(startDelegate);
                t.IsBackground = true;
                t.Start();
          /* }
            finally
            {
                semafor.Release();
            }*/
        }
Пример #5
0
        private void Multithread(object sender, System.Windows.Forms.KeyEventArgs e)
        {

            mutexxx = new Mutex(false, MUTEX_GUID);      

            if (selectedString == null) return;
            Keys keyPress = e.KeyCode;
            switch (keyPress)
            {
                case (Keys.F1):
                    {
                        lock (selectedString)
                        {
                            if (selectedString.Speed < 10) selectedString.Speed++;
                        }
                        break;
                    }
                case (Keys.F2):
                    {
                        lock (selectedString)
                        {
                            if (selectedString.Speed > 1) selectedString.Speed--;
                        }
                        break;
                    }
                case (Keys.F3):
                    {
                        Monitor.Enter(LockObject);
                        try
                       // lock (selectedString)
                        {
                            selectedString.Angle -= 10;
                            if (selectedString.Angle < 0) selectedString.Angle += 360;
                        }
                        finally
                        {
                            Monitor.Exit(LockObject);
                        }
                        panel1.Refresh();
                        break;
                    }
                case (Keys.F4):
                    {
                        mutexxx.WaitOne();

                        {
                            try
                            {
                                selectedString.Angle += 10;
                                if (selectedString.Angle >= 360) selectedString.Angle -= 360;
                            }
                            finally
                            {
                                mutexxx.ReleaseMutex();

                            }
                        }
                        panel1.Refresh();
                        break;
                    }
                case (Keys.F5):
                    {
                        int index = drawStrings.IndexOf(selectedString);
                        if (++index > drawStrings.Count - 1) index = 0;
                        lock (selectedString)
                        {
                            selectedString.Selected = false;
                        }
                        selectedString = (DrawString)drawStrings[index];
                        lock (selectedString)
                        {
                            selectedString.Selected = true;
                        }
                        panel1.Refresh();
                        break;
                    }
                case (Keys.F6):
                    {
                        int index = drawStrings.IndexOf(selectedString);
                        drawStrings.Remove(selectedString);
                        if ((selectedString.Thread.ThreadState & ThreadState.Suspended) == ThreadState.Suspended) selectedString.Thread.Resume();
                        selectedString.Thread.Abort();
                        selectedString.Dispose();
                        if (index > drawStrings.Count - 1) index = 0;
                        if (drawStrings.Count == 0)
                            selectedString = null;
                        else
                        {
                            selectedString = (DrawString)drawStrings[index];
                            selectedString.Selected = true;
                        }
                        panel1.Refresh();
                        break;
                    }
                case (Keys.F7):
                    {
                        /*_pauseEvent.WaitOne();
                        if (selectedString.Thread.ThreadState == System.Threading.ThreadState.WaitSleepJoin)
                        {
                            _pauseEvent.Set();
                        }

                        else
                        {
                            _pauseEvent.Reset();
                        } 
                            
                              
                            */
                        selectedString.Thread.Suspend();
                        break;
                    }
                case (Keys.F8):
                    {
                        if ((selectedString.Thread.ThreadState & ThreadState.Suspended) == ThreadState.Suspended)
                              selectedString.Thread.Resume();
                           // _pauseEvent.Set();
                         // wh.WaitOne();
                        break;
                    }
            }
        }
Пример #6
0
        private void Multithread(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            mutexxx = new Mutex(false, MUTEX_GUID);

            if (selectedString == null)
            {
                return;
            }
            Keys keyPress = e.KeyCode;

            switch (keyPress)
            {
            case (Keys.F1):
            {
                lock (selectedString)
                {
                    if (selectedString.Speed < 10)
                    {
                        selectedString.Speed++;
                    }
                }
                break;
            }

            case (Keys.F2):
            {
                lock (selectedString)
                {
                    if (selectedString.Speed > 1)
                    {
                        selectedString.Speed--;
                    }
                }
                break;
            }

            case (Keys.F3):
            {
                Monitor.Enter(LockObject);
                try
                // lock (selectedString)
                {
                    selectedString.Angle -= 10;
                    if (selectedString.Angle < 0)
                    {
                        selectedString.Angle += 360;
                    }
                }
                finally
                {
                    Monitor.Exit(LockObject);
                }
                panel1.Refresh();
                break;
            }

            case (Keys.F4):
            {
                mutexxx.WaitOne();

                {
                    try
                    {
                        selectedString.Angle += 10;
                        if (selectedString.Angle >= 360)
                        {
                            selectedString.Angle -= 360;
                        }
                    }
                    finally
                    {
                        mutexxx.ReleaseMutex();
                    }
                }
                panel1.Refresh();
                break;
            }

            case (Keys.F5):
            {
                int index = drawStrings.IndexOf(selectedString);
                if (++index > drawStrings.Count - 1)
                {
                    index = 0;
                }
                lock (selectedString)
                {
                    selectedString.Selected = false;
                }
                selectedString = (DrawString)drawStrings[index];
                lock (selectedString)
                {
                    selectedString.Selected = true;
                }
                panel1.Refresh();
                break;
            }

            case (Keys.F6):
            {
                int index = drawStrings.IndexOf(selectedString);
                drawStrings.Remove(selectedString);
                if ((selectedString.Thread.ThreadState & ThreadState.Suspended) == ThreadState.Suspended)
                {
                    selectedString.Thread.Resume();
                }
                selectedString.Thread.Abort();
                selectedString.Dispose();
                if (index > drawStrings.Count - 1)
                {
                    index = 0;
                }
                if (drawStrings.Count == 0)
                {
                    selectedString = null;
                }
                else
                {
                    selectedString          = (DrawString)drawStrings[index];
                    selectedString.Selected = true;
                }
                panel1.Refresh();
                break;
            }

            case (Keys.F7):
            {
                /*_pauseEvent.WaitOne();
                 * if (selectedString.Thread.ThreadState == System.Threading.ThreadState.WaitSleepJoin)
                 * {
                 *  _pauseEvent.Set();
                 * }
                 *
                 * else
                 * {
                 *  _pauseEvent.Reset();
                 * }
                 *
                 *
                 */
                selectedString.Thread.Suspend();
                break;
            }

            case (Keys.F8):
            {
                if ((selectedString.Thread.ThreadState & ThreadState.Suspended) == ThreadState.Suspended)
                {
                    selectedString.Thread.Resume();
                }
                // _pauseEvent.Set();
                // wh.WaitOne();
                break;
            }
            }
        }