2017-04-05 24 views
0

をマルチスレッド使用して、私はその後、私は2つのWindowsフォームアプリケーションがありproduct_listテーブルでリアルタイム更新

+----+-------------+-------+-------+ 
| id | description | price | Stock | 
+----+-------------+-------+-------+ 
| 1 | Item 1  | 1.50 | 5 | 
+----+-------------+-------+-------+ 

をデータベースを持っています。各アプリケーションは別々のプロジェクトにあります。

  1. 最初のアプリケーションは、INSERT INTO product_list (description, price, stock) VALUES ('Item 2', 2.00, 10)というクエリを持つテーブルにデータを挿入することです。
  2. 第2のアプリケーションは、datagridviewを使用してリアルタイムでテーブルを表示することです。 私はデータベース用のオブジェクトを持っており、DataTableを返す関数名GetTable()を持っています。

以下は、DataTableを返すデータベースオブジェクトのコードです。

ここ
 public DataTable GetTable() 
    { 
     DataTable table = new DataTable(); 

     try 
     { 
      MySqlDataAdapter daTable = new MySqlDataAdapter(this.query, this.conn); 
      daTable.Fill(table); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.ToString()); 
     } 

     return table; 
    } 

私は、Visual Studioは、InvalidOperationExceptionがを与えて実行するマルチスレッド

DataTable inventoryTable; 
    inventoryDB inventoryDB; 

    Thread updateDataGridView; 

    public Form1() 
    { 
     InitializeComponent(); 
     inventoryDB = new inventoryDB(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     inventoryDB.SetQuery("SELECT id AS ID, description as Description, price as Price, stock as Stock FROM `product_list"); 
     inventoryTable = inventoryDB.GetTable(); 

     this.dataGridView1.DataSource = inventoryTable; 

     this.updateDataGridView = new Thread(new ThreadStart(this.RealTimeData)); 
     this.updateDataGridView.Start(); 
    } 

    private void RealTimeData() { 
     while (true) { 
      testTable = inventoryDB.GetTable(); 

      this.dataGridView1.DataSource = testTable; 
      this.dataGridView1.Update(); 
      this.dataGridView1.Refresh(); 
     } 
    } 

ためのコード。

+0

その制御を呼び出そうとした場合にコントロールを作成したもの以外のスレッド、デバッガは、メッセージと例外InvalidOperationExceptionを上昇させる「とは、上で作成されたスレッド以外のスレッドからアクセスコントロール名を制御します。」あなたはそれを直接行うことはできません、私はObjectDisposedExceptionを与えるフォームを閉じるときにコントロール – Vandita

答えて

1

コントロールのInvokeRequiredプロパティを使用できます。このコードを試してください。

DataTable inventoryTable; 
inventoryDB inventoryDB; 

Thread updateDataGridView; 

public Form1() 
{ 
    InitializeComponent(); 
    inventoryDB = new inventoryDB(); 
} 

private void Form1_Load(object sender, EventArgs e) 
{ 
    inventoryDB.SetQuery("SELECT id AS ID, description as Description, price as Price, stock as Stock FROM `product_list"); 
    inventoryTable = inventoryDB.GetTable(); 

    this.dataGridView1.DataSource = inventoryTable; 

    this.updateDataGridView = new Thread(new ThreadStart(this.RealTimeData)); 
    this.updateDataGridView.Start(); 
} 

private void RealTimeData() { 
    testTable = inventoryDB.GetTable(); 
    this.SetDataSourceInGridView(testTable); 
} 

// This delegate enables asynchronous calls for setting 
// the datasource property on a GridView control. 
delegate void GridViewArgReturningVoidDelegate(DataTable testTable); 

private void SetDataSourceInGridView(DataTable testTable) 
{ 
    // InvokeRequired required compares the thread ID of the 
    // calling thread to the thread ID of the creating thread. 
    // If these threads are different, it returns true. 
    if (this.dataGridView1.InvokeRequired) 
    {  
     GridViewArgReturningVoidDelegate d = new StringArgReturningVoidDelegate(SetDataSourceInGridView); 
     this.Invoke(d, new object[] { testTable }); 
    } 
    else 
    { 
     this.dataGridView1.DataSource = testTable; 
     this.dataGridView1.Update(); 
     this.dataGridView1.Refresh(); 
    } 
} 
+0

のInvokeRequiredプロパティを使用する必要があります。 –

+0

@Ardianこれを参照することができますhttp://stackoverflow.com/questions/19619966/exception-when-closing-form-thread-invoke – Vandita

+0

非常に感謝@ヴァンディタ:)乾杯 –

関連する問題