Main Site Documentation

Help with Threads


#1

I need help again. I have a guy at work who is telling everyone that we will not beat his Halo Reach score before he gets 2,000,000 points. I decided to use a Panda II to prove him wrong. The problem that I am having is mscorlib.dll exceptions when I try to debug the program.

The concept is simple:

  1. I am starting the game by pressing the ‘A’ button repeatedly for a preset amount of time.

  2. After the game starts I (The Panda II) will be pressing the ‘B’ button repeatedly for a preset amount of time. This makes the console think that the user is not away and also serves to bring the Game back to the main menu so the game can be started again.

I would greatly appreciate someone pointing out my errors and giving me any guidance.

Thanks,

Kevin


using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;

namespace XBox
{
    public class Program
    {
        static bool bButtonState = false;
        static bool aButtonState = false;
        static bool threadTracker = false;
        static TimeSpan atimespan = new TimeSpan(0, 0, 0, 10);
        static TimeSpan btimespan = new TimeSpan(0, 0, 0, 15);
        static OutputPort aButtonGND = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di11, false);
        static OutputPort aButtonOut = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di10, !aButtonState);
        static OutputPort bButtonGND = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di13, false);
        static OutputPort bButtonOut = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di12, !bButtonState);

        public static void Main()
        {
            Thread aButtonThread = new Thread(aButtonTimer);
            Thread bButtonThread = new Thread(bButtonTimer);
            while (true)
            {
                if (threadTracker == false)
                {
                    bButtonThread.Abort();
                    if (aButtonThread.IsAlive == true)
                    {
                    }
                    else
                    {
                        aButtonThread.Start();
                    }
                }

                if (threadTracker == true)
                {
                    aButtonThread.Abort();
                    if (bButtonThread.IsAlive == true)
                    {
                    }
                    else
                    {
                        bButtonThread.Start();
                    }
                }
            }
        }

        public static void aButtonTimer()
        {
            DateTime now = DateTime.Now;
            DateTime FinishTime = now.Add(atimespan);
            do
            {
                Timer aTimer = new Timer(new TimerCallback(aButton), null, 500, 3000);
            }
            while (DateTime.Now < FinishTime);
            threadTracker = true;

        }
        public static void bButtonTimer()
        {
            DateTime now = DateTime.Now;
            DateTime FinishTime = now.Add(btimespan);
            do
            {
                Timer bTimer = new Timer(new TimerCallback(bButton), null, 500, 3000);
            }
            while (DateTime.Now < FinishTime);
            threadTracker = false;
        }
        public static void aButton(object o)
        {
            aButtonState = !aButtonState;
        }
        public static void bButton(object o)
        {
            bButtonState = !bButtonState;
        }
    }
}



#2

Does the firmware on your Panda II match up with the SDK? I had lots of errors at first with my projects and it was all because of DLL mismatches.


#3

Why bother aborting a thread you know is exiting?

Also why even have threads? If this is all that’s going on why not just handle it in a couple of whiles without bother for new threads?


#4

Good point. the only reason I am using threads is to learn how to handle them properly. How do I know that the thread is exiting? Not really sure…


#5

Like any other method when it reaches the end it exists. So as soon as your while statement ends it reaches the end of the method and the thread stops.