using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Winforms_Task_Test1
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private async void button_Go_Click(object sender, EventArgs e)
{
TestClass item = new TestClass();
int limit = 10;
item.Init(textBox_Status, progressBar_Progress, limit, 100);
for (int i=1;i<=limit;i++)
{
await item.Process(i);
}
}
private void button_Exit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Winforms_Task_Test1
{
class TestClass
{
public Control Text_control;
public ProgressBar Progress_control;
public int delay;
public int max_value;
public void SetControlThreadSafe(Control control, Action<object[]> action, object[] args)
{
if (control.InvokeRequired)
{
try
{
control.Invoke(new Action<Control, Action<object[]>, object[]>(SetControlThreadSafe), control, action, args);
}
catch
{
}
}
else action(args);
}
public void Init(Control textbox_control, ProgressBar progressbar_control, int limit, int delay_ms)
{
Text_control = textbox_control;
Progress_control = progressbar_control;
SetControlThreadSafe(Text_control, (arg) =>
{
Progress_control.Value = 0;
Progress_control.Maximum = limit;
// Progress_control.Refresh();
}, null);
SetControlThreadSafe(Text_control, (arg) =>
{
Text_control.Text = "";
// Text_control.Refresh();
}, null);
delay = delay_ms;
max_value = limit;
}
public async Task Process(int counter)
{
SetControlThreadSafe(Progress_control, (arg) =>
{
Progress_control.Value = counter;
Progress_control.Refresh();
}, null);
SetControlThreadSafe(Text_control, (arg) =>
{
Text_control.Text = string.Format("Counter: {0}", counter);
Text_control.Refresh();
}, null);
await Task.Delay(delay);
}
}
}
Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.
When asked, what has been your best career decision?
Deciding to stick with EE.
Being involved with EE helped me to grow personally and professionally.
Connect with Certified Experts to gain insight and support on specific technology challenges including:
We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE