나는 기능을 정의하는 System::Windows::Forms
클래스를 만들었습니다 : I 입력하여 분리 된 스레드에서 호출 할새 스레드를 만들고 실행하는 방법은 무엇입니까?
System::Void expanding(System::Windows::Forms::TreeViewEventArgs^ e)
{
//some code
}
:
e
가
treeView
구성 요소에서
afterCheck
기능에 의해 전달되는
Thread^ thisThread = gcnew Thread(
gcnew ThreadStart(this,&Form1::expanding(e)));
thisThread->Start();
.
error C3350: 'System::Threading::ThreadStart' : a delegate constructor expects 2 argument(s)
및
error C2102: '&' requires l-value
나는 그것이에 보여 주었다 그대로 Form1
의 새로운 인스턴스를 만들려고 :
this example from MSDN 모든에 따르면 잘 작동하지만, 대신에 나는 컴파일러 오류가 발생한다 MSDN 예제를 사용하지만 결과는 같습니다.
@Tudor가 간계 한 내용. 그러나 using System :: Threading Form1 클래스의 구성 요소를 수정할 수 없습니다. 그래서 내가 다른 솔루션을 찾고 있었고, 난
this을 발견했습니다 어쩌면 내가 이해하지 못했다 방법 BackgroundWorker에 일을하지만 차단 GUI.
내가 원하는 것은 사용자가 특정 버튼을 사용하여 프로세스를 중지 할 수 있도록 별도의 스레드 (어떤 방법 으로든 완료해야 함)를 실행하는 것이 었습니다.이 새 스레드는 사용할 수 있습니다. 부모 스레드의 구성 요소. 여기
는 BackgroundWorker에를 사용하여 내 샘플 코드입니다//Worker initialization
this->backgroundWorker1->WorkerReportsProgress = true;
this->backgroundWorker1->DoWork += gcnew System::ComponentModel::DoWorkEventHandler(this, &Form1::backgroundWorker1_DoWork);
this->backgroundWorker1->ProgressChanged += gcnew System::ComponentModel::ProgressChangedEventHandler(this, &Form1::backgroundWorker1_ProgressChanged);
this->backgroundWorker1->RunWorkerCompleted += gcnew System::ComponentModel::RunWorkerCompletedEventHandler(this, &Form1::backgroundWorker1_RunWorkerCompleted);
비동기 조작은 버튼 클릭 이벤트 처리기 기능은 기본 재귀 가져 오는 기능
System::Void fetch(String^ thisFile)
{
try{
DirectoryInfo^ dirs = gcnew DirectoryInfo(thisFile);
array<FileSystemInfo^>^dir = (dirs->GetFileSystemInfos());
if(dir->Length>0)
for(int i =0 ;i<dir->Length;i++)
{
if((dir[i]->Attributes & FileAttributes::Directory) == FileAttributes::Directory)
fetch(dir[i]->FullName);
else
**backgroundWorker1 -> ReportProgress(0, dir[i]->FullName);**//here i send results to be printed on gui RichTextBox
}
}catch(...){}
}
에게 있습니다
System::Void fetchClick(System::Object^ sender, System::EventArgs^ e) {
dirsCreator();//List of directories to be fetched
backgroundWorker1 ->RunWorkerAsync();
}
DoWork에 의해 호출 그렇지 않으면 준수하지 않는,
이Thread^ thisThread = gcnew Thread(
gcnew ThreadStart(this,&Form1::expanding));
thisThread->Start();
는 또한 함수가 매개 변수를 사용하지 않아야
그리고 여기 당신은 함수 호출에 매개 변수를 지정할 필요가 없습니다 기능
System::Void backgroundWorker1_ProgressChanged(System::Object^ sender, System::ComponentModel::ProgressChangedEventArgs^ e) {
this->outputBox->AppendText((e->UserState->ToString())+"\n");
this->progressBar1->Value = (this->rand->Next(1, 99));
}
참고로 Visual C++는 단순히 IDE입니다. C++/CLI는 Microsoft가 관리되는 통합을 지원하기 위해 추가 한 C++의 확장 이름입니다. – user7116