-
June 14th, 2012, 03:38 AM
#1
[RESOLVED] How to pass character arrays as parameters to a Thread^
Hi,
I'm using C++ and I want to start a thread calling a function (extractVoice) that has char* as two parameters. Below is my code:
Thread^ extractVoiceThread = gcnew Thread(gcnew ParameterizedThreadStart(this, &MainWindow::extractVoice));
extractVoiceThread->Start(filepathV, file);
void ConEmRecog::MainWindow::extractVoice(char* filepath, char* filename){
}
However, the parameter can only one and of type System ^ Object. In my case I need to pass two character arrays. Any ideas how this can be done?
Thank you very much!
-
June 18th, 2012, 02:09 PM
#2
Re: How to pass character arrays as parameters to a Thread^
I've always found it easiest to use the instance of a class in the thread proc - that way I can access any of the class members from within the thread.
Code:
#include "stdafx.h"
using namespace System;
using namespace System::Threading;
public ref class MyThread
{
public:
void StartThread()
{
Thread^ thread = gcnew Thread( gcnew ThreadStart(this, &MyThread::ExtractVoice));
thread->Start();
}
MyThread( String^ filePath, String^ fileName)
{
_filePath = gcnew String( filePath );
_fileName = gcnew String( fileName );
}
MyThread(MyThread^ r)
{
_filePath = gcnew String(r->GetFilePath());
_fileName = gcnew String(r->GetFileName());
}
String^ GetFilePath() { return _filePath; }
String^ GetFileName() { return _fileName; }
protected:
void ExtractVoice( )
{
Console::WriteLine( "FilePath: {0} FileName: {1}", GetFilePath(), GetFileName() );
}
private:
String^ _filePath;
String^ _fileName;
};
int main(array<System::String ^> ^args)
{
MyThread^ myThread = gcnew MyThread( "C:\\temp", "test.png" );
myThread->StartThread( );
Console::ReadLine();
return 0;
}
Also, I noticed that your code seems to indicate that you are accessing windows controls from within your thread proc. Keep in mind that threads shouldn't access windows controls (other than from the UI thread that created the control). For more info, see How to: Make Thread-Safe calls to Windows Forms Controls.
-
June 18th, 2012, 08:56 PM
#3
Re: How to pass character arrays as parameters to a Thread^
Hi,
Thanks for your reply. I just changed my parameter into array<String^>^ and the error is gone.
Thanks!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|