Click here to Skip to main content
15,887,596 members
Home / Discussions / C#
   

C#

 
AnswerRe: Solution Explorer to be shown on the toolbar Pin
Dave Kreskowiak7-May-13 7:41
mveDave Kreskowiak7-May-13 7:41 
GeneralRe: Solution Explorer to be shown on the toolbar Pin
T Pat7-May-13 20:00
T Pat7-May-13 20:00 
AnswerRe: Solution Explorer to be shown on the toolbar Pin
Pete O'Hanlon7-May-13 8:16
mvePete O'Hanlon7-May-13 8:16 
AnswerRe: Solution Explorer to be shown on the toolbar Pin
Anna King8-May-13 3:07
professionalAnna King8-May-13 3:07 
GeneralRe: Solution Explorer to be shown on the toolbar Pin
Pete O'Hanlon8-May-13 3:47
mvePete O'Hanlon8-May-13 3:47 
QuestionBackground worker with progress bar timing issue Pin
Blubbo7-May-13 5:02
Blubbo7-May-13 5:02 
AnswerRe: Background worker with progress bar timing issue Pin
Eddy Vluggen7-May-13 9:28
professionalEddy Vluggen7-May-13 9:28 
AnswerRe: Background worker with progress bar timing issue Pin
Matt T Heffron7-May-13 11:31
professionalMatt T Heffron7-May-13 11:31 
(This looks like Windows Forms vs. WPF.)
In the ...DoWorkn() method you create a local ProgressBar that is passed to the Write_Tag and Read_Tag methods.
There's not a tight linkage between that ProgressBar and the one that ReportProgress() is updating, so the one that is actually visible is not updated very often.

If the Write_Tag and Read_Tag methods aren't careful to deal with possible cross-thread updating of the ProgressBar passed to them, then you're kind of out-of-luck. It doesn't appear that there's an event on the ProgressBar that could be used to redirect the updates to the .Value to use the worker.ReportProgress(..).

If the Write_Tag and Read_Tag methods are dealing with the cross-thread updating correctly, then you can pass the visible ProgressBar into the BackgroundWorker with the argument to the RunWorkerAsync(progressBar[n]) and use the ReportProgress() when updating directly in the worker, and pass it into the Write_Tag and Read_Tag methods, counting on them to "do the right thing" when they update it. Something like:
C#
private void InitializeProgressBars()
{
  for (int i = 0; i < DevicePath.Count; i++)
  {
    progressBar[i] = new ProgressBar();
    progressBar[i].Location = new Point(3, 16);
    progressBar[i].Size = new Size(605, 31);
    progressBar[i].Maximum = 100;
  }
 
  gbProgressStatus0.Controls.Add(progressBar[0]);
  gbProgressStatus1.Controls.Add(progressBar[1]);
  gbProgressStatus2.Controls.Add(progressBar[2]);
  gbProgressStatus3.Controls.Add(progressBar[3]);
}
 
private void InitializeBackgroundWorkers()
{
  for (var f = 0; f < DevicePath.Count; f++)
  {
    bw[f] = new BackgroundWorker();
    bw[f].WorkerReportsProgress = true;
    bw[f].WorkerSupportsCancellation = true;
 
    switch (f)
    {
      case 0:
      {
        bw[f].DoWork += new DoWorkEventHandler(BackgroundWorkerFilesDoWork0);
        bw[f].RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundWorkerFilesRunWorkerCompleted0);
        break;
      }
      case 1:
      {
        bw[f].DoWork += new DoWorkEventHandler(BackgroundWorkerFilesDoWork1);
        bw[f].RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundWorkerFilesRunWorkerCompleted1);
        break;
      }
      case 2:
      {
        bw[f].DoWork += new DoWorkEventHandler(BackgroundWorkerFilesDoWork2);
        bw[f].RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundWorkerFilesRunWorkerCompleted2);
        break;
      }
      case 3:
      {
        bw[f].DoWork += new DoWorkEventHandler(BackgroundWorkerFilesDoWork3);
        bw[f].RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundWorkerFilesRunWorkerCompleted3);
        break;
      }
    }
    bw[f].ProgressChanged += (sender, e) => { ((ProgressBar)e.Argument).Value = e.ProgressPercentage; };
  }
}
 
private void BackgroundWorkerFilesDoWork0(object sender, DoWorkEventArgs e)
{
  BackgroundWorker worker = sender as BackgroundWorker;
  worker.ReportProgress(0);
  ProgressBar pb = (ProgressBar)e.Argument;

  System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
  sw.Start();
  // counts on pb being updated safely for threading issues.
  int status = tag_device0.Write_Tag(0, writeDat, en_statusDat, pb);
 
  if (ReadTag)
  {
    worker.ReportProgress(0);
    byte[] tagData = new byte[1024];
    tag5_device0.Read_Tag(0, 0, tagData .Length, ref tagData , pb);
  }
 
  sw.Stop();
  string result = string.Format("Status: {0}    Elapsed: {1}", status, sw.Elapsed.ToString());
  e.Result = result;
}

// Then when the BackgroundWorkers are started:
bw[n].RunWorkerAsync(progressBar[n]);


Another design option, if you can change the Write_Tag and Read_Tag methods, would be to have them take an Action<int> delegate that deals with updating progress, instead of the ProgressBar. In this case, it would call worker.ReportProgress().
GeneralRe: Background worker with progress bar timing issue Pin
Blubbo8-May-13 8:18
Blubbo8-May-13 8:18 
SuggestionRe: Background worker with progress bar timing issue Pin
Matt T Heffron8-May-13 8:50
professionalMatt T Heffron8-May-13 8:50 
GeneralRe: Background worker with progress bar timing issue Pin
Blubbo8-May-13 9:11
Blubbo8-May-13 9:11 
Questionnetwork programming Pin
Member 100297487-May-13 4:53
Member 100297487-May-13 4:53 
AnswerRe: network programming Pin
Richard MacCutchan7-May-13 4:59
mveRichard MacCutchan7-May-13 4:59 
GeneralRe: network programming Pin
Ennis Ray Lynch, Jr.7-May-13 7:33
Ennis Ray Lynch, Jr.7-May-13 7:33 
GeneralRe: network programming Pin
Richard MacCutchan7-May-13 20:34
mveRichard MacCutchan7-May-13 20:34 
AnswerRe: network programming Pin
Anna King8-May-13 3:19
professionalAnna King8-May-13 3:19 
QuestionEthernet Port C# Pin
Member 100344047-May-13 2:28
Member 100344047-May-13 2:28 
AnswerRe: Ethernet Port C# Pin
Richard MacCutchan7-May-13 2:51
mveRichard MacCutchan7-May-13 2:51 
QuestionSaving objects with Cross-reference Pin
larsp7777-May-13 1:35
larsp7777-May-13 1:35 
AnswerRe: Saving objects with Cross-reference Pin
Eddy Vluggen7-May-13 9:22
professionalEddy Vluggen7-May-13 9:22 
GeneralRe: Saving objects with Cross-reference Pin
larsp7777-May-13 12:14
larsp7777-May-13 12:14 
GeneralRe: Saving objects with Cross-reference Pin
Bernhard Hiller7-May-13 22:56
Bernhard Hiller7-May-13 22:56 
GeneralRe: Saving objects with Cross-reference Pin
larsp7778-May-13 2:24
larsp7778-May-13 2:24 
GeneralRe: Saving objects with Cross-reference Pin
Eddy Vluggen8-May-13 6:53
professionalEddy Vluggen8-May-13 6:53 
GeneralRe: Saving objects with Cross-reference Pin
larsp7779-May-13 20:19
larsp7779-May-13 20:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.