Visual Basic/VB >> Calling code in DLL that has an "infinite Loop"

by Mike Larson » Sat, 13 Sep 2003 03:41:29 GMT

Hello,

I'm trying to tie some hardware my company builds into a PC. I'm running
Win98 on the PC and can access my hardware C++ functions in a DLL. Right
now I'm checking if the hardware puts up some information then when the PC
gets around to it, it reads the informaiton. This is leading to a lag in
data because the PC can get busy with other things and not read the data
right away.

I'm wondering if there is a way to start a routine in the C++ dll that uses
a near infinite loop to "interrupt" the vb6 program when data comes in by
putting the data into something like text box.

I am not creating driver DLL yet, just a C++ program compiled into a DLL.

Can this be done? Would someone have some advice on accomplishing this?

Thanks.

Mike




Visual Basic/VB >> Calling code in DLL that has an "infinite Loop"

by Stephen J Whiteley » Sat, 13 Sep 2003 04:28:42 GMT

How much of a 'lag' are you talking about?

if your hardware just 'dumps' data into a specific location (your DLL) you
could have your program poll the DLL for new information, say, every 10mS
(or 50 or 100, or whatever).
(i'm presuming this is pretty close to what you've got). If the data comes
in fast, and you can tolerate a few mS lag, but want to get ALL the data,
put the data (in the C++ DLL) into a circular buffer, and when the VB app
gets round to checking for data, it can read the data as a block.

Unless you are doing real-time control, I can't imagine a problem with even
a huge delay of 500mS - which is plenty of time for a VB app to do pretty
much anything with a huge chunk of data.

If you want it 'event driven' then you will have to make a COM DLL that can
create events that your VB app will receive.

BTW, if you are serious about doing this sort of thing, go with NT/2000/XP,
and drop 98.

Stephen J Whiteley



uses




Visual Basic/VB >> Calling code in DLL that has an "infinite Loop"

by Stephen J Whiteley » Sat, 13 Sep 2003 06:40:50 GMT

ave your C program actually go into an 'infinate loop'! Add a Sleep()
command in the loop, and a 'getout' clause so you can end it. (sounds like
your familiar with C, so you may be able to achieve this, but it all depends
on how you are using/creating the DLL and getting it to run).


you could then have your C DLL create a shared memory region where it can
put flags/data, and have your VB program grab the data out of shared memory
on a fixed interval (20mS, say), then you are assured (as assured as anyone
can be) of a maximum lag of 20mS. This really depends on what you are
monitoring.

Alternatively, why can your VB program not access the very same APIs your C
program does? This may be easier, depending on the APIs. Then you can do
your polling in VB.

You can go the route that you yourself suggested, but you somehow have to
get the text into the text box, and trigger the change event...could be
messy, However, you could send a windows message to your VB application
using the SendMessage (or PostMessage) API. Does that sound feasable to you?
It'll require the VB application window being subclassed, so you can get
access to those messages.

Driver development is not an area you want to delve into lightly (can't be
done from VB), and not something you can knock out in a day.

Hope (some) of this gives you a better idea.

SJW


"Mike Larson" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...
how
app.
the
Change
you
10mS
comes
data,
app
pretty
the
data
this?




Visual Basic/VB >> Calling code in DLL that has an "infinite Loop"

by btpanek09 » Sat, 13 Sep 2003 23:06:37 GMT

ou could create a Thread in the DLL that would be your "endless loop"
to monitor the event. Then when the event occurs as detected by the
Thread in the DLL you could use a CallBack function to notify the VB
program from the DLL. I have used the CreateThread inside a DLL to
create a separate process that can monitor an event like a Serial port
and Socket reads.

The Thread has the advantage of not being bound to the VB process.
The VB program is not tied directly to the loop inside the Thread and
will then not lock up waiting for the loop to finsih.

I have gotten the CallBack function to work using sample-code provided
for EnumWindows from MS, however I haven't gotten it to work in a
situation like you have mentioned. But, it should be possible.



"Stephen J Whiteley" <yakATsteelcoder.com> wrote in message news:< XXXX@XXXXX.COM >...


Visual Basic/VB >> Calling code in DLL that has an "infinite Loop"

by Mike Larson » Mon, 15 Sep 2003 22:32:51 GMT

entlemen,

Thank you for your responses. I think I get the idea but I would like to
run my idea here and get some feedback as to what I can expect

The code goes like this :

DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
HWND TheControl
lpString AString [40]; //Null Terminated String

TheControl= lpParameter;
while (near infinite loop) {
//my code
SetWindowText(TheControl, AString; );
SendMessage(TheControl, WM_KEYUP, 0, 0);
}
}

int _stdcall StartTheThread (HWND TheControl) ) {
{
//This routined called from VB App


HANDLE hWorkerThread;
DWORD dwThreadId;

hWorkerThread = CreateThread (NULL,
0,
ThreadProc
TheControl,
0,
&dwThreadId);

I will call StartTheThread from my VB app with the handle to a text box and
start the near infinite loop. When my near infinite loop is ready to
tranfer info it will call the SetWindowText and the SendMeassage call which
will cause at leaset the KeyUp event in the text box and possibly the Change
event.

Now I've read that using the plaine CreateThread may cause some problems if
calling other run time libraries. Am I in this case? Should I use
"_beginthreadex" or AfxBeginThread?

Thanks for the help.

Mike



"Brad Panek" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...
news:< XXXX@XXXXX.COM >...
like
depends
can
memory
anyone
your C
to
you?
be
know
vb
experience
into a
near
have
done
DLL)
VB
with
that
when
lag
the
that
comes in
a




Similar Threads

1. Thread Calls DLL, DLL Loops Infin, Application Stuck

I have a problem with threading in vb.net, it seems if a dll freezes
or falls into an infinite loop, my multithreaded program starts
allocating more threads because it cant use the frozen thread

for example:

declare function testit lib "test.dll" (somearg as integer) as integer
dim thdTest as threading.thread

sub runtest()
  thdTest= new threading.thread(addressof mytest)
  thdTest.start
end sub

sub mytest()
  dim i as integer
  i = testit(0)
  'If the dll call loops infinitely/freezes
  'this thread is dead in the water
  'and cannot be aborted, used, killed
  'and it stays in memory til the 
  'application is forcefully terminated
end sub

sub killtest()
   thdTest.abort 'would not abort the thread
end sub

The DLL must be used, the code in it would take a VERY long time to
recode and the original source is not avaliable (to debug).

If I was able to successfully terminate, the frozen thread, it would
not be a problem, but I can't (dont know of any method).

Any advice? Thanks in advance.

2. API VBA: Infinite Loop

3. Problem with Form.ValidateChildren (infinite validation loop?)

 When attempting to use the form.ValidateChildren() with multiple validation 
errors the code appears to get in an infinite loop among the various 
validation subroutines.  With only one validation error on the form the 
problem does not appear to occur.  Thank you for your help on this issue.

-- 
Jeffrey A Heimberger, MCP
Information Systems Assistant 
Stark Area Regional Transit Authority (SARTA) 
Ph. 330-454-6132 x 526 -- fax 330-454-5476 
Voted #1 Transit in America 

4. Infinite loop querying remote WMI

5. Infinite loop with crystal control viewer

Hi:
Here in my company we have been trying to figure out (for several days
already) what is the weird reason that is doing the crystal control
viewer (crviewer.dll) act this way.

When we are previewing a report (that is using an Access Database as it
source), we call this snippet of code...

    With CRViewer1
        .EnableDrilldown = False
        .EnableGroupTree = False
        .Top = 0
        .Left = 0
        .Height = ScaleHeight
        .Width = ScaleWidth
        .Zoom (100)
        .DisplayBackgroundEdge = True
        .DisplayTabs = False
        .ReportSource = m_crReport
        .ViewReport 'Here, the VB.EXE Process jumps to 100% usage
    End With

Just after we call CRViewer1.ViewReport method, the application hangs
and VB6.EXE jumps in the CPU Usage (verified through Task Manager).

There is no way to close the app (or Stop VB6), the only way to start
again is kill the process and start again... It's getting us crazy!

Here is some information about our environment
- Running on W2K
- Applied latest set of MDAC and OLEDB JET libraries (just in case it
was an Access problem) - Jet 4.0 Service Pack 8 -
- This error happens regardless whether we are in the VB IDE or if the
app is compiled.

Please, if anyone had experienced a similar problem, I would really  be
thankful for your help.

Any idea would be appreciated.

6. Infinite While Loops - Visual Basic/VB

7. Weird infinite loop

Hi!

I have a software server that uses Winsock API and connects to an
Oracle DB.

The server is located in a dual-core 3.6 Pentium D with 2 GB ram.

After some - random - time, sometimes less, sometimes more, the CPU use
of the process goes to 100% (50% of the CPU which is responding) and
the server stops responding (it appears as "not responding")

I have reviewed the code lots of times, all the loops are double -
checked, but I can't find any solution.

Any ideas?

8. XP Setup in an infinite loop - Visual Basic/VB