N
Nurchi BECHED
Hello, my dearest respected brother, All!
I am writing an application which uses printer port to play with LEDs for
now
and something else later...
I created a button which makes LEDs "dance"...
What I need to do is, make LEDs 'Dance' as I click the button.
Stop them when I click it again.
Resume, if I click it again.
Stop when I click again.
etc...
Here is the code:
//DanceLEDThread Declaration
System.Threading.Thread DanceLEDThread=null;
//Constructor code:
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
this.DanceLEDThread=new System.Threading.Thread(
new System.Threading.ThreadStart(this.Dance_LEDs));
this.DanceLEDThread.Priority=
System.Threading.ThreadPriority.BelowNormal;
}
//This makes LEDs "dance"
//Random numbers will be used in the future...
private void Dance_LEDs()
{
int[] x=new int[]
{
24, 36, 66, 129,
0,
129, 195, 231, 255
};
while(true)
{
for (int i=0; i<x.Length; i++)
{
//InpOut32.Output() is a wrapper
//method for InpOut32 library (DLL)
//and it works fine
//this.BaseAddress is the address of the port
//x is a local variable for this method
InpOut32.Output(this.BaseAddress, x);
System.Threading.Thread.Sleep(250);
}
}
}
//bDanceLeds button click method
private void bDanceLeds_Click(object sender, System.EventArgs e)
{
if (!this.Dancing)
{
if (this.DanceLEDThread.ThreadState==
System.Threading.ThreadState.Unstarted)
this.DanceLEDThread.Start();
else if (this.DanceLEDThread.ThreadState==
System.Threading.ThreadState.Suspended)
this.DanceLEDThread.Resume();
this.bDanceLeds.Text="Stop";
//Here is a weird thing:
//The thread is "Running" the first time I click the button
//and the LEDs dance
//The second time it is "WaitSleepJoin" (see 'else' below)
//and the LEDs stop dancing
//The third time it is "WaitSleepJoin, Suspended"
//And after that, it doesn't change
//and the LEDs are still not dancing
MessageBox.Show(
this.DanceLEDThread.ThreadState.ToString());
}
else
{
//Here is a weird thing:
//The thread state turns out to be "WaitSleepJoin"
//I assume, it is because of Sleep being invoked
//within the DanceLEDs method...
MessageBox.Show(
this.DanceLEDThread.ThreadState.ToString());
this.DanceLEDThread.Suspend();
this.bDanceLeds.Text="Dance Leds";
}
//this.Dancing is a bool variable
//to check whether 'Dancing' or not
this.Dancing=!this.Dancing;
}
In addition to all, the thread keeps running even
after I exit the application.
Well, this should be because I am not terminating the thread.
I tried to use Abort() in the Closing() method of the form, but it
throws an exception.
So, what would be a good way of doing what I need to do?
With best regards, Nurchi BECHED.
I am writing an application which uses printer port to play with LEDs for
now
and something else later...
I created a button which makes LEDs "dance"...
What I need to do is, make LEDs 'Dance' as I click the button.
Stop them when I click it again.
Resume, if I click it again.
Stop when I click again.
etc...
Here is the code:
//DanceLEDThread Declaration
System.Threading.Thread DanceLEDThread=null;
//Constructor code:
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
this.DanceLEDThread=new System.Threading.Thread(
new System.Threading.ThreadStart(this.Dance_LEDs));
this.DanceLEDThread.Priority=
System.Threading.ThreadPriority.BelowNormal;
}
//This makes LEDs "dance"
//Random numbers will be used in the future...
private void Dance_LEDs()
{
int[] x=new int[]
{
24, 36, 66, 129,
0,
129, 195, 231, 255
};
while(true)
{
for (int i=0; i<x.Length; i++)
{
//InpOut32.Output() is a wrapper
//method for InpOut32 library (DLL)
//and it works fine
//this.BaseAddress is the address of the port
//x is a local variable for this method
InpOut32.Output(this.BaseAddress, x);
System.Threading.Thread.Sleep(250);
}
}
}
//bDanceLeds button click method
private void bDanceLeds_Click(object sender, System.EventArgs e)
{
if (!this.Dancing)
{
if (this.DanceLEDThread.ThreadState==
System.Threading.ThreadState.Unstarted)
this.DanceLEDThread.Start();
else if (this.DanceLEDThread.ThreadState==
System.Threading.ThreadState.Suspended)
this.DanceLEDThread.Resume();
this.bDanceLeds.Text="Stop";
//Here is a weird thing:
//The thread is "Running" the first time I click the button
//and the LEDs dance
//The second time it is "WaitSleepJoin" (see 'else' below)
//and the LEDs stop dancing
//The third time it is "WaitSleepJoin, Suspended"
//And after that, it doesn't change
//and the LEDs are still not dancing
MessageBox.Show(
this.DanceLEDThread.ThreadState.ToString());
}
else
{
//Here is a weird thing:
//The thread state turns out to be "WaitSleepJoin"
//I assume, it is because of Sleep being invoked
//within the DanceLEDs method...
MessageBox.Show(
this.DanceLEDThread.ThreadState.ToString());
this.DanceLEDThread.Suspend();
this.bDanceLeds.Text="Dance Leds";
}
//this.Dancing is a bool variable
//to check whether 'Dancing' or not
this.Dancing=!this.Dancing;
}
In addition to all, the thread keeps running even
after I exit the application.
Well, this should be because I am not terminating the thread.
I tried to use Abort() in the Closing() method of the form, but it
throws an exception.
So, what would be a good way of doing what I need to do?
With best regards, Nurchi BECHED.