I am trying to call my static function using a separate thread, I have in my function something like this->listBox1->Items->Add(s);
. The compiler shows that I can't use this
inside a static function. I tried to make my function non-static (i.e remove static
keyword) but when I did that, again the compiler shows two errors which are:
Error 2 error C3350: 'System::Threading::ThreadStart' : a delegate constructor expects 2 argument(s) c:\users\ahmed\documents\visual studio 2010\projects\testscan\testscan\Form1.h 116
Error 1 error C2276: '&' : illegal operation on bound member function expression c:\users\ahmed\documents\visual studio 2010\projects\testscan\testscan\Form1.h 116
Edit:
The function:
void ScanMyDir(String^ SourceDir)
{
array <String^> ^fileEntries = Directory::GetFiles(SourceDir);
for each (String^ fileName in fileEntries)
this->Form1->listBox1->Items->Add(fileName);
array<String^> ^SubDirEntries = Directory::GetDirectories(SourceDir);
for each (String^ subdir in SubDirEntries)
if ((File::GetAttributes(subdir) & FileAttributes::ReparsePoint)!= FileAttributes::ReparsePoint)
ScanMyDir(subdir);
}
Way to call it:
void button1_Click(System::Object^ sender, System::EventArgs^ e) {
Thread ^thr1 = gcnew Thread(gcnew ParameterizedThreadStart(this,&Form1::ScanMyDir));
thr1->Start("c:\\");
}
Modification on Form load:
void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
System::Windows::Forms::Control::CheckForIllegalCrossThreadCalls = false;
}
The new errors :( :
Error 5 error C3352: 'void testScan::Form1::ScanMyDir(System::String ^)' : the specified function does not match the delegate type 'void (System::Object ^)' c:\users\ahmed\documents\visual studio 2010\projects\testscan\testscan\Form1.h 117
Error 1 error C2273: 'function-style cast' : illegal as right side of '->' operator c:\users\ahmed\documents\visual studio 2010\projects\testscan\testscan\Form1.h 105
Error 2 error C2227: left of '->listBox1' must point to class/struct/union/generic type c:\users\ahmed\documents\visual studio 2010\projects\testscan\testscan\Form1.h 105
Error 3 error C2227: left of '->Items' must point to class/struct/union/generic type c:\users\ahmed\documents\visual studio 2010\projects\testscan\testscan\Form1.h 105
Error 4 error C2227: left of '->Add' must point to class/struct/union/generic type c:\users\ahmed\documents\visual studio 2010\projects\testscan\testscan\Form1.h 105