Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Q. How do you start 2 or more other classes to threading from a single Form1??

Q. How do you start 2 or more other classes to threading from a single Form1??

Scheduled Pinned Locked Moved C#
question
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gman44
    wrote on last edited by
    #1

    Q. How do you start 2 or more other classes to threading from a single Form1?? I tried this code on Form1 (Form1_Load) but if failed... objClass1 = new Class1(); objClass1 = new Thread(new ThreadStart(StartMoveKing)); // also objClass2 = new Class2(); objClass2 = Thread(new ThreadStart(StartMoveQueen));

    K L 2 Replies Last reply
    0
    • G gman44

      Q. How do you start 2 or more other classes to threading from a single Form1?? I tried this code on Form1 (Form1_Load) but if failed... objClass1 = new Class1(); objClass1 = new Thread(new ThreadStart(StartMoveKing)); // also objClass2 = new Class2(); objClass2 = Thread(new ThreadStart(StartMoveQueen));

      K Offline
      K Offline
      kayhustle
      wrote on last edited by
      #2

      Thats because you have to start them, you just constructed them. objClass1 = new Class1(); objClass1 = new Thread(new ThreadStart(StartMoveKing)); objClass1.Start(); // also objClass2 = new Class2(); objClass2 = new Thread(new ThreadStart(StartMoveQueen)); objClass2.Start();

      1 Reply Last reply
      0
      • G gman44

        Q. How do you start 2 or more other classes to threading from a single Form1?? I tried this code on Form1 (Form1_Load) but if failed... objClass1 = new Class1(); objClass1 = new Thread(new ThreadStart(StartMoveKing)); // also objClass2 = new Class2(); objClass2 = Thread(new ThreadStart(StartMoveQueen));

        L Offline
        L Offline
        LongRange Shooter
        wrote on last edited by
        #3

        You have to be careful of what you are doing though. If those threads have the end purpose of sending new information to a Windows Form you will have very unpredictable results. You either have to ensure that your threads are within the message pump apartment or you have to alter memory in your program and then setup another thread to monitor changes. The Windows.Forms.Timer will do that. This is a rough example of a skeleton of a program that would accomplish something like that. Example:

        public class Chess()
        {
        ...
        public void MoveKing(chessBoard);
        {
        do {
        Stack[] moveStacks = new Stack[10];
        // construct move stacks analyze weigh move
        ....
        // reached decision to end myself
        if (moveMade)
        {
        move = "K-KR3";
        this.Threading.Thread.Stop();
        }
        ...
        } while (threadExecutionRequired);
        // now determine which was best move
        // when my thinking was interuptted
        move = "RESIGN";
        }
        public Chess()
        {
        IntializeComponents();
        threadExecutionRequired=false;
        }
        public void PlayChess()
        {
        ....
        }
        private void MakeMyMove()
        {
        Thread moveThread = new Thread(MoveKing);
        moveThread.Start();
        this.formTimer.Start();
        }
        private formTimer_Tick(...)
        {
        if (ICantWaitAnyLonger)
        {
        threadExecutionRequired=false;
        }
        if (move != null) // a move was announced
        {
        //update form within apartment thread
        MakeMyMove(move);
        DisplayBoard(chessBoard);
        formTimer.Stop();
        }
        }
        }

        This signature left intentionally blank

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups