简体   繁体   中英

Start same thread twice - the second time after the first one finished

So I made a Thread which executes one function which is dependent on a global variable. After that, I would like to change that variable and run the thread once again, so that it executes my procedure once again with another argument. The problem is, I can't seem to figure out when my Thread finished. After a lot of googling, I found nothing that could help me. Can someone here help ? Thanks in advance. Some code would be appreciated. Here is mine:

//temp_folder is global variable

temp_folder = APPDATA + "Local" + "\\" + "Temp\\";

Thread oThread = new Thread(new ThreadStart(clean_temp));
oThread.Start();

//Here I should wait for this oThread to finish, then change the temp_folder
//variable mentioned in comment down and start once again

//temp_folder = WINDIR + "\\" + "Temp\\";
//oThread.Start();

Here is a clean_temp() procedure. And yes, this is GUI app.

public void clean_temp()
{
DirectoryInfo directory = new DirectoryInfo(temp_folder);

try
{
    DirectoryInfo[] folders = directory.GetDirectories();

    foreach (DirectoryInfo folder in folders)
    {
        temp_folder = temp_folder + folder.Name + "\\";
        clean_temp();
        temp_folder = temp_folder.Replace(folder.Name + "\\", "");
    }
}
catch { }

long sz;
double size, trenutna;

foreach (FileInfo file in directory.GetFiles())
{
    sz = file.Length;
    size = (double)sz / 1024.0;
    trenutna = Convert.ToDouble(lbl_junk_size.Text);
    trenutna += size;

    MethodInvoker mi_invoker_size_change = new MethodInvoker(()=> lbl_junk_size.Text = trenutna.ToString());
    lbl_junk_size.Invoke(mi_invoker_size_change);

    MethodInvoker mi_invoker_count_change = new MethodInvoker(() => lbl_junk.Text = (Convert.ToInt64(lbl_junk.Text) + 1).ToString());
    lbl_junk.Invoke(mi_invoker_count_change);
}
}

You can use Join() to make sure your thread has finished.

Eg :

Thread oThread = new Thread(new ThreadStart(clean_temp));
oThread.Start();

oThread.Join();

I think you are asking the wrong question here. Instead of asking how to wait for a thread to finish you should look at how to perform another operation after the first finishes.

In most cases you would do this using the Task API in .Net . Tasks give you a very clean way to define an operation that is to be performed asynchronously and then do something else when it is done.

So you could probably do something like this pseudo-code:

var task = new Task(clean_temp);
task.ContinueWith(update_global_variable);
task.ContinueWith(clean_temp);
task.Wait();

The actual code won't be exactly like this, but the idea is that you define something that should be done (call clean_temp), then you define something to be done after that first job is done (update_global_variable) and so on. You can chain as many operations you want.

This will probably be much cleaner than handling threads yourself.

EDIT: Actually, you could probably simplify your code even more by getting rid of the global variable.

If you modify your clean_temp method to take an input argument that specifies the folder to clean then you can do the following (again, pseudocode!!!):

new Task(clean_temp, "C:\first temp folder")
   .ContinueWith(clean_temp, "C:\second folder")
   .Wait();

Much cleaner and much safer :-)

If you are using C# 5, try wrapping your thread within a task. Tasks provide a method called 'ContinueWith' in which you can call your thread again. Or asynchronous calls with 'await'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM