0

sqlserverのテーブルを更新しているプログラムがあり、進行状況を表示したいフォームがあります。進行状況バーが増加していますが、これは表示されていません。これにはバックグラウンドワーカーを使用する必要がありますか?私がしていることの例

public void updateTable(string tableName)
{
  // con is an instance of my form to access progressbar
  con.progressBar1.Minimum = 1;
  con.progressBar1.Step = 1;
  string dbQuery = "select summet from someting"

  con.progressBar1.Maximum = address.Tables[0].Rows.Count;
  MessageBox.Show("progress bar max " + con.progressBar1.Maximum);

   foreach (DataRow LonLat in address.Tables[0].Rows)
   {
       con.progressBar1.PerformStep();
       MessageBox.Show(con.progressBar1.Value.ToString()); // this is incrementing
       //plus updating table 

   }


}
4

4 に答える 4

2

短い答え、はい。メインスレッドから実行すると、プログレスバーの更新は表示されません。

長い答え:

「メッセージ」に変換されたコードの一部からウィンドウメッセージキューにユーザーインターフェイスを変更すると、そのメッセージが取得され、メインスレッドでそれに応じてUIが更新されます。

ただし、メインスレッドはコードの処理でビジー状態であるため、実際にユーザーインターフェイスを更新する「時間」はなく、プロセスが完了した場合にのみ、ユーザーインターフェイスを自由に更新できます。そのため、中間ステップなしで進行状況バーが0%から100%に変化します。

あなたがすべきこと:

あなたがしたいのは、バックグラウンドワーカーに作業を配置することです。そうすれば、メインスレッドはUI更新要求に自由に参加できます...ちなみに、UIの応答性を維持したい場合は、これが標準的な方法です。

于 2012-10-01T15:41:56.470 に答える
0

はい、バックグラウンドワーカーを使用する必要がありますが、役に立たない場合があります。バックグラウンドワーカーを使用するには、バインディングやその他すべてを確認する必要があります。次のようなものを使用します。

Window.Dispatcher.BeginInvoke((Action)(() =>
        {
            con.progressBar1.PerformStep();
        }
于 2012-10-01T15:43:28.987 に答える
0

That happens if we cannot use a BackgroundWorker. I have a form with a progress bar whose only job is to accept a command to PerformStep, from some other form that opens it, and the form must simply do that. Both forms are in the GUI thread. Nevertheless I don't get a consistent behavior from the progressbar. In the code below the progressbar is always one step behind than what its Value Property Signifies:

public partial class Form1 : Form
   {
      private readonly int m_numOfSteps;

      public Form1(int numOfSteps)
      {
         m_numOfSteps = numOfSteps;
         InitializeComponent();
      }

      private void Form1_Load( object sender, EventArgs e )
      {
         progressBar1.Step = (int)Math.Ceiling( 100.0 / m_numOfSteps );
      }

      public void DoStep(string msg)
      {
         progressBar1.PerformStep();
         label1.Text = msg;
         label2.Text = String.Format( "{0}%", progressBar1.Value  );
         label1.Refresh();
         label2.Refresh();
      }
   }

   public partial class Form2 : Form
   {
      public Form2()
      {
         InitializeComponent();
      }

      private void button1_Click( object sender, EventArgs e )
      {
         using( Form1 f = new Form1( 3 ) )
         {
            f.Show();
            f.Refresh();
            Thread.Sleep( 1000 );
            f.DoStep( "AAAA" );
            Thread.Sleep( 1000 );
            f.DoStep( "BBBB" );
            Thread.Sleep( 1000 );
            f.DoStep( "CCCC" );
            Thread.Sleep( 1000 );
         }
      }
   }
于 2013-02-08T07:55:12.867 に答える
0

はい、バックグラウンドワーカーを使用する必要があります

BackgroundWorker worker = new BackgroundWorker();
    worker.DoWork += (o, ea) =>
    {
     // your code goes here
       }
 worker.RunWorkerCompleted += (q, ea) =>
    {
      }
 worker.RunWorkerAsync();
于 2012-10-01T15:46:45.497 に答える