Mombu the Programming Forum

Go Back   Mombu the Programming Forum > Programming > Thread and ActiveX - HELP!!!
User Name
Password
REGISTER NOW! Mark Forums Read




Reply Bookmark and Share
1 26th August 08:46
bo
External User
 
Posts: 1
Default Thread and ActiveX - HELP!!!



I have Builder c++ 6.0 I am using QCVideoX activeX to capture frame from internet camera. It is working correct but without Threads.

I would like to have access to Form (to buttons, ...) during capturing a frames.
1. I have added Thread class to my project.
2. I have moved my capturing code to Thread class.

All components are working correct with Thread but ActiveX doesn't. I don't know way?
Using ActiveX is stoping a program and program must be close only by using CTR+F2(Reset Program).
My Code - TImage component - everything is OK:
-----------------------------------------------
do
{
Form1->Image1->Picture->LoadFromFile("tmp.bmp");
Form1->Image1->Repaint();
Form1->Image1->Canvas->FillRect(Rect(0,0,100,100));
Form1->Image1->Repaint();
}while(Form1->Stop==1);
----------------------------------------------------
----------------------------------------------------
With ActiveX - stooping the program
do
{
Form1->QCVideoX1->SaveSingleFrameToFile(m_filename);
Form1->Image1->Picture->LoadFromFile(m_filename);
Form1->Image1->Repaint();
}while(Form1->Stop==1);
------------------------------------------------------------
I don't know what is wrong?
This QCVideoX1->SaveSingleFrameToFile function is traing to save frame to file bud is stooping and the file is empty
HELP me!!!
  Reply With Quote


 


2 26th August 08:46
remy lebeau \(teamb\)
External User
 
Posts: 1
Default Thread and ActiveX - HELP!!!



ActiveX is very sensitive to threading models. You can't just move your
ActiveX code from one thread to another and expect it to work properly
as-is. Depending on how the ActiveX control is designed by its programmer,
it may not be accessible at all. For example, if it is Apartment-threaded
(which it most likely is) and is instantiated in the main thread, then only
the main thread can access it. A worker thread will not be able to access
it without generating errors, unless you either marshal the ActiveX
control's interfaces manually across the threads, or else instantiate the
control in the worker thread instead of in the main thread (and no - I do
not mean in the thread's constructor, it would have to be inside of the
thread's Execute() method itself).


Where exactly are you calling that code from? If it is in a thread, that
that code is not thread-safe.


Again, where exactly is that code being called from?


Gambit
  Reply With Quote


 


3 26th August 08:46
bo
External User
 
Posts: 1
Default Thread and ActiveX - HELP!!!


With ActiveX - stooping the program
//Thread.cpp:
void __fastcall T::Execute()
{
do
{
Form1->QCVideoX1->SaveSingleFrameToFile(m_filename);
Form1->Image1->Picture->LoadFromFile(m_filename);
Form1->Image1->Repaint();
}
while(Form1->Stop==1);
}
----------------------------------------
//Main.cpp:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
T *Process = new T(false);
}

void __fastcall TForm1::Button2Click(TObject *Sender)
{
Stop=0;
}
-----------------------------------------

I don't have a big experiences with THREADS
  Reply With Quote
4 26th August 08:46
remy lebeau \(teamb\)
External User
 
Posts: 1
Default Thread and ActiveX - HELP!!!


Most of that code is not thread-safe. You cannot safely access the GUI
directly in a worker thread. You must synchronize access with the main
thread, so that only the main thread is the one accessing the GUI directly.
Given the simple nature of the above code, everything in that loop has to be
in the main thread, which renders the thread useless. You may as well just
use a timer in the main thread instead, ie:


void __fastcall TForm1::Button1Click(TObject *Sender) {
Timer1->Enabled = true;
}

void __fastcall TForm1::Button2Click(TObject *Sender) {
Timer1->Enabled = false;
}

void __fastcall TForm1::Timer1Timer(TOBject *Sender) {
QCVideoX1->SaveSingleFrameToFile(m_filename);
Image1->Picture->LoadFromFile(m_filename);
Image1->Invalidate();
}


If you must use a thread, then you need to change it to something more like
the following:

void __fastcall T::Execute()
{
TQCVideoX1 *Video = new TQCVideoX1(NULL);
try
{
while( !Terminated ) {
Video->SaveSingleFrameToFile(m_filename);
Synchronize(DisplayImage);
}
}
__finally {
delete Video;
}
}

void __fastcall T:isplayImage() {
Form1->Image1->Picture->LoadFromFile(m_filename);
Form1->Image1->Repaint();
}


private:
T *Process;

void __fastcall TForm1::Button1Click(TObject *Sender) {
Button2->Click();
Process = new T(false);
}

void __fastcall TForm1::Button2Click(TObject *Sender)
{
if( Process ) {
Process->Terminate();
Process->WaitFor();
delete Process;
Process = NULL
}
}


Gambit
  Reply With Quote
5 26th August 08:47
bo
External User
 
Posts: 1
Default Thread and ActiveX - HELP!!!


Hi!

I hope that it will work!

THANK YOU !!!!!
Boguslaw
  Reply With Quote
6 26th August 08:47
bo
External User
 
Posts: 1
Default Thread and ActiveX - HELP!!!


This is working fine for about 20 seconds sometimes more and after that I have an errors:
1.External Exception EEFFACE


I think that I will try with THREAD
Boguslaw
  Reply With Quote
