title: Call a function from a thread and RS 232
body: <p>I am programming in Visual Studio 2008 in console application. I am working with a display that is communicated by Rs 232.</p>
  
  <p>I have a thread that counts from 0 to 10 seconds. When reaches 10 I want to turn off the displays backlight. For that I have a function that is called from the thread. The called from the thread is well because I know that the code of the function is executed. </p>
  
  <p>But code of turning the backlight off does not work when the function is called from the thread and it works the it is called from another place. Any ideas?
  Thanks.</p>
  
  <pre><code>void FunctionBacklightoff(HANDLE portHandle,DWORD bytesTransmitted)
  {
      cout &lt;&lt; "backoff";
      WriteFile(portHandle, backlight_off , 4, &amp;bytesTransmitted, NULL);//does not work when 
      //it is called from the thread. It works when it is called from wmain()
  }
  
  DWORD WINAPI solo_thread(void* arg)
  { 
     int Counter = 0;
     printf( "In second thread...\n" );
  
     while ( true )
     {
         if(Counter&lt;10)
         {
             Counter++;
             Sleep(1000);
         }
         else 
         {
             printf( "Han pasado 10 segundos; Counter:-&gt; %d\n", Counter );
             FunctionBacklightoff(portHandle,bytesTransmitted);//from here doesnt work
             Counter = 0;
         }
  
     }
  
  return 0;
  }
  
  int wmain(void)
  {
      hThread =CreateThread(NULL, 0, solo_thread,NULL ,0, NULL);
      //inicialize rs232 communications...
      retVal = PortOpen(&amp;portHandle, 115200);
      if (!retVal)
      {
          printf("Could not open CoM port");
          getchar();
      }   
      else
      {
  
          printf("CoM port opened successfully");
          retVal = FALSE;
      }
      FunctionBacklightoff(portHandle,bytesTransmitted);//from here works
  }
  </code></pre>