I have used Backgroundworkerclass to update my user interface element (Label). Please! Check my code below. It is working fine. But I am trying to replace Backgroundworkerclass and use simple worker thread to achieve the same goal. How can I achieve that? Any suggestion will be appreciated. Thanks
public void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
label1.Text = e.ProgressPercentage.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
if (!backgroundWorker1.IsBusy)
backgroundWorker1.RunWorkerAsync();
else
label1.Text = "Busy Processing";
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i <= 100; i++)
{
Thread.Sleep(100);
backgroundWorker1.ReportProgress(i);
if (backgroundWorker1.CancellationPending)
{
e.Cancel = true;
backgroundWorker1.ReportProgress(0);
return;
}
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
label1.Text = e.ProgressPercentage.ToString();
}
delegate void DelegateDoWork(int number);
private void button1_Click(object sender, EventArgs e)
{
new Thread(DoWork).Start();
}
public void ProgressBar(int i)
{
if (label1.InvokeRequired)
{
var d = new DelegateDoWork(ProgressBar);
this.Invoke(d, i);
}
else
label1.Text = i.ToString();
}
public void DoWork()
{
for (int i = 0; i <= 100; i++)
{
Thread.Sleep(100);
ProgressBar(i);
}
}
Related
I have problem, which consists in aesthetic sense, correctly - There is textBox to which i apply true condition of UseSystemPasswordChar.. It's work! But i get bold points. Try to change font size - decreases textbox's field. Below is the code (although why is it here?). Can anyone help, thank you in advance)
public partial class frmRegistr : Form
{
public frmRegistr()
{
InitializeComponent();
}
int counter = 0;
int a = 0;
string b;
private void frmRegistr_Load(object sender, EventArgs e)
{
b = label1.Text;
a = b.Length;
label1.Text = "";
timer1.Start();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
if (counter < a)
{
counter++;
label1.Text = b.Substring(0, counter);
}
else
{
timer1.Stop();
}
}
private void label4_Click(object sender, EventArgs e)
{
timer3.Start();
}
private void label4_MouseHover(object sender, EventArgs e)
{
//if this.MouseLeave
label4.BackColor = Color.FromArgb(((int)(((byte)(154)))), ((int)(((byte)(181)))), ((int)(((byte)(101)))));
}
private void timer2_Tick(object sender, EventArgs e)
{
if (Opacity == 1)
{
timer2.Stop();
}
Opacity += .2;
}
private void timer3_Tick(object sender, EventArgs e)
{
if (Opacity <= 0)
{
this.Close();
}
Opacity -= .2;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
textBox2.UseSystemPasswordChar = true;
}
}
}
If you want to define your own password character, use property TextBox.PasswordChar. If you want this in a certain font, use Control.Font
As you only have to do this once, do this in the constructor:
public MyForm : Form
{
InitializeComponents(),
this.textBox1.PasswordChar = '_';
this.textBox11.Font = new Font(...)
};
You can also decide to do this using the visual studio designer.
You can setup this in VisualStudio designer, but this is code:
textBox1.PasswordChar = '*';
//* = password character
I have a function that displays numbers using a while loop but I want to stop execution of a while loop at random variable value using c# by clicking a button.
For Example:
private void FrqSent()
{
int i = 1;
while (i <= 5)
{
i = i + 1;
}
}
Here is a quick example on how you can use a Backgroundworker to accomplish your task:
public partial class Form1 : Form
{
private int i = 1;
public Form1()
{
InitializeComponent();
}
private void FrqSent()
{
while (i <= 500000000)
{
if (backgroundWorker1.CancellationPending)
{
break;
}
else
{
i = i + 1;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void button2_Click(object sender, EventArgs e)
{
backgroundWorker1.CancelAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
FrqSent();
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show(i.ToString());
}
}
Just create a new Windows-Forms Project and add a backgroundworker object aswell as 2 buttons. You have to set the DoWork, RunWorkerCompleted and Click events manually.
Edit: Do not forget to set the BackgroundWorker`s WorkerSupportsCancellation property to true.
not very elegant but simple
public partial class Form1 : Form
{
private bool _stop;
public Form1()
{
InitializeComponent();
}
private void buttonStart_Click(object sender, EventArgs e)
{
_stop = false;
FrqSent();
}
private void buttonStop_Click(object sender, EventArgs e)
{
_stop = true;
}
private void FrqSent()
{
int i = 1;
while (i <= 5000000 && !_stop)
{
i = i + 1;
Application.DoEvents();
}
}
}
My problem is that I want the form still display data when it increases but the form is blocked and I cannot do anything with it.
This is my code :
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
while (true)
for (int i = 1; i < 11; i++)
richTextBox1.Text += "here" + i + "/n";
}
}
}
How I can prevent form from blocking?
private void Form1_Load(object sender, EventArgs e)
{
BackgroundWorker BWorker = new BackgroundWorker();
BWorker.DoWork += BWorker_DoWork;
BWorker.RunWorkerAsync();
}
void BWorker_DoWork(object sender, DoWorkEventArgs e)
{
// while(true) is meaningless.
for (int i = 1; i < 11; i++)
{
Action UpdateUI = () => { richTextBox1.Text += "here" + i + "/n"; };
this.BeginInvoke(UpdateUI);
}
}
Split your working cycle into steps by utilizing timer
private int _workStep;
private void button1_Click(object sender, EventArgs e)
{
_workStep = 0;
timerWork.Start();
}
private void timerWork_Tick(...)
{
switch(workStep)
{
case 0:
... // do something
if(something)
_workStep = 1;
case laststep:
timerWork.Stop();
}
}
or put work into thread (by using Thread, BackgroundWorker or Task), but then you must use Invoke/BeginInvoke when updating something in the user interface (accessing controls).
You can do this to get a responsive ui
delegate void DisplayInvoker(string text);
private void DisplayinRichTextbox(string text)
{
if (this.InvokeRequired)
this.BeginInvoke(new DisplayInvoker(DisplayinRichTextbox), text);
return;
}
richTextBox1.Text += text;
}
private void button1_Click(object sender, EventArgs e)
{
// some synchronization would have to be done kill old
// pool threads when the button is hit again
//
ThreadPool.QueueUserWorkItem((x) =>
{
while (true)
for (int i = 1; i < 11; i++)
DisplayinRichTextbox("here" + i + "/n");
});
}
I would like to have a progress bar, where the value of the bar would rise if the button was being pressed down(MouseDown Event), the bar needs to rise simultaneousely.
Any ideas? I tried with a timer but this is all I have currently
private void button1_Click(object sender, EventArgs e)
{
progressBar1.PerformStep();
progressBar1.UseWaitCursor = true;
}
private void button2_Click(object sender, EventArgs e)
{
progressBar1.Value = 0;
}
public void button1_MouseDown(object sender, MouseEventArgs e)
{
timer1.Start();
}
public void button1_MouseUp(object sender, MouseEventArgs e)
{
timer1.Stop();
}
private void timer1_Tick(object sender, EventArgs e)
{
}
It should look like this:
void timer_Tick(object sender, EventArgs e) {
progressBar1.PerformStep();
}
void button1_MouseDown(object sender, MouseEventArgs e) {
timer.Start();
}
void button1_MouseUp(object sender, MouseEventArgs e) {
timer.Stop();
}
The following program will update the progress bar each 20 ms the mouse is held down and get 10 100% after 2 seconds
public partial class Form1 : Form
{
private Timer pbTimer;
private int pbProgress = 0;
public Form1()
{
InitializeComponent();
pbTimer = new Timer();
pbTimer.Tick += new EventHandler(ProgressUpdate);
pbTimer.Interval = 20;
this.MouseDown += Form1_MouseDown;
this.MouseUp += Form1_MouseUp;
}
private void ProgressUpdate(object sender, EventArgs e)
{
if (pbProgress < 100)
{
progressBar1.Value = ++pbProgress;
}
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
pbTimer.Start();
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
pbTimer.Stop();
progressBar1.Value = 0;
pbProgress = 0;
}
I have a simple bitmap editor for small monochrome bitmaps. And I have a menus which allows to choose thickness of the pen and robber. The problem is that I don't like the following code. Is there any way to make it more compact?
private void toolStripMenuItem4_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 3;
}
private void toolStripMenuItem3_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 2;
}
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 1;
}
private void toolStripMenuItem5_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 4;
}
private void toolStripMenuItem6_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 5;
}
private void toolStripMenuItem7_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 6;
}
private void toolStripMenuItem8_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 7;
}
private void toolStripMenuItem9_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 8;
}
private void toolStripButton3_Click_1(object sender, EventArgs e)
{
CurrentPanel.EditMode = EditMode.Clear;
}
private void toolStripDropDownButton2_Click(object sender, EventArgs e)
{
CurrentPanel.EditMode = EditMode.FreeHand;
}
private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
{
CurrentPanel.EditMode = EditMode.Clear;
}
private void toolStripMenuItem10_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 1;
}
private void toolStripMenuItem11_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 2;
}
private void toolStripMenuItem12_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 3;
}
private void toolStripMenuItem13_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 4;
}
private void toolStripMenuItem14_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 5;
}
private void toolStripMenuItem15_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 6;
}
private void toolStripMenuItem16_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 7;
}
private void toolStripMenuItem17_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 8;
}
private void toolStripMenuItem18_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 16;
}
private void toolStripMenuItem19_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 32;
}
private void toolStripMenuItem21_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 32;
}
private void toolStripMenuItem20_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 16;
}
private void toolStripMenuItemCommon_Click(object sender, EventArgs e)
{
ToolStripMenuItem item = (ToolStripMenuItem) sender;
int thickness = (int)item.Tag;
CurrentPanel.PenThickness = thickness ;
}
Of course you need to initialize Tag of each ToolStripMenuItem and set toolStripMenuItemCommon_Click as a handler for Click event. You can do it in a for cycle for example:
for(int i = 1; i < 8; i++)
{
ToolStripMenuItem item = new ToolStripMenuItem();
item.Text = "Set thickness: " + i;
item.Click += toolStripMenuItemCommon_Click;
item.Tag = i;
// add item to strip container
}
Yes, there is:
private void SetPenThickness(int thickness)
{
CurrentPanel.PenThickness = thickness;
}
toolStripMenuItem1.Click += (s,e) => SetPenThickness(1);
toolStripMenuItem2.Click += (s,e) => SetPenThickness(2);
toolStripMenuItem3.Click += (s,e) => SetPenThickness(3);
toolStripMenuItem4.Click += (s,e) => SetPenThickness(4);
// ...
Try this:
private void toolStripMenuItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem toolStripMenuItem = (ToolStripMenuItem) sender;
switch (toolStripMenuItem.ID)
{
case "toolStripMenuItem4":
{
CurrentPanel.PenThickness = 3;
break;
}
..........
..........
}
}