As you are already using a background worker, the easiest way to fix the problem would be to use it properly!
private: System::Void backgroundWorker1_DoWork_1(
System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e)
{
while (true)
{
int data = runPing();
backgroundWorker1->ReportProgress(0, Convert::ToString(data));
Sleep(2500);
}
}
And connect an handler for ProgressChanged event and then move some of your code to it:
void backgroundWorker1_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
auto textData = e->UserData.ToString()UserState->ToString();
pingOutbox->Text = textData;
cout << textData << endl;
}
It would probably also be possible to leave cout in the background loop but it might mess up the outputed text if multiple threads are writing to it.
Update
By the way, reading official documentation would help:
How to: Implement a Form That Uses a Background Operation[
^]
Also, it is very easy to search help using Google once you have the error message and you won't have to wait for someone to answer!