7 26th August 08:47
bo
External User
 
Posts: 1
Default Thread and ActiveX - HELP!!!


Another error - Timer' version is used:

Access violation ...... in module kernel32.dll.......

Boguslaw
  Reply With Quote
8 26th August 08:47
bo
External User
 
Posts: 1
Default Thread and ActiveX - HELP!!!


Now I have tried with Threads!
My code:
--------------------------------------------------------
void __fastcall T::Execute()
{
TQCVideoX *Video;
try
{
while( !Terminated )
{
AnsiString S = "tmp.bmp";
wchar_t * m_filename = new wchar_t[S.WideCharBufSize()];
m_filename = S.WideChar(m_filename,S.WideCharBufSize());
-> Video->SaveSingleFrameToFile(m_filename);
Synchronize(DisplayImage);
}
}
__finally {
delete Video;
}
}
-----------------------------------------------
I have used:
TQCVideoX *Video;
because
TQCVideoX *Video= new TQCVideoX(NULL);
doesn't work!!!

The program stop and I have an error in file UTILCLS.H:
bool operator ! () const { return m_Dispatch == 0; }
I think that I have to use somewere in Thread the methods
//Video->Initialization (0);
//Video->SetVideoDisplay(TRUE);

Maybe You can try to run QCVideoX activex and take all frames to file with using of Threads!!!
Old version of QCVideoX You can find on the page http://www.cs.cmu.edu/~illah/ROBOCOD...CVideoX.ocx.gz


Boguslaw
  Reply With Quote
9 26th August 08:47
remy lebeau \(teamb\)
External User
 
Posts: 1
Default Thread and ActiveX - HELP!!!


EEFFACE means that a C++ exception was allowed to escape into the VCL inner
Delphi code. Delphi can't handle C++ exceptions, so they show up as an
EExternal exception. The only code I have given you that can throw a C++
exception is the call to SaveSingleFrameToFile(), so wrap that in a
try..catch block, ie:

void __fastcall TForm1::Timer1Timer(TOBject *Sender) {
Timer1Timer->Enabled = false;
bool saved = false;
try {
QCVideoX1->SaveSingleFrameToFile(m_filename);
saved = true;
}
catch(const Exception &) {}
catch(...) {}

if( saved ) {
Image1->Picture->LoadFromFile(m_filename);
Image1->Invalidate(); }
Timer1Timer->Enabled = true;
}

Then you need to find out why SaveSingleFrameToFile() is crashing in the
first place.


Gambit
  Reply With Quote
10 26th August 08:47
remy lebeau \(teamb\)
External User
 
Posts: 1
Default Thread and ActiveX - HELP!!!


You are not instantiating the TQCVideoX object at all. You must do so.


Use the WideString class instead:

WideString m_filename = "tmp.bmp";


Since you did not instantiate the object, it is not available to save the frames.


Just saying that it doesn't work says nothing about the actual problem you
are having. Always provide specific details. What EXACTLY happened?


What is the exact error?


Since you haven't provided any details about all about the TQCVideoX class,
there is no way to tell you the proper way to initialize it. Whatever you
are doing with it in the main thread can be done programmably in the worker
thread.

Also, make sure that you call CoInitialize() and CoUninitialize() in the
thread, ie:

private:
TQCVideoX1 *Video;

void __fastcall T::Execute()
{
CoInitialize(NULL);

Video = new TQCVideoX1(NULL);
// initialize Video as needed ...

while( !Terminated ) {
Video->SaveSingleFrameToFile(m_filename);
Synchronize(DisplayImage);
}
}

void __fastcall T:oTerminate()
{
delete Video;
CoUninitialize();
}


Gambit
  Reply With Quote
Reply


Thread Tools
Display Modes


Some other forums that might be of your interest : Development, Ada, Apple script, Assembler, Awk, Beos, Basic, C, C++, C#, C# .net, .net, .net frameworks, Asp .net, Clarion, Clipper, Clos, Clu, Cobol, Coldfusion, Delphi, Dylan, Eiffel, Forth, Fortran, Haskell, Hermes, Icon, Idl, Java, Java script, Jscript .net, Jcl, Linoleum, Lisp, Lotus, Limbo, Logo, Ml, Mumps, Oberon, Postscript, Pop, Pl1, Prolog, Python, Ruby, Pascal, Perl, Php, Rebol, Rexx, Sed, Sather, Scheme, Smalltalk, Tcl, Vhdl, Vrml, Visual basic, Visual basic .net, Yorick, Mysql, Omnis, Postgresql, Xbase, Access, Oracle, Adabas, Berkeley, Btrieve, Filemaker, Gupta, Db2, Informix, Ingres, Mssql server, Object, Olap, Paradox, Rdb, Revelation, Sybase, Theory, Dbase, Html, Java script, Css, Flash, Photoshop, Corel script, Xml, Tech, Beos, Gem, Hp48, Hpux, Linux, Mac, Ms-dos, Os2, Palm, Solaris, Ti99, Windows, Xenix, Aos, Chorus, Geos, Inferno, Lantastic, Lynx, Mach, Minix, Netware, Os9, Parix, Plan9, Psos, Qnx, Xinu, Sco, Unix, Aix, Aux, 386bsd, Bsdi, Freebsd, Netbsd, Openbsd, Ultrix, Amd, Intel, Aptiva, Buz, Deals, Homebuilt, Overclocking, Programming, Extra forums


Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666