3
Answers

Threading

Sir/Madam,

    In  the below coding i have used three threads and give me a solution to stop one particular thread.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Threading;

namespace AQP_Network
{
    public partial class AQP_Network: Form
    {
        //Operations opr = new Operations();
      //  System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
        bool editbuttonclick = false;
      
        ThreadStart job;
        Thread th;
        Image background = Image.FromFile(@"G:\ieee projects\AQP-Network\123.jpg");
        Image red_car = Image.FromFile(@"G:\ieee projects\AQP-Network\obj1 copy.gif");
        Image red_car1 = Image.FromFile(@"G:\ieee projects\AQP-Network\fantasy_133.gif");
        Image red_car2 = Image.FromFile(@"G:\ieee projects\AQP-Network\lilman.gif");
        int x = 6, y = 146, m = 3,x2=93,y2=10,m2=3;
        int x1 = 980, y1 = 9, m1 = 3;
        int count = 0, count1 = 0, count2 = 0;
        public AQP_Network()
        {
            Text = "Simple Animation";
            CenterToScreen();

            job = new ThreadStart(Threadjob);
            th = new Thread(job);

            th.Start();

            ThreadStart job1 = new ThreadStart(Threadjob1);
            Thread th1 = new Thread(job1);
            th1.Start();


            ThreadStart job2 = new ThreadStart(Threadjob2);
            Thread th2 = new Thread(job2);
            th2.Start();
          

            //enabling double-buffering to avoid flicker
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.DoubleBuffer, true);
            this.WindowState = FormWindowState.Maximized;
        }
        void OnPaint(Object sender, PaintEventArgs e)
        {
            Thread.Sleep(50);
           

            if (count < 70)
            {
                if (x < Width)
                {
                    if (count == m)
                    {
                        x += 1;
                        y -= 1;
                        m = m + 3;
                    }
                    else
                    {
                        x += 2;
                        y +=1;
                        count++;
                    }

                }
              
            }
            if (count >= 70 && count < 145)
            {
                if (x < Width)
                {
                    if (count == m)
                    {
                        x += 1;
                        y -= 1;
                        m = m + 3;
                    }
                    else
                    {
                        x += 2;
                        y += 2;
                        count++;


                    }

                }

             
            }
                 
            Graphics g = e.Graphics;
            DrawStuff(g);
            Invalidate();

        }

      
        void DrawStuff(Graphics g)
        {
            g.DrawImage(background, 0, 0);
            g.DrawImage(red_car, x, y);
           
            Invalidate();
            Button l = new Button();
            l.Location = new Point(100, 100);
            l.Text = "ooo";
            this.Controls.Add(l);
      
          
        }
        void DrawStuff1(Graphics g1)
        {
          
            g1.DrawImage(red_car1, x1, y1);
            Invalidate();
        }
        void DrawStuff2(Graphics g2)
        {
            g2.DrawImage(red_car2, x2, y2);
            Invalidate();
        }
        void Threadjob()
        {
            Paint += new PaintEventHandler(OnPaint);
            Invalidate();

        }
        void Threadjob1()
        {
            Paint+=new PaintEventHandler(AQP_Network_Paint);
            Invalidate();
        }
        void Threadjob2()
        {
            Paint += new PaintEventHandler(OnPaint1);
        }

        private void Main_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;

        }
        private void AQP_Network_Paint(object sender,PaintEventArgs  e)
        {
          
            if (count1 < 350)
            {
                if (x1 < Width)
                {
                    if (count1 == m1)
                    {
                        x1 -= 1;
                        y1 += 1;
                        m1 = m1 + 3;
                        count1++;
                    }
                    else
                    {
                        x1 -= 1;
                        y1 += 1;
                        count1++;
                    }
                }
            }
            if (count1 >= 350 && count1 < 451)
            {
                if (x1 < Width)
                {
                    if (count1 == m1)
                    {
                        x1 += 1;
                        y1 += 1;
                        m1 = m1 + 5;
                        count1++;
                    }
                    else
                    {
                        x1 += 1;
                        count1++;

                    }

                }

            }
          
            Graphics g1 = e.Graphics;
            DrawStuff1(g1);
            Invalidate();
        }
        void OnPaint1(object sender, PaintEventArgs e)
        {
            if (count2 < 170)
            {
                if (x1 < Width)
                {
                    if (count2 == m2)
                    {
                       
                        y2 += 1;
                        m2 = m2 + 3;
                        count2++;
                    }
                    else
                    {

                        y2 += 1;
                        count2++;
                    }
                }
            }
            if (count2 >= 170 && count2<=328)
            {
                if (x1 < Width)
                {
                    if (count2 == m2)
                    {
                        x2 += 1;
                        y2 += 1;
                        m2 = m2 + 3;
                        count2++;
                    }
                    else
                    {
                        x2 += 2;
                        y2 += 1;
                        count2++;
                    }
                }
            }
            if (count2 >= 328  && count2 <= 430)
            {
                if (x1 < Width)
                {
                    if (count2 == m2)
                    {
                        x2 += 1;
                      
                        m2 = m2 + 3;
                        count2++;
                    }
                    else
                    {
                        x2 += 2;
                   
                        count2++;
                    }
                }
            }
         
            Graphics g2 = e.Graphics;
            DrawStuff2(g2);
            Invalidate();
        }

    }
}

Answers (3)