我試圖在Windows窗體中運行C#中的ATM模擬,它可以同時具有一個ATM機與一個銀行帳戶進行交易的多個實例。單獨線程中的多個表單
這個想法是使用信號量/鎖定來阻止可能導致競爭條件的關鍵代碼。
我的問題是這樣的:
我怎樣才能在單獨的線程同時運行兩種形式?尤其是,所有這些與已經存在的Application.Run()
如何配合?
這裏是我的主類:
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;
}
...
我已經嘗試使用
Thread ATM2 = new Thread(new ThreadStart(/*What goes in here?*/));
但是什麼方法我放在的ThreadStart構造函數,因爲ATM形式是事件驅動的,而且也沒有控制它的一種方法?
編輯:
我試着
ATM atm1 = new ATM(ac);
ATM atm2 = new ATM(ac);
Thread ATM2_T = new Thread(new ThreadStart(atm1.Show));
Thread ATM1_T = new Thread(new ThreadStart(atm2.Show));
ATM1_T.Start();
ATM2_T.Start();
本行構造
更換Application.Run(new ATM(ac));
。沒有顯示任何內容,並且程序在Main()函數的末尾丟棄。
請不要用 「C#的WinForms」 前綴的標題和這樣。這就是標籤的用途。 – 2012-03-25 04:18:11