Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C#
namespace SampleThreading
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : System.Windows.Window
    {

        public MainWindow ( )
        {
            InitializeComponent ( );
            Thread th = new Thread ( new ThreadStart ( CopyFile ) );
            th.IsBackground = true;
            th.Start ( );
        }

        private void CopyFile ( )
        {

           

        }
    }
}


I Want to continuously running the method CopyFile ( ). its not working
Posted
Updated 2-Mar-15 22:29pm
v2
Comments
Afzaal Ahmad Zeeshan 3-Mar-15 4:30am    
You're not at all looping the statement, how can that execute multiple times?
Mahesh Alappuzha 3-Mar-15 4:41am    
how can i
loop
Richard MacCutchan 3-Mar-15 4:58am    
Your CopyFile method needs to actually do something.

C#
public MainWindow ( )
        {
            InitializeComponent ( );
            Thread th = new Thread ( ( ) =>
            {
                while ( true )
                {
                    CopyFile ( );
                }
            } );

            th.Start ( );
        }
 
Share this answer
 
 
Share this answer
 
CopyFile is executed once, of course. If you need a never-ending processing there then you have to use an infinite loop.
 
Share this answer
 
Comments
Mahesh Alappuzha 3-Mar-15 5:48am    
infinite loop for Thread??
CPallini 3-Mar-15 6:10am    
You don't like infinite loops, do you?
Mahesh Alappuzha 3-Mar-15 6:32am    
Thread t = new Thread( () =>
{
while(true)
{
CopyFile()
}
});

t.Start();

LIKE THIS??
CPallini 3-Mar-15 6:37am    
Nope. CopyFile should do its useful stuff and terminate. If it must run continuosly (e.g. copying an infinite list of files) then it should implement an infinite loop.
Mahesh Alappuzha 3-Mar-15 6:42am    
then how it is possible??
this is my CopyFile Function
private void CopyFile ( )
{
string sSourceFolderPath = @"E:\Source";
string sDestinationFolderPath = @"E:\Destination";

DirectoryInfo dDirectoryInfo = new DirectoryInfo ( sSourceFolderPath );
FileInfo[] Files = dDirectoryInfo.GetFiles ( );

foreach ( FileInfo file in Files )
{
string sSourceFileName = file.Name;
string sDestinationFilePath = System.IO.Path.Combine ( sDestinationFolderPath , sSourceFileName );
string sSourceFilePath = System.IO.Path.Combine ( sSourceFolderPath , sSourceFileName );

if ( !System.IO.Directory.Exists ( sDestinationFolderPath ) )
{
System.IO.Directory.CreateDirectory ( sDestinationFolderPath );
}
System.IO.File.Copy ( sSourceFilePath , sDestinationFilePath , true );
}

Thread.Sleep ( 5000 );/* milliseconds to wait */
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900