Reputation: 71
I am currently working on a project that requires processing a lot of data, so I decided to create a windows form informing the user that the processing is in progress. Of course I have to use another thread. But I need to run this windows form several times (not at the same time but it starts well the first time and not well the second), I must have a method that starts the thread and another that stops it as well as a label that will display different things depending on the number of loop perform in a method that is in another thread (it's tricky^^).
I read a lot about threads but I still have not found how to do it. Some code I did :
public partial class Progress : Form
{
Thread thr = null;
public Progress()
{
InitializeComponent();
try
{
thr = new Thread(() => this.ShowDialog());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void Start()
{
thr.Start();
//this.ShowDialog();
}
public void Stop()
{
thr.Abort();
thr.Join();
//this.Close();
}
and i want to use something liked that, How can I do it ?
other class {
Progress t = new Progress();
t.Start();
firstFunction();
SecondBigFunction(t);
threeFunction();
t.Stop();
}
SecondBigFunction(Thread t){
while(list.lenght > 0){
// Do somework
t.labelToChange += 1;
}
}
Thank you.
Upvotes: 1
Views: 101
Reputation: 10929
labeltoChange.Invoke((MethodInvoker)(() => labeltoChange.Text+=1));
This method is a good way, it's just that when you do labeltoChange.Text+=1
you increment the number do not do addition.
If you want to add to the text box use this:
labeltoChange.Invoke((MethodInvoker)(() => Convert.ToInt32(labeltoChange.Text) + 1));
Upvotes: 0
Reputation: 9610
Normally all UI work should be performed on the same thread. So trying to launch a Form on a separate thread will normally just cause problems.
At the very least you would need to use Invoke, which will use the UI thread to update the control. So effectively means that you are just using one thread.
It is possible to do this, for more details you should see this; In WinForms, Is More than One Thread Possible in the UI?
and this; Multiple UI Threads - Winforms
Better practice is to put the "hard work" computations in a background worker thread, and then update the UI when you are finished.
This article gives an example; https://www.codeproject.com/Articles/99143/BackgroundWorker-Class-Sample-for-Beginners
Upvotes: 0
Reputation: 187
You can use backgroundworker class in order to achieve your task
private BackgroundWorker bg1 = new BackgroundWorker();
bg1.DoWork += bg1_DoWork;
private void bg1_DoWork(object sender, DoWorkEventArgs e)
{
//the function you want to execute
}
Similarly you can define various backgrounder workers with different functions in each one of them.
and finally call the background workers in the order in which you want the functions to be executed(all those functions will execute simultaneously).
Upvotes: 4
Reputation: 325
I am not sure but it can helps you.
labeltoChange.Invoke((MethodInvoker)(() => labeltoChange.Text+=1));
Upvotes: 3