Invoke control - copy file blocks another thread - c#

I have a problem with backgroundworker. I wrote program to copying files from local disc to network disk and I want to shows progressbar spinning in circles (not shows progress) when process is running.When I set backgroundworker into empty button everything works fine, but when I set backgroundworker into button which have function to coping files progressbar shows when copying process is finished. Why and how can I solved that? Below code.
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
circularProgressBar1.BeginInvoke(new MethodInvoker(sd));
}
public void sd()
{
circularProgressBar1.Visible = true;
circularProgressBar1.Enabled = true;
circularProgressBar1.Style = ProgressBarStyle.Marquee;
}
private void backgroundWorker1_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
circularProgressBar1.Value = e.ProgressPercentage;
}
private void backgroundWorker1_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
MessageBox.Show("End");
}
private void button1_Click_1(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void copy_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
List<string> pathList = new List<string>();
// for example add 1000 path
pathList.add("C:\test\2.jpg");
pathList.add("C:\test\3.jpg");
foreach(string in in pathList)
{
File.Copy(in,"D:\test2",true);
}
}

You need to put the File.Copy in backgroundWorker1_DoWork - this is where the background work is done.
You may activate the circularProgressBar before the call to backgroundWorker1.RunWorkerAsync and disable it in backgroundWorker1_RunWorkerCompleted.

Related

C# WPF Progress bar not fully updating with background worker

I am working on a program that gets the MD5 hash signature from files that the user has selected then run it through a text file of well-known virus md5 and checks for a match. What I came to realize is that some files take a bit of time to convert. I have attempted to get a progress bar to work but have an issue where I only get it to update when it has finished and it ignores my other report progress. Any help appreciated
Here is the code for the Progress bar:
private void Border_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
{
this.Dispatcher.Invoke((Action)(() =>
{
BackgroundWorker scanner = new BackgroundWorker();
scanner.RunWorkerCompleted += scanner_RunScannerCompleted;
scanner.WorkerReportsProgress = true;
scanner.DoWork += scanner_Scan;
scanner.ProgressChanged += scanner_ProgressChanged;
scanner.RunWorkerAsync();
}));
}
private void scanner_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.Dispatcher.Invoke((Action)(() =>
{
progressBar.Value = e.ProgressPercentage;
}));
}
private void scanner_Scan(object sender, DoWorkEventArgs e)
{
this.Dispatcher.Invoke((Action)(() =>
{
var scanner = sender as BackgroundWorker;
scanner.ReportProgress(0);
scanner.ReportProgress(10);
scanner.ReportProgress(20);
getMD5(tbFilePath.Text);
scanner.ReportProgress(80);
checkSignatures();
scanner.ReportProgress(100);
}));
}
private void scanner_RunScannerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.Dispatcher.Invoke((Action)(() =>
{
MessageBox.Show("Scan Complete");
progressBar.Value = 0;
}));
}
I have already tried to add the "this.Dispatcher.Invoke" as I have already seen from other pages that this should fix the issue but hasn't for me.

How Do I Show Image Loading Progress in progress-bar c#

