Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use "this" in a thread!
    primarykey
    data
    text
    <p>I am trying to call my static function using a separate thread, I have in my function something like <code>this-&gt;listBox1-&gt;Items-&gt;Add(s);</code>. The compiler shows that I can't use <code>this</code> inside a static function. I tried to make my function non-static (i.e remove <code>static</code> keyword) but when I did that, again the compiler shows two errors which are:</p> <blockquote> <p>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</p> <p>Error 1 error C2276: '&amp;' : illegal operation on bound member function expression c:\users\ahmed\documents\visual studio 2010\projects\testscan\testscan\Form1.h 116</p> </blockquote> <hr> <p><strong>Edit</strong>:</p> <p>The function:</p> <pre><code>void ScanMyDir(String^ SourceDir) { array &lt;String^&gt; ^fileEntries = Directory::GetFiles(SourceDir); for each (String^ fileName in fileEntries) this-&gt;Form1-&gt;listBox1-&gt;Items-&gt;Add(fileName); array&lt;String^&gt; ^SubDirEntries = Directory::GetDirectories(SourceDir); for each (String^ subdir in SubDirEntries) if ((File::GetAttributes(subdir) &amp; FileAttributes::ReparsePoint)!= FileAttributes::ReparsePoint) ScanMyDir(subdir); } </code></pre> <p>Way to call it:</p> <pre><code>void button1_Click(System::Object^ sender, System::EventArgs^ e) { Thread ^thr1 = gcnew Thread(gcnew ParameterizedThreadStart(this,&amp;Form1::ScanMyDir)); thr1-&gt;Start("c:\\"); } </code></pre> <p>Modification on Form load:</p> <pre><code>void Form1_Load(System::Object^ sender, System::EventArgs^ e) { System::Windows::Forms::Control::CheckForIllegalCrossThreadCalls = false; } </code></pre> <p>The new errors :( :</p> <blockquote> <p>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</p> <p>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</p> <p>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</p> <p>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</p> <p>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</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload