我现在正在创建一个文件事务系统(通过FTP),使用wxWidgets作为GUI和一个来自CodeProject的多线程类(http://www.codeproject.com/KB/threads/SynchronizedThreadNoMfc.aspx) - 请先阅读文章以供参考。
在我的GUI中,我有一个文本框(wxTextCtrl),它存储了想要发送到FTP服务器的文件路径,我想通过多线程函数获取它的值。
到目前为止,这是我的代码:(简化;多个文件)
/////// 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的内容(使用tb_file-&gt; GetValue())并将其传递给多线程函数(void run())稍后上传。
任何形式的帮助都将不胜感激!
感谢。 (抱歉长代码..)
答案 0 :(得分:3)
这很简单。
你应该创建一个start函数,它接受std :: string(或任何其他参数)并将它们(它们)存储在svThread对象中。然后你可以在run函数中访问它:
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());
答案 1 :(得分: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());
// ...
}
答案 2 :(得分:0)
您希望run
看到的任何数据都可以存储为svThread
类的成员数据。将其作为成员数据存储的好方法是将其作为参数传递给svThread
构造函数。
答案 3 :(得分:0)
我还要使hInternet和hFTPSession不是全局的,并将它们传递给每个线程。这样你以后在使用多个ftp会话时就不会遇到麻烦了。 我还宁愿不在maindlg.cpp中声明它们,主对话框是gui部分,这些变量与gui没有任何关系。
btw
的目的是什么?if(this->ThreadName == "Upload")
我的意思是,如果线程是用于上传的,为什么要对它的名字字符串进行双重检查?