Resuming reinitialization c #

I globally declared a stream

private Thread _ftpthread ;

this stream is used to upload image to ftp

and in my function i used

 private void uploadImage()
{

...
...
_ftpthread = new Thread(() => sendThumbsToFTP(path,image));
_ftpthread.Start();

_ftpthread = new Thread(() => sendThumbsToFTP(path2,image2));
_ftpthread.Start();
...
...
}

      

My question is, can I initialize a thread like this? will the first thread terminate when reinitialized? or will both be done?

+3


source to share


2 answers


To answer your questions:

  • Is it possible to initialize a stream like this?

    Of course, why not?

  • Will the first thread terminate when reinitialized? or will both be done?

    Both will be executed, and the first thread will just run until finished.




The field _ftpthread

is just a link to the generated stream, and there should be no hidden semantics to rewrite the link other than losing the ability to reference it. So your code is almost * , equivalent to:

private Thread _ftpthread;

private void uploadImage()
{
    //...
    new Thread(() => sendThumbsToFTP(path,image)).Start();

    _ftpthread = new Thread(() => sendThumbsToFTP(path2,image2));
    _ftpthread.Start();

    //...
}

      

* I say almost because in a multi-threaded environment there is a chance that another thread is accessing _ftpthread

between the two destinations in your source code and gets a reference to the first thread created. But if no other threads are accessing _ftpthread

, the code snippets are equivalent.

+2


source


both will run together, and in sound 2, the thread will run together and don't stop the first time.

you can create a method like this if you want to use TPL using Thread directly



private void uploadImage() {
    Parallel.Invoke(
        () => sendThumbsToFTP(path,image),
        () => sendThumbsToFTP(path2,image2));
}

      

also you can see this link http://msdn.microsoft.com/en-us/library/ff963549.aspx to see how you can use Tasks.

+1


source







All Articles