C # line execution error

I transfer files and I want the progress bar to display the actual progress of each file. This works fine for files under the age of 15 megabytes, but the files are larger than it seems, causing my application to freeze. Unless I name this code for the progress bar, these large files are transmitted just fine.

I tried all sorts of ways to handle this with delegates, but no luck. Rather, they work fine with smaller files, but not larger ones.

Some examples that worked ...

pbFileProgress.Invoke((MethodInvoker) delegate { pbFileProgress.Value = args.PercentDone; }); 

In addition, this set of methods worked for small files.

 private delegate void SetProgressBarCallback(int percentDone); public void UpdateProgressBar(object send, UploadProgressArgs args) { if (pbFileProgress.InvokeRequired) { var d = new SetProgressBarCallback(ProgressBarUpdate); BeginInvoke(d, new object[] { args.PercentDone }); } else { ProgressBarUpdate(args.PercentDone); } } public void ProgressBarUpdate(int percentDone) { pbFileProgress.Value = percentDone; } 

But then again, everything just freezes if I try to enlarge the files.

+4
source share
3 answers

Despite the lack of context, here is an example that works. The BeginInvoke or Invoke method is called only 100 times.

 Task.Factory.StartNew(() => { using (var source = File.OpenRead(@"D:\Temp\bbe.wav")) using (var destination = File.Create(@"D:\Temp\Copy.wav")) { var blockUnit = source.Length / 100; var total = 0L; var lastValue = 0; var buffer = new byte[4096]; int count; while ((count = source.Read(buffer, 0, buffer.Length)) > 0) { destination.Write(buffer, 0, count); total += count; if (blockUnit > 0 && total / blockUnit > lastValue) { this.BeginInvoke( new Action<int>(value => this.progressBar1.Value = value), lastValue = (int)(total / blockUnit)); } } this.BeginInvoke( new Action<int>(value => this.progressBar1.Value = value), 100); } }); 
+2
source

This problem is very common when communicating between background and front threads: the background thread sends too many updated threads.

The front end handles updating, drawing, and user input, so when too many updates appear, the user interface freezes, trying to catch up.
Obviously, if the background thread continues to send updates, the front end can be copied even after the background task completes!

There are several solutions to this problem, but my strongest recommendation is to use Timer in the foreground thread to poll background progress and update the user interface.
Benefit of using Timer :

  • Background thread can report progress as often as possible
  • The front end can just relax until an update is needed.
  • The front end will not back up updates.
  • If the foreground thread is at rest, then the background thread gets more processor time.
  • Timer frequency can be set to a "reasonable" value, for example 250 ms (4 updates per second), so that the progress will be smooth, but it will not take the entire processor

As always, thread safety is important when transferring progress between threads. Using a simple 32-bit int value in this scenario is thread safe, but using a 64-bit double not safe on 32-bit machines.

+1
source

You can invoke based on a user interface element. For instance:

 private delegate void InvokeUpdateProgressBar(object send, UploadProgressArgs args); private int _PercentDone = -1; public void UpdateProgressBar(object send, UploadProgressArgs args) { if(_PercentDone != args.PercentDone) { if (pbFileProgress.InvokeRequired) { pbFileProgress.Invoke( new InvokeUpdateProgressBar(UpdateProgressBar), new object[] { send, args }); } else { ProgressBarUpdate(args.PercentDone); } _PercentDone = args.PercentDone; } } 

Otherwise, I would suggest what Aaron Maciver did and used the BackgroundWorker class. See the example here for details on updating the progress bar using the BackgroundWorker class.

Update It seems you are not the only one in this matter. See Amazon s3 Transferutility.Upload in C # here. Kent also points out: If you read in about the S3 forums you'll find many people having similar issues

0
source

Source: https://habr.com/ru/post/1384996/


All Articles