| View previous topic :: View next topic |
| Author |
Message |
YAZ Guest
|
Posted: Sun Nov 12, 2006 8:24 pm Post subject: How to interrupt a function in DLL |
|
|
Hello,
I made an application composed of an exe with BCB for the GUI and a DLL
with VC for the numerical computation.
The EXE must be able to interrupt the DLL fonctions.
in the DLL a have a globale variable called interrpted that tests if
the EXE wants to interrupt the execution.
//----------- DLL Code---------------
int interrputed;
int * init_dll()
{
return &interrputed;
}
void execute_dll()
{
interrputed=0;
for (int i=0;i <N;i++)
{
doSomeComputationwith(i);
if (interrputed ==1)
return;
}
}
//-----------------------------------------
in the EXE I have a Form with a start_button and a stop_button.
//------------------EXE Code -----------------------------
int *bcb_interrupted; // pointer to the DLL global variable
_fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
bcb_interrupted = init_dll();
}
void __fastcall TForm1::StartClick(...)
{
try {
Screen->Cursor = crHourGlass;
execute_dll();
}
catch(...)
{
Screen->Cursor = crDefault;
}
Screen->Cursor = crDefault;
}
void __fastcall TForm1::StopClick(...)
{
*bcb_interrupted=1; // set the DLL global variable to 1
}
//------------------------------------------------------------
My problem is that the Stop_button event is not called before the
Start_button event returns.
I thought about two ways to solve this :
1- make a simple function in the EXE that do a
Application->ProcessMessages() and call it inside the DLL loop. This
works but it slows down the dll_execute() with a factor of about 100.
2- use TThread to call dll_execute() inside a thread. But i don't know
if this approach will work.
Can you advice me about the implementation of this approache if you
think that it will work.
Any idea about another approache to interrput the dll function without
slowing the execute_dll() function?
Sincereley, |
|
| Back to top |
|
 |
YAZ Guest
|
Posted: Mon Nov 13, 2006 3:01 am Post subject: Re: How to interrupt a function in DLL |
|
|
I found a solution that works with TThread.
I put the DLL function call inside the Execute method of TMyThread.
//---------Unit1.h----------------
#include <Classes.hpp>
class TMyThread : public TThread
{
protected:
void __fastcall Execute();
public:
__fastcall TMyThread();
};
//----------Unit1.cpp------------------------------
#include "Unit1.h"
__fastcall TMyThread::TMyThread()
: TThread(false)
{
FreeOnTerminate = true;
}
void __fastcall TMyThread::Execute()
{
dll_execute();
}
//---------------------------------
In the EXE I create an instance of TMyThread in the StartClick event :
void __fastcall TForm1::StartClick(...)
{
//execute_dll();
MyThread = new TMyThread();
}
This approache works fine. But I have a new problem:
I have more than 10 functions in the EXE GUI to display graphical plot
of the data processed by the DLL. Now, graphical plots are cleared when
MyThread finish execution.
So calling dll_execute() from a MyThread is a bad solution for me.
Any suggestion ? |
|
| Back to top |
|
 |
YAZ Guest
|
Posted: Mon Nov 13, 2006 10:49 pm Post subject: Re: How to interrupt a function in DLL |
|
|
I found a solution that doesn't slow the loop using the GetTickCount
which is very fast compared to clock() function !!.
void execute_dll()
{
interrputed=0;
unsigned long tic=GetTickCount();
for (int i=0;i <N;i++)
{
if ( (GetTickCount()- tic)> 1000) // every second
{
CallEXEApplicationProcessMessages();
tic=GetTickCount();
}
if (interrputed ==1)
return;
doSomeComputationwith(i);
}
} |
|
| Back to top |
|
 |
|