0

I'm searching for a concept on howto realize the following: I have 4 devices connected via RS232 to a PC where I want to create some control application. I have already created classes for the 4 different devices that handle the communication and error handling.

In the main thread I now want to create some initialization sequence. For example one control parameter is temperature. So I set the temperature and then need to wait up to 10 minutes until the temperature is reached. When it is reached I want to set the output voltage of a power supply and so on. There are parameters that I need to control in parallel (while waiting for the temperature to get into a acceptable window I also need to monitor that the pressure is within some range).

So I am dreaming of something like this, but have no clue on howto best realize it:

Device1.setTemp(200); magic.Wait(function to get current value, object containing min/max levels, max time);

For the simple case. But I think once I know in which direction to go, I can extend it to allow monitoring of multiple values.

Please give me some into a good direction. I already looked into threads, BackgroudWorkers and so on. There are many different options available, but I have no clue on howto create it in a safe way from the beginning on.

Thank you, thefloe

4

1 に答える 1

1

EventWaitHandles を使用して、スレッド内の操作を同期できます。詳細はこちらをご覧ください

static EventWaitHandle _go = new AutoResetEvent(false);

static void Main(string[] args)
{
    //Set the temperature 
    int Temp = 100;
    int Voltage = 240;

    Thread t1 = new Thread(() => SetTemperature(Temp) );
    Thread t2 = new Thread(() => SetPowerVoltage(Votage));
    Thread t3 = new Thread(() => SomeOtherImportantWork()); //Which can go on in parallel
    t1.Start();
    t2.Start();
    t3.Start();
    ....
    ....

    //you can wait or do other operations
}

//Logic to set the temperature
static void SetTemperature(int temperature)
{
    Console.WriteLine("Setting temperature");

    //Do your stuff     

    //Signal your other thread
    _go.Set();

}


static void SetPowerVoltage(int voltage)        
{
    //Wait for the temperature to be set
    Console.WriteLine ("Waiting...");
    _go.WaitOne();


    //This will begin execution once the temp thread signals
    //Do your stuff to set voltage

}

static void SomeOtherImportantWork()         
{
     //Do Important work
}
于 2013-06-02T18:17:59.160 に答える