Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Managed forms application with 'autosave' wanted

Managed forms application with 'autosave' wanted

Scheduled Pinned Locked Moved Managed C++/CLI
csharphelptutorialquestionannouncement
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    iMikki
    wrote on last edited by
    #1

    I am building an application where I want to use some kinda autosave function. I found a post about using threads but I cannot find a way to get it working... Currently I have this: LoadFiles() will get run at the beginning of the application. Here should the autosave thread be started...

    public: Threading::Thread thd = new Thread(new ThreadStart(autoSave));
    private: System::Void LoadFiles(void) {
    			Threading::Thread::Start();
    		}
    private: System::Void autoSave(void) {
    			 Threading::Thread.Sleep(5000);
    		 }
    

    And getting the following errors:

    1>d:\app2\app2\Form1.h(424) : error C3845: 'App2::Form1::thd': only static data members can be initialized inside a ref class or value type
    1>d:\app2\app2\Form1.h(58) : error C2512: 'System::Threading::Thread::Thread' : no appropriate default constructor available
    1>d:\app2\app2\Form1.h(426) : error C2352: 'System::Threading::Thread::Start' : illegal call of non-static member function
    1> c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see declaration of 'System::Threading::Thread::Start'
    1>d:\app2\app2\Form1.h(429) : error C2143: syntax error : missing ';' before '.'
    1>d:\app2\app2\Form1.h(429) : error C2143: syntax error : missing ';' before '.'
    1>Build log was saved at "file://d:\App2\App2\Release\BuildLog.htm"

    Does anyone have any idea how to solve this OR how to make a better thingy? :p Kind regards,

    Z 1 Reply Last reply
    0
    • I iMikki

      I am building an application where I want to use some kinda autosave function. I found a post about using threads but I cannot find a way to get it working... Currently I have this: LoadFiles() will get run at the beginning of the application. Here should the autosave thread be started...

      public: Threading::Thread thd = new Thread(new ThreadStart(autoSave));
      private: System::Void LoadFiles(void) {
      			Threading::Thread::Start();
      		}
      private: System::Void autoSave(void) {
      			 Threading::Thread.Sleep(5000);
      		 }
      

      And getting the following errors:

      1>d:\app2\app2\Form1.h(424) : error C3845: 'App2::Form1::thd': only static data members can be initialized inside a ref class or value type
      1>d:\app2\app2\Form1.h(58) : error C2512: 'System::Threading::Thread::Thread' : no appropriate default constructor available
      1>d:\app2\app2\Form1.h(426) : error C2352: 'System::Threading::Thread::Start' : illegal call of non-static member function
      1> c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see declaration of 'System::Threading::Thread::Start'
      1>d:\app2\app2\Form1.h(429) : error C2143: syntax error : missing ';' before '.'
      1>d:\app2\app2\Form1.h(429) : error C2143: syntax error : missing ';' before '.'
      1>Build log was saved at "file://d:\App2\App2\Release\BuildLog.htm"

      Does anyone have any idea how to solve this OR how to make a better thingy? :p Kind regards,

      Z Offline
      Z Offline
      zhushaolin2005
      wrote on last edited by
      #2

      try as following public: Threading::Thread ^thd = gcnew Thread(new ThreadStart(autoSave)); private: System::Void LoadFiles(void) { thd->Start(); } private: System::Void autoSave(void) { Threading::Thread.Sleep(5000); }

      I 1 Reply Last reply
      0
      • Z zhushaolin2005

        try as following public: Threading::Thread ^thd = gcnew Thread(new ThreadStart(autoSave)); private: System::Void LoadFiles(void) { thd->Start(); } private: System::Void autoSave(void) { Threading::Thread.Sleep(5000); }

        I Offline
        I Offline
        iMikki
        wrote on last edited by
        #3

        1>d:\app2\app2\Form1.h(425) : error C3845: 'App2::Form1::thd': only static data members can be initialized inside a ref class or value type
        1>d:\app2\app2\Form1.h(428) : error C2143: syntax error : missing ';' before '.'
        1>d:\app2\app2\Form1.h(428) : error C2143: syntax error : missing ';' before '.'

        But I corrected Threading::Thread.Sleep(5000); to Threading::Thread::Sleep(5000); which resolved those errors. Now Im still left with C3845.

        M 1 Reply Last reply
        0
        • I iMikki

          1>d:\app2\app2\Form1.h(425) : error C3845: 'App2::Form1::thd': only static data members can be initialized inside a ref class or value type
          1>d:\app2\app2\Form1.h(428) : error C2143: syntax error : missing ';' before '.'
          1>d:\app2\app2\Form1.h(428) : error C2143: syntax error : missing ';' before '.'

          But I corrected Threading::Thread.Sleep(5000); to Threading::Thread::Sleep(5000); which resolved those errors. Now Im still left with C3845.

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          The error message explains it. You can't initialize "thd" like that unless you make the variable static. Either make the variable static, or move the initialization code to a method, for example - the constructor:

          public ref class MyClass
          {
          public:
          System::Threading::Thread ^thd;

          MyClass::MyClass()
          {
              thd = gcnew System::Threading::Thread(gcnew ThreadStart(this, &MyClass::autoSave)); 
          }
          

          private:
          System::Void LoadFiles(void)
          {
          thd->Start();
          }

          System::Void autoSave() 
          { 
              Threading::Thread::Sleep(5000);
          }
          

          };

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          I 2 Replies Last reply
          0
          • M Mark Salsbery

            The error message explains it. You can't initialize "thd" like that unless you make the variable static. Either make the variable static, or move the initialization code to a method, for example - the constructor:

            public ref class MyClass
            {
            public:
            System::Threading::Thread ^thd;

            MyClass::MyClass()
            {
                thd = gcnew System::Threading::Thread(gcnew ThreadStart(this, &MyClass::autoSave)); 
            }
            

            private:
            System::Void LoadFiles(void)
            {
            thd->Start();
            }

            System::Void autoSave() 
            { 
                Threading::Thread::Sleep(5000);
            }
            

            };

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            I Offline
            I Offline
            iMikki
            wrote on last edited by
            #5

            Thanks for your reply. I currently have this:

            public ref class Form1 : public System::Windows::Forms::Form
            {
            public:
            System::Threading::Thread ^thd;
            Form1::MyClass() {
            thd = gcnew System::Threading::Thread(gcnew ThreadStart(this, &Form1::autoSave));
            }

            private: System::Void autoSave(void) {
            Threading::Thread::Sleep(5000);
            warningBox("Test? 5000");
            }

            private: System::Void LoadFiles(void) {
            thd->Start();
            }
            }

            But it doesnt work due the following errors:

            1>d:\app2\app2\Form1.h(61) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
            1>d:\app2\app2\Form1.h(63) : warning C4183: 'MyClass': missing return type; assumed to be a member function returning 'int'
            1>d:\app2\app2\Form1.h(62) : error C2061: syntax error : identifier 'ThreadStart'
            1>d:\app2\app2\Form1.h(62) : error C2143: syntax error : missing ';' before ')'
            1>d:\app2\app2\Form1.h(62) : error C2143: syntax error : missing ';' before ')'

            M 1 Reply Last reply
            0
            • I iMikki

              Thanks for your reply. I currently have this:

              public ref class Form1 : public System::Windows::Forms::Form
              {
              public:
              System::Threading::Thread ^thd;
              Form1::MyClass() {
              thd = gcnew System::Threading::Thread(gcnew ThreadStart(this, &Form1::autoSave));
              }

              private: System::Void autoSave(void) {
              Threading::Thread::Sleep(5000);
              warningBox("Test? 5000");
              }

              private: System::Void LoadFiles(void) {
              thd->Start();
              }
              }

              But it doesnt work due the following errors:

              1>d:\app2\app2\Form1.h(61) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
              1>d:\app2\app2\Form1.h(63) : warning C4183: 'MyClass': missing return type; assumed to be a member function returning 'int'
              1>d:\app2\app2\Form1.h(62) : error C2061: syntax error : identifier 'ThreadStart'
              1>d:\app2\app2\Form1.h(62) : error C2143: syntax error : missing ';' before ')'
              1>d:\app2\app2\Form1.h(62) : error C2143: syntax error : missing ';' before ')'

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              iMikki wrote:

              But it doesnt work due the following errors:

              MyClass was, well, my class name.....you need to adjust the code to use your class name. Are you a beginner in C++?

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              I 1 Reply Last reply
              0
              • M Mark Salsbery

                iMikki wrote:

                But it doesnt work due the following errors:

                MyClass was, well, my class name.....you need to adjust the code to use your class name. Are you a beginner in C++?

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                I Offline
                I Offline
                iMikki
                wrote on last edited by
                #7

                Quite new yes. I had some CLI C++ at university but never worked with Managed stuff... But I did change the first part to Form1 (referencing to public ref class Form1 : public System::Windows::Forms::Form).. Any way you could help me? :p Or do you might have another idea to realize this 'autosave' thing. The code I currently have is 1400 lines long. :p Kind regards,

                1 Reply Last reply
                0
                • M Mark Salsbery

                  The error message explains it. You can't initialize "thd" like that unless you make the variable static. Either make the variable static, or move the initialization code to a method, for example - the constructor:

                  public ref class MyClass
                  {
                  public:
                  System::Threading::Thread ^thd;

                  MyClass::MyClass()
                  {
                      thd = gcnew System::Threading::Thread(gcnew ThreadStart(this, &MyClass::autoSave)); 
                  }
                  

                  private:
                  System::Void LoadFiles(void)
                  {
                  thd->Start();
                  }

                  System::Void autoSave() 
                  { 
                      Threading::Thread::Sleep(5000);
                  }
                  

                  };

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  I Offline
                  I Offline
                  iMikki
                  wrote on last edited by
                  #8

                  Good - I found the solution after some trying and stuff :p (fail, retry, fail again, error, swear, trying, reading, failing again, fixing, waiting, evil laughing, posting the solution on CodeProject) Here is the code I have now:

                  public ref class Form1 : public System::Windows::Forms::Form
                  {
                  public:
                  System::Threading::Thread ^thd;
                  private: System::Void autoSave(void) {
                  Threading::Thread::Sleep(5000);
                  warningBox("Test? 5000");
                  runSave();
                  }
                  private: System::Void runSave(void) {
                  thd = gcnew System::Threading::Thread(gcnew System::Threading::ThreadStart(this, &App2::Form1::autoSave));
                  thd->Start();
                  }

                  private: System::Void LoadFiles(void) {
                  runSave();
                  //other code
                  }

                  Works like a charm! I started the app - and 5 second later I got this message! (warningBox(std::string msg) is a messageBox produced by the Windows::Apps thingy... :p Thnx everyone! Edit: This code keeps restarting the thread. :)

                  modified on Thursday, December 3, 2009 7:49 AM

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups