Board index » cppbuilder » Threads in ActiveForms using Synchronize()

Threads in ActiveForms using Synchronize()

I have an ActiveForm (I embed in a html doc) that uses a thread to download
images using Indy HTTP. I then update a TImage (via a refrence I pass to the
thread) with the dloadedJpeg. I believe I should be using Synchronize() to
update the TImage, but if I use Synchronize the thread  just hangs. I am
having some occasional exceptions happening that are hard to track down, I
am wondering if this is not a side effect of not using synchronization - but
I can't get it to work WITH Synchronize??? Very stumped by this one! Here is
a simplified version of what I have:
...
while (!Terminated)
      {
         JPG = NULL;

         if (Terminated)
         break;

          try
          {
             MemoryStream->Clear();
             MemoryStream->Seek(0,0);
             // get the jpeg
             HTTP->Get(URL,MemoryStream);
             if (Terminated)
                break;
             MemoryStream->Seek(0,0);
             JPG = new TJPEGImage;
             JPG->LoadFromStream(MemoryStream);
            //-----------------------------------------
             // Synchronize(UpdateImage); <-----Never gets called!!! It just
hangs
            UpdateImage(); // hmm not the best way I think..
            //-----------------------------------------
             ::Sleep(10);
          }
          catch(Exception& e) {
                  // just skip to the next one
          }
            ...
     }
...

void __fastcall TImageThread::UpdateImage()
{
    try {
        ImageRef->Picture->Assign(JPG);
    }
    catch(...) {
        ; // swallow all exceptions
    }

Quote
}

 

Re:Threads in ActiveForms using Synchronize()


I have an ActiveForm (I embed in a html doc) that uses a thread to download
images using Indy HTTP. I then update a TImage (via a refrence I pass to the
thread) with the dloadedJpeg. I believe I should be using Synchronize() to
update the TImage, but if I use Synchronize the thread  just hangs. I am
having some occasional exceptions happening that are hard to track down, I
am wondering if this is not a side effect of not using synchronization - but
I can't get it to work WITH Synchronize??? Very stumped by this one! Here is
a simplified version of what I have:
...
while (!Terminated)
      {
         JPG = NULL;

         if (Terminated)
         break;

          try
          {
             MemoryStream->Clear();
             MemoryStream->Seek(0,0);
             // get the jpeg
             HTTP->Get(URL,MemoryStream);
             if (Terminated)
                break;
             MemoryStream->Seek(0,0);
             JPG = new TJPEGImage;
             JPG->LoadFromStream(MemoryStream);
            //-----------------------------------------
             // Synchronize(UpdateImage); <-----Never gets called!!! It just
hangs
            UpdateImage(); // hmm not the best way I think..
            //-----------------------------------------
             ::Sleep(10);
          }
          catch(Exception& e) {
                  // just skip to the next one
          }
            ...
     }
...

void __fastcall TImageThread::UpdateImage()
{
    try {
        ImageRef->Picture->Assign(JPG);
    }
    catch(...) {
        ; // swallow all exceptions
    }

Quote
}

Re:Threads in ActiveForms using Synchronize()


I have an ActiveForm (I embed in a html doc) that uses a thread to download
images using Indy HTTP. I then update a TImage (via a refrence I pass to the
thread) with the dloadedJpeg. I believe I should be using Synchronize() to
update the TImage, but if I use Synchronize the thread  just hangs. I am
having some occasional exceptions happening that are hard to track down, I
am wondering if this is not a side effect of not using synchronization - but
I can't get it to work WITH Synchronize??? Very stumped by this one! Here is
a simplified version of what I have:
...
while (!Terminated)
      {
         JPG = NULL;

         if (Terminated)
         break;

          try
          {
             MemoryStream->Clear();
             MemoryStream->Seek(0,0);
             // get the jpeg
             HTTP->Get(URL,MemoryStream);
             if (Terminated)
                break;
             MemoryStream->Seek(0,0);
             JPG = new TJPEGImage;
             JPG->LoadFromStream(MemoryStream);
            //-----------------------------------------
             // Synchronize(UpdateImage); <-----Never gets called!!! It just
hangs
            UpdateImage(); // hmm not the best way I think..
            //-----------------------------------------
             ::Sleep(10);
          }
          catch(Exception& e) {
                  // just skip to the next one
          }
            ...
     }
...

void __fastcall TImageThread::UpdateImage()
{
    try {
        ImageRef->Picture->Assign(JPG);
    }
    catch(...) {
        ; // swallow all exceptions
    }

Quote
}

Other Threads