2009-09-26 2 views
0

GUI 용 wxWidgets와 CodeProject()의 멀티 스레딩 클래스를 사용하여 FTP를 통한 파일 트랜잭션 시스템을 만들고 있습니다. 먼저이 기사를 읽으십시오.클래스와 관련이없는 다른 함수를 통해 인스턴스화 된 클래스 멤버 변수를 호출하는 방법은 무엇입니까?

내 GUI에는 FTP 서버에 보내려는 파일 경로를 저장하는 텍스트 상자 (wxTextCtrl)가 있는데 멀티 스레딩 기능을 통해 그 값을 얻고 싶었습니다. 여기

지금까지 내 코드입니다 : (간체, 여러 파일) ​​

/////// Organizer.h // Main header file that utilizes all other headers 
#include <wx/wx.h> 
#include <wx/datectrl.h> 
#include <wininet.h> 
#pragma comment(lib, "wininet.lib") 
#include "Threading.h" 
#include "MainDlg.h" 
#include "svDialog.h" 

///////// Threading.h // Please read the article given above 
#include "ou_thread.h" 
using namespace openutils; 

extern HINTERNET hInternet; // both declared in MainDlg.cpp 
extern HINTERNET hFtpSession; 

class svThread : public Thread 
{ 
private: 
    char* ThreadName; 
public: 
    svThread(const char* szThreadName) 
    { 
    Thread::setName(szThreadName); 
    this->ThreadName = (char*)szThreadName; 
    } 
    void run() 
    { 
    if(this->ThreadName == "Upload") 
    { 
    hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0); 
    hFtpSession = InternetConnect(hInternet, L"myserver.com", INTERNET_DEFAULT_FTP_PORT, L"user", L"pass", INTERNET_SERVICE_FTP, 0, 0); 

    std::string filenameOnServer((char*)tb_file->GetValue().c_str()); // HERE..the tb_file.. 
    std::vector<std::string> filepathParts; 
    __strexp(filenameOnServer, "\\", filepathParts); // this is user-defined function that will split a string (1st param) with the given delimiter (2nd param) to a vector (3rd param) 
    filenameOnServer = filepathParts.at(filepathParts.size() - 1); // get only the filename 

    if(FtpPutFile(hFtpSession, tb_file->GetValue().c_str(), (LPCWSTR)filenameOnServer.c_str(), FTP_TRANSFER_TYPE_BINARY, 0)) 
    { 
     MessageBox(NULL, L"Upload Complete", L"OK", 0); 
    } 
    else 
    { 
     MessageBox(NULL, L"Upload Failed", L"OK", 0); 
    } 
    } 
    } 
}; 

////////// svDialog.h 
class svDialog : public wxFrame 
{ 
public: 
    svDialog(const wxString &title); 
    void InitializeComponent(); 
    void ProcessUpload(wxCommandEvent &event); // function (button event) that will start the UPLOAD THREAD 
    wxTextCtrl *tb_file; // this is the textbox 
    //....other codes 
}; 

///////////svDialog.cpp 
#include "Organizer.h" 
Thread *UploadRoutine; 

svDialog::svDialog(const wxString &title) : wxFrame(...) // case unrelated 
{ 
    InitializeComponent(); 
} 
void svDialog::InitializeComponent() 
{ 
    tb_file = new wxTextCtrl(...); 
    //......other codes 
} 
void svDialog::ProcessUpload(wxCommandEvent &event) 
{ 
    UploadRoutine = new svThread("Upload"); 
    UploadRoutine->start(); 
    //......other codes 
} 

////// MainDlg.cpp // (MainDlg.h only contains the MainDlg class declaration and member function prototypes) 
#include "Organizer.h" 

HINTERNET hInternet; 
HINTERNET hFtpSession; 
IMPLEMENT_APP(MainDlg) // wxWidgets macro 

bool MainDlg::OnInit() // wxWidgets window initialization function 
{ 
    //......other codes 
} 

글쎄, 당신은 위의 내 코드에서 볼 수 있듯이, 나는 (tb_file-> GetValue와 (tb_file의 콘텐츠를하고 싶어)) 나중에 그것을 업로드하기 위해 멀티 스레딩 기능 (void run())에 전달하십시오.

모든 종류의 도움을 주시면 감사하겠습니다.

감사합니다. (그리고 긴 코드 .. 죄송합니다)

답변

3

을 저장해야합니다.

std :: string (또는 다른 매개 변수)을 사용하여 svThread 객체에 저장하는 start 함수를 만들어야합니다. 그런 다음 실행 기능에 액세스 할 수 있습니다

class svThread : public Thread 
{ 
    private: 
     char* ThreadName; 
     std::string FileName; 

    public: 
     svThread(const char* szThreadName) 
     { 
     Thread::setName(szThreadName); 
     this->ThreadName = (char*)szThreadName; 
     } 

     void Start(const std::string& filename) 
     { 
     this->FileName = filename; 
     Thread::Start(); 
     } 

     void Run() 
     { 
     // ... 
     if(FtpPutFile(hFtpSession, FileName,(LPCWSTR)filenameOnServer.c_str(), FTP_TRANSFER_TYPE_BINARY, 0)) 
     // ... 
     } 
}; 

를 대화 상자 클래스에서 그냥 같이 스레드를 시작해야합니다

UploadRoutine = new svThread("Upload"); 
UploadRoutine->start(tb_file->GetValue().c_str()); 
0

run보고 싶은 모든 데이터는 회원 데이터 svThread 클래스로 저장할 수 있습니다. 구성원 데이터로 저장하는 좋은 방법은 매개 변수로 svThread 생성자에 전달하는 것입니다.

1

당신은 그것은 매우 간단 스레드가 스레드의 멤버 변수로 필요한 데이터, 예컨대 :

class svThread : public Thread 
{ 
private: 
    const std::string filename_; 
public: 
    svThread(const std::string& filename) 
     : filename_(filename) 
    {} 
    void run() 
    { 
     // ... 
      __strexp(filename_, /* ... */); 
     // ... 
    } 
}; 

void svDialog::ProcessUpload(wxCommandEvent &event) 
{ 
    UploadRoutine = new svThread(tb_file->GetValue()); 
    // ... 
} 
0

는 또한 HINTERNET 및 hFTPSession 글로벌하지 만들 것이라고 및 통과 그것들을 각 스레드에 보냅니다. 그런 식으로 나중에 여러 ftp 세션을 활용할 때 문제가 생길 수 있습니다. 또한 maindlg.cpp에서 선언하지 말고, 기본 대화 상자는 gui 파트이며, 해당 변수는 gui와 관련이 없습니다.

은 BTW 이유는 이름 문자열의에 있음을 번 확인해야

if(this->ThreadName == "Upload") 

스레드가 업로드위한 것입니다 만약 내가 의미의 목적은 무엇인가?

+0

나는 단순하다고 말했다. 다른 관련이없는 코드는 추가하지 않았습니다. –

관련 문제