如何使用std::thread
为UWP应用程序创建线程?对不起,我在创建对象内的成员函数的线程所需的语法方面存在问题。我刚开始在visual studio中使用C++/cx。这里是我的代码:如何在UWP中的对象的成员函数内使用std :: thread C++/CX
void MainPage::increment(int val)
{
for (int i = 0; i < val; ++i){
m_timer4++;
Sleep(100);
textBox4->Text = m_timer4.ToString();
}
}
void _4ThreadsUWP::MainPage::button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
m_timer->Start();
std::thread th(&MainPage::increment, this, 5);
}
编辑:
我有这些错误:
ERROR1:
'std::invoke': no matching overloaded function found
误差2:
Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...)'
您的线程对象在创建后立即超出范围。你应该加入它,即'th.join();'。 – Azeem
@Azeem尽管我添加了'th.join()'它仍然不能解决问题。感谢您的建议 – Gibs
很久以前,我放弃了'bind'-style语法,只是使用lambda:'std :: thread th([this] {this-> increment(5);});'。 –