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?? 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));
-
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));
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();
-
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));
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