I want to know how to show image loading progress in progress-bar in win form.
my code is
private void Form2_Load(object sender, EventArgs e)//load form and get profile and avatar
{
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
label2.Text = "Avatar Loaded";
}
private void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)//showing progress
{
if (InvokeRequired)
{
BeginInvoke(new Action(() => backgroundWorker1_ProgressChanged(sender, e)));
}
else
{
if (progressBarX1.Value != e.ProgressPercentage)
{
progressBarX1.Value = e.ProgressPercentage;
progressBarX1.Refresh();
}
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)//loading avatar
{
WebClient wc = new WebClient();
Stream stream = wc.OpenRead("http://avatar.nimbuzz.com/getAvatar?jid=" + textBox1.Text);
pictureBox1.Image = (Image.FromStream(stream));
}
The image loads successfully but do not show any progress in progress-bar
Well, i would use the ProgressChanged Event from a WebClient to get the result of how far the download progress actually is. Then you can't save it from the thread of the backgroundworker, so you gotta do this when you have the file.
UPDATED
private BackgroundWorker bgw;
private void Form1_Load(object sender, EventArgs e)
{
bgw = new BackgroundWorker();
bgw.DoWork += bgw_DoWork;
bgw.WorkerReportsProgress = true;
bgw.RunWorkerCompleted += bgw_RunWorkerCompleted;
bgw.ProgressChanged += bgw_ProgressChanged;
bgw.RunWorkerAsync();
}
void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
pictureBox1.Image = (Image)e.Result;
}
void bgw_DoWork(object sender, DoWorkEventArgs e)
{
WebClient wc = new WebClient();
wc.DownloadProgressChanged += wc_DownloadProgressChanged;
Stream stream = wc.OpenRead("" + textBox1.Text);
e.Result = (Image.FromStream(stream));
}
void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
bgw.ReportProgress(e.ProgressPercentage);
}
The problem with your code is that backgroundWorker1_ProgressChanged() is called in the context of the worker thread but UI updates can only be done within the UI thread (main thread). In your scenario you must call:
progressBarX1.Value = e.ProgressPercentage;
within the UI thread. There are many ways how to do this but a simple one is to use InvokeReqzired() to check if you are in the UI thread and you are not then call BeginInvoke() to send the execution of that command to the UI-/main-thread. The following should do the trick:
private void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
if (InvokeRequired)
{
BeginInvoke(new Action(() => backgroundWorker1_ProgressChanged(sender, e)));
}
else
{
progressBarX1.Value = e.ProgressPercentage;
}
}
Update:
I would also change:
backgroundWorker1.RunWorkerAsync();
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
to:
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
backgroundWorker1.RunWorkerAsync();
so the progress event is assigned (and therefore in place) before starting the asynchronous work. Otherwise you may miss a progress call.
Update 2:
You also need the following line to execute in the UI thread:
pictureBox1.Image = (Image.FromStream(stream));
To do so use the Completed event of BackgroundWorker and bass the image data using the result parameter. Then in the eventhandler use InvokeRequired() and BeginInvoke() just like in the Progress event).
Uodate 3:
Remeins unchanged is good as you don't get a crash because of not calling UI stuff in the UI thread ;-)
Try to force a repaint on the control by calling:
progressBarX1.Refresh();
(just after you assigned a new value to it).
So the code in the progress event looks like this:
private void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
if (InvokeRequired)
{
BeginInvoke(new Action(() => backgroundWorker1_ProgressChanged(sender, e)));
}
else
{
if (progressBarX1.Value != e.ProgressPercentage)
{
progressBarX1.Value = e.ProgressPercentage;
progressBarX1.Refresh();
}
}
}
If that still doesn't work check that your main/UI thread is not blocked or totally busy.
Update 4:
Just to make sure: you need to enable progress-reporting on the background worker and call its ReportProgress() method from time to time within the backgroundWorker1_DoWork(). See the following MS-Tutorial for more information on usage of the BackgroundWorker: http://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx
Enabling progress reporting on a backgroundworker:
backgroundWorker1.WorkerReportsProgress = true;
or use the form-designer and set the property WorkerReportsProgress to True. Then you still need to call the backgroundWorker1.ReportProgress() from time to time.
Update 5:
Ok, lets give it a complete try. I've checked some reference docs from MS so in case of the backgroundworker ProgressChanged and Completed events you don't need to BeginInvoke() them as MS does this already for you.
private void Form2_Load(object sender, EventArgs e)
{
getto();
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.ProgressChanged -= backgroundWorker1_ProgressChanged;
backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;
backgroundWorker1.RunWorkerCompleted -= backgroundWorker1_Completed;
backgroundWorker1.RunWorkerCompleted += backgroundWorker1_Completed;
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
using (var wc = new WebClient())
{
wc.DownloadProgressChanged += (sender, e) => backgroundWorker1.ReportProgress(sender, e);
using (var stream = wc.OpenRead("http://avatar.nimbuzz.com/getAvatar?jid=" + textBox1.Text))
{
e.Result = (Image.FromStream(stream));
}
}
}
private void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
progressBarX1.Value = e.ProgressPercentage;
progressBarX1.Refresh();
}
private void backgroundWorker1_Completed(object sender, DoWorkEventArgs e)
{
pictureBox1.Image = e.Result;
}

Serial port reads only once

