C# WinForms. Multiple Forms in separate threads
        Posted  
        
            by 
                Calum Murray
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Calum Murray
        
        
        
        Published on 2012-03-24T23:27:21Z
        Indexed on 
            2012/03/24
            23:29 UTC
        
        
        Read the original article
        Hit count: 442
        
I'm trying to run an ATM Simulation in C# with Windows Forms that can have more than one instance of an ATM machine transacting with a bank account simultaneously.
The idea is to use semaphores/locking to block critical code that may lead to race conditions.
My question is this:
How can I run two Forms simultaneously on separate threads?  In particular, how does all of this fit in with the Application.Run() that's already there?
Here's my main class:
public class Bank
    {
        private Account[] ac = new Account[3];
        private ATM atm;
        public Bank()
        {
            ac[0] = new Account(300, 1111, 111111);
            ac[1] = new Account(750, 2222, 222222);
            ac[2] = new Account(3000, 3333, 333333);
            Application.Run(new ATM(ac));
        }
        static void Main(string[] args)
        {
            new Bank();
        }
    }
...that I want to run two of these forms on separate threads...
 public partial class ATM : Form
{
    //local reference to the array of accounts
    private Account[] ac;
    //this is a reference to the account that is being used
    private Account activeAccount = null;
    private static int stepCount = 0;
    private string buffer = "";
    // the ATM constructor takes an array of account objects as a reference
    public ATM(Account[] ac)
    {
        InitializeComponent();  //Sets up Form ATM GUI in ATM.Designer.cs
        this.ac = ac;
    }
...
I've tried using
Thread ATM2 = new Thread(new ThreadStart(/*What goes in here?*/));
But what method do I put in the ThreadStart constructor, since the ATM form is event-driven and there's no one method controlling it?
Thanks, Calum
© Stack Overflow or respective owner