I am setting the .Text value of a textbox, disabling it, and then calling a BackgroundWorker to do a lengthy filesystem operation. The textbox does not update with the new text value until about halfway through the BackgroundWorker operation.
What can I do to force the texbox to show the new text value ASAP? Relevant code below:
void BeginCacheCandidates()
{
textBox1.Text = "Indexing..."; // <-- this does not update until about 20 to 30 seconds later
textBox1.Enabled = false;
backgroundWorker1.RunWorkerAsync();
}
void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
//prime the cache
CacheCandidates(candidatesCacheFileName);
}
void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
textBox1.Text = "";
textBox1.Enabled = true;
textBox1.Focus();
}
Update: I resolved the issue. It was code unrelated to this - I had overridden WndProc and it was going into a loop...
Unless there's some detail I'm missing, wouldn't ReportProgress give you what you want?
void BeginCacheCandidates()
{
textBox1.Text = "Indexing...";
textBox1.Enabled = false;
backgroundWorker1.ReportProgress += new ProgressChangedEventHandler(handleProgress)
backgroundWorker1.RunWorkerAsync();
}
void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
//prime the cache
backgroundWorker1.ReportProgress(<some int>, <text to update>);
CacheCandidates(candidatesCacheFileName);
}
void handleProgress(object sender, ProgressChangedEventArgs e)
{
...
textBox1.Text = e.UserState as String;
...
}
Try invoking the change on the textbox instead of directly calling it.
textBox1.BeginInvoke(new MethodInvoker(() => { textBox1.Text = string.Empty; }));
This will cause the change to happen on the Form's thread.
Use Form.Update() method to force UI updates.
void BeginCacheCandidates()
{
textBox1.Text = "Indexing..."; // <-- this does not update until about 20 to 30 seconds later
textBox1.Enabled = false;
this.Update(); // Force update UI
backgroundWorker1.RunWorkerAsync();
}
Related
I can't make a thread for this method, because it has one parameter. I see same questions like this but my methods are "separated" and if I pass the variable to it the error says that the name text (the argument name) doesn't exist in this current context.
private void Form1_Load(object sender, EventArgs e)
{
Thread TH = new Thread(PressBind); //I cant make thread for this method
TH.SetApartmentState(ApartmentState.STA);
CheckForIllegalCrossThreadCalls = false;
TH.Start();
}
private void TxBxKTB_TextChanged_1(object sender, EventArgs e)
{
TextBox objTextBox = (TextBox)sender;
string text = objTextBox.Text;
label2.Text = $"the bind key is {text}";
PressBind(text);
}
void PressBind(string text)
{
Enum.TryParse(text, out Key key1);
Thread.Sleep(40);
label1.Text = "ready";
if (Keyboard.IsKeyDown(key1))
{
Thread.Sleep(40);
SendKeys.SendWait("e");
}
}
The PressBind method must match the signature of ParameterizedThreadStart, which accepts parameter of type object. Inside the PressBind method, cast this parameter to string. The value is passed to the thread in Thread.Start.
Also note, that in order to update a Label text from the other thread, you have to use Invoke.
private void Form1_Load(object sender, EventArgs e)
{
Thread TH = new Thread(PressBind); //I cant make thread for this method
TH.SetApartmentState(ApartmentState.STA);
CheckForIllegalCrossThreadCalls = false;
TH.Start("some-text" /* here you pass the text */);
}
private void TxBxKTB_TextChanged_1(object sender, EventArgs e)
{
TextBox objTextBox = (TextBox)sender;
string text = objTextBox.Text;
label2.Text = $"the bind key is {text}";
PressBind(text);
}
void PressBind(object state)
{
string text = (string)state; // cast object parameter back to string
// do other things...
// must use InvokeRequired + Invoke if accessing Label
// created by the UI thread
if (InvokeRequired)
{
Invoke(() => label1.Text = "ready");
}
else
{
label1.Text = "ready"; // we're on the UI thread
}
// do other things...
}
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;
}
I have a backgroundworker in the designer. Set the option WorkerReportsProgress and WorkerSupportsCancellations to true.
Also added all the three events of the backgroundworker.
Now i have timer1 tick event:
private void timer1_Tick(object sender, EventArgs e)
{
counter += 1;
label9.Text = counter.ToString();
label9.Visible = true;
}
I havea method called it NewsUpdate:
private void NewsUpdate()
{
if (counter == 10)
{
scrollLabel1.Reset();
scrollLabel1.Text = " ";
scrollLabel1.Invalidate();
client.Encoding = System.Text.Encoding.GetEncoding(1255);
page = client.DownloadString("http://rotter.net/scoopscache.html");
TextExtractor.ExtractDateTime(page, newText, dateTime);
StreamWriter w = new StreamWriter(#"d:\rotterhtml\rotterscoops.html");
w.Write(page);
w.Close();
TextExtractor.ExtractText(#"d:\rotterhtml\rotterscoops.html", newText, dateTime);
combindedString = string.Join(Environment.NewLine, newText);
this.scrollLabel1.Text = combindedString;
scrollLabel1.Invalidate();
counter = 0;
}
}
And all the backgroundworker events:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
NewsUpdate();
}
Progress:
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
}
Completed:
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
}
What i want to do is to call each 10 seconds to the method NewsUpdate through the backgroundworker so it will not freeze the application for 1-2 seconds each time it's making an update. But now it's calling the NewsUpdate method only once.
How do i use the backgroundworker with the NewsUpdate method ?
What should i add if at all in each of the events of the backgroundworker ?
Are there any methods or files in the NewsUpdate method that the backgroundworker will throw exception on them since it's ui or something ?
In my windows forms application I have a textbox and backgroundworker component. In dowork event of the backgroundworker I am trying to access value of the textbox. How can i do that? I'm getting following exception in dowork event handler code when I try to access value of the textbox:
Cross-thread operation not valid: Control 'txtFolderName' accessed from a thread other than the thread it was created on`
You can only access textbox / form controls in GUI thread, you can do so like that.
if(txtFolderName.InvokeRequired)
{
txtFolderName.Invoke(new MethodInvoker(delegate { name = txtFolderName.text; }));
}
try this
txtFolderName.Invoke((MethodInvoker)delegate
{
string strFolderName = txtFolderName.Text;
});
You need to use MethodInvoker. Like:
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += delegate(object sender, DoWorkEventArgs e)
{
MethodInvoker mi = delegate { txtFolderName.Text = "New Text"; };
if(this.InvokeRequired)
this.Invoke(mi);
};
You will have to invoke your TextBox on the main thread.
tb.Invoke((MethodInvoker) delegate
{
tb.Text = "Update your text";
});
Try This:
void DoWork(...)
{
YourMethod();
}
void YourMethod()
{
if(yourControl.InvokeRequired)
yourControl.Invoke((Action)(() => YourMethod()));
else
{
//Access controls
}
}
Hope This Help.
this is the another 2 methods i use.
//save the text value of txtFolderName into a local variable before run the backgroundworker.
string strFolderName;
private void btnExe_Click(object sender, EventArgs e)
{
strFolderName = txtFolderName.text;
backgroundworker.RunWorkerAsync();
}
private void backgroundworker_DoWork(object sender, DoWorkEventArgs e)
{
backgroundworkerMethod(strFolderName);//get the value from strFolderName
...
}
----------------------------------------------------
private void btnExe_Click(object sender, EventArgs e)
{
backgroundworker.RunWorkerAsync(txtFolderName.text);//pass the value into backgroundworker as parameter/argument
}
private void backgroundworker_DoWork(object sender, DoWorkEventArgs e)
{
backgroundworkerMethod(e.Argument.ToString());//get the value from event argument
...
}
i cant get the progress bar to work! if i execute the following code the bar remains empty even if the code gets executed the ReportProgress doesnt seem to update anything..:
namespace GPUZ_2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
GPUZdata test = new GPUZdata
{
};
//invio l'oggetto al thread backgroundworker
backgroundWorker1.RunWorkerAsync(test);
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
//
// e.Argument always contains whatever was sent to the background worker
// in RunWorkerAsync. We can simply cast it to its original type.
//
GPUZdata argumentTest = e.Argument as GPUZdata;
argumentTest.OneValue = 6;
Thread.Sleep(2000);
backgroundWorker1.ReportProgress(50);
argumentTest.TwoValue = 3;
Thread.Sleep(2000);
backgroundWorker1.ReportProgress(100);
//
// Now, return the values we generated in this method.
// Always use e.Result.
//
e.Result = argumentTest;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// Receive the result from DoWork, and display it.
GPUZdata test = e.Result as GPUZdata;
this.Text = test.OneValue.ToString() + " " + test.TwoValue.ToString();
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// Change the value of the ProgressBar to the BackgroundWorker progress.
progressBar1.Value = e.ProgressPercentage;
// Set the text.
this.Text = e.ProgressPercentage.ToString();
}
}
}
thanks in advance for your help
To initialize the BackgroundWorker, you must enable progress reporting and hook up your event handlers:
// Enable progress reporting
backgroundWorker1.WorkerReportsProgress = true;
// Hook up event handlers
backgroundWorker1.DoWork += backgroundWorker1_DoWork;
backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;
I don't see where you set the WorkerReportsProgress property to true - that most likely is the problem:
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.RunWorkerAsync(test);
I had the same problem. In AssemblyInfo.cs you should make this change for ComVisible.
[assembly: ComVisible(true)]