The following code is called several times to read data from a serial port in response to different commands sent to an attached chip & pin terminal.
However it fails to report any further responses after the 1st command.
Although there is data there, for the following commands, is not picked up or read.
How can I fix that?
I'm using ReadExisting serial port property.
private void MySerialReader()
{
ip_serialport.Open();
ip_serialport.DataReceived += serialPort_DataReceived;
//ip_serialport.Close(); //This causes the problems
}
public string RxString;
private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
RxString = ip_serialport.ReadExisting();
this.Invoke(new EventHandler(DisplayText_RX)); //<<-- ????!!!
}
private void DisplayText_RX(object sender, EventArgs e)
{
RX_Box.Items.Add(RxString);
}
I think if you use timer then you can read remaining data
private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
RxString = ip_serialport.ReadExisting();
this.Invoke(new EventHandler(DisplayText_RX));
}
private void DisplayText_RX(object sender, EventArgs e)
{
RX_Box.Items.Add(RxString);
if(RxString!="")
timer.start();
}
void timer_Tick(object sender, EventArgs e)
{
if(serialport.isopen)//check for open
{
RxString = ip_serialport.ReadExisting();
this.Invoke(new EventHandler(DisplayText_RX));
}
timer.stop();
}

WPF MediaElement how to resume playing from where it pause?

i have 2 buttons , Play and Pause , When i click pause , the music stops and when i click Play it starts the audio from beginning . I want to do it like when i press Play , it resume from where i have stopped.
private void PlayAudio()
{
McMediaElement.LoadedBehavior = MediaState.Manual;
McMediaElement.Source = new Uri("../../SingAlong/Food Fit For A King/old king cole.mp3", UriKind.RelativeOrAbsolute);
McMediaElement.Play();
}
private void button1_Click_1(object sender, RoutedEventArgs e)
{
PlayAudio();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
McMediaElement.Pause();
}
Your PlayAudio() method reloads the media file when you set the Source property. This causes your object to play the newly loaded media from the beginning when you call Play(). Instead of doing this in the event handler button1_Click_1, you should call the Play() method only:
private void button1_Click_1(object sender, RoutedEventArgs e)
{
McMediaElement.Play();
}
This worked for me..
private void button1_Click_1(object sender, RoutedEventArgs e)
{
McMediaElement.LoadedBehavior = MediaState.Pause;
}
private void button2_Click_1(object sender, RoutedEventArgs e)
{
McMediaElement.LoadedBehavior = MediaState.Play;
}

How to wait two BackgrandWorker's RunWorkerCompleted event finished?

That's the code:
private void button1_Click(object sender, EventArgs e)
{
ParaClass pcs = new ParaClass();
pcs.strPath = textBox1.Text;
pcs.sendedGrid = ugSrc;
this.backgroundWorker1.RunWorkerAsync(pcs);
ParaClass pcsB = new ParaClass();
pcsB.strPath = textBox2.Text;
pcsB.sendedGrid = ultraGrid2;
this.backgroundWorker2.RunWorkerAsync(pcsB);
doSomething();
}
and in both backgrandworker1 & backgrandworker2 ' complet event ,i write code like this:
private void backgroundWorker1_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
{
doSomethingelsebk1();
}
private void backgroundWorker2_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
{
doSomethingelsebk2();
}
now the problem is : the function doSomething() in button1's click event must wait both backgrandworker's complete event finish.
if i change doSomething() to
private void backgroundWorker2_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
{
doSomethingelsebk2();
doSomething();
}
then,because there are two thread,i don't know which thread will finish first,so what is the solution
Create 2 flags which represent complete state of 2 BackgroundWorker.
Turn each flag on in the RunWorkerCompleted event, then call doSomething() method.
In doSomething method, check if both flags is on, then continue to do, otherwise, return.
Create 2 AutoResetEvents, set them when each Background worker finishes and wait for them all in the main method with a WaitHandle.
WaitHandle[] handles = new WaitHandle[] { new AutoResetEvent(false), new AutoResetEvent(false)};
private void button1_Click(object sender, EventArgs e)
{
ParaClass pcs = new ParaClass();
pcs.strPath = textBox1.Text;
pcs.sendedGrid = ugSrc;
this.backgroundWorker1.RunWorkerAsync(pcs);
ParaClass pcsB = new ParaClass();
pcsB.strPath = textBox2.Text;
pcsB.sendedGrid = ultraGrid2;
this.backgroundWorker2.RunWorkerAsync(pcsB);
WaitHandle.WaitAll(this.handles);
doSomething();
}
private void backgroundWorker1_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
{
doSomethingelsebk1();
((AutoResetEvent)this.handles[0]).Set();
}
private void backgroundWorker2_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
{
doSomethingelsebk2();
((AutoResetEvent)this.handles[1]).Set();
}

Categories

Resources