Managed forms application with 'autosave' wanted
-
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,
-
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,
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); }
-
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); }
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);
toThreading::Thread::Sleep(5000);
which resolved those errors. Now Im still left with C3845. -
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);
toThreading::Thread::Sleep(5000);
which resolved those errors. Now Im still left with C3845.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:
-
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:
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 ')' -
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 ')'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:
-
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:
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, -
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:
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