I think I implemented the pattern described but get the same blocking behaivoir.
My delegate declaration
Code:
public: delegate void logThread(System::Object ^obj);
The button click function that starts the new thread and logging
Code:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
this->label2->Text = "Logging";
Thread^ newLogThread = gcnew Thread(gcnew ParameterizedThreadStart(&log));
newLogThread->IsBackground=true;
newLogThread->Start(this);
}
Here is the log function passed above to the new thread. Just calls safeThread1
Code:
static void log(System::Object ^obj)
{
SafeThread1(obj);
}
Finally the work horse of the thread that is still blocking. I was testing if ob the form invoke required but I changed it to the textBox that is being updated with the logging results. one other control is actually updated in this thread the label
Code:
static void SafeThread1(System::Object ^obj){
Form1 ^ob = (Form1^) obj;
if( ob->textBox1->InvokeRequired){
//logThread ^d = gcnew logThread(SafeThread1);
ob->BeginInvoke(gcnew logThread(ob->SafeThread1),gcnew array<System::Object^>{ob});
}
else{
while (ob->connect== EDK_OK) {
ob->state = EE_EngineGetNextEvent(ob->eEvent);
// New event needs to be handled
if (ob->state == EDK_OK) {
EE_Event_t eventType = EE_EmoEngineEventGetType(ob->eEvent);
EE_EmoEngineEventGetUserId(ob->eEvent, &userID);
// Log the EmoState if it has been updated
if (eventType == EE_EmoStateUpdated) {
EE_EmoEngineEventGetEmoState(ob->eEvent, ob->eState);
const float timestamp = ES_GetTimeFromStart(ob->eState);
ob->label2->Text=" New EmoState from user "+timestamp+userID;
ob->logEmoStateTXT(userID, ob->eState, writeHeader);
//writeHeader = false;
}
}else if (ob->state != EDK_NO_EVENT) {
ob->label2->Text = "Internal error in Emotiv Engine!";
break;
}
}
}
}