CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2008
    Posts
    161

    [RESOLVED] All methods in class on a thread?

    Code:
        public static class MyClass
        {
            public static string Version = "2.0 Beta";
            public static string ListLocation = "EList.txt";
            public static Thread myClassThread = null;
    
            public static void Startup( )
            {
                myClassThread = new Thread( new ThreadStart( StartupCheck ) );
                myClassThread.Start( );
            }
    
            private static void StartupCheck( )
            {
                if ( ExistCheck( ) == false )
                {
                    SaveList( );
                }
                else
                {
                    LoadList( );
                }
            }
    
            private static bool ExistCheck( )
            {
                Log.Write( "Checking for \"File.txt\"..." );
                Thread.Sleep( 2000 );
                bool exists = false;
                if ( File.Exists( ListLocation ) )
                {
                    Log.Write( "\"File.txt\" found." );
                    exists = true;
                }
                else
                {
                    Log.Write( "\"File.txt\" not found." );
                }
                return exists;
            }
    
            public static void SaveList( )
            {
                try
                {
                    Log.Write( "Attempting to save \"File.txt\"..." );
                    Thread.Sleep( 2000 );
                    TextWriter tw = new StreamWriter( ListLocation );
    
                    tw.Close( );
                    Log.Write( "\"File.txt\" saved successfully." );
                }
                catch ( Exception )
                {
                    Log.Write( "An error occured while saving \"File.txt\"" );
                }
            }
    
            public static void LoadList( )
            {
                try
                {
                    Log.Write( "Loading \"File.txt\"..." );
                    Thread.Sleep( 2000 );
                    TextReader tr = new StreamReader( ListLocation );
                    MainFrm.frm.LoadList( tr.ReadToEnd( ) );
                    tr.Close( );
                    Log.Write( "\"File.txt\" loaded successfully." );
                }
                catch ( Exception )
                {
                    Log.Write( "Error loading \"File.txt\"!" );
                }
                
            }
        }
    In the above class i call the startup method on the applications start.
    Obviously that runs the two methods below on a separate thread.
    But i sometimes need to call the "SaveList()" and "LoadList()" outside of the startup
    method.

    But if i setup their own threads and run the startup it will start unnecessary threads.

    Anyone have any ideas on what i should do? Threading isn't my strongest area...

    Appreciate it.
    Last edited by Pale; June 10th, 2009 at 10:33 AM.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: All methods in class on a thread?

    You could make the StartupCheck( ) method public and call it directly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured