473,396 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Event handling in VC++ 2005 .NET

Ben
Hi

I'm trying to setup a project that uses events. I'm creating a windows
forms based application.

The application interfaces to a DLL that controls a piece of external
hardware, whenever an action occurs on the hardware it needs to signal
my application.

The DLL instructions tell me to use CreateEvent to make the event, I
then pass the event handle to the DLL. This side of things seems to
work correctly and comes back with no errors.

What I'm having trouble with is how to tell the application to execute
this event. The event needs to read the hardware and then return to
the main program.

I could use a timer and continually poll the hardware but as the
hardware could signal at any time there is the danger I wouldn't be
able to pick up the messages quickly enough.

When an action is required the DLL uses SetEvent to signal the app.

My event creation code is:
(this is performed inside a button control on Form1)
HANDLE hGlobalLDVEvent;
hGlobalLDVEvent=CreateEventW(NULL,FALSE,FALSE,TEXT ("LDVEVENT"));
hGlobalLDVEvent=OpenEventW(EVENT_ALL_ACCESS,FALSE, TEXT("LDVEVENT"));

this returns with 0x268 which I believe this indicated the event is
properly
created?

What do I need to do now?

Thanks for your help

Ben
Jul 11 '08 #1
7 1581
Ben wrote:
I'm trying to setup a project that uses events. I'm creating a windows
forms based application.

The application interfaces to a DLL that controls a piece of external
hardware, whenever an action occurs on the hardware it needs to signal
my application.

The DLL instructions tell me to use CreateEvent to make the event, I
then pass the event handle to the DLL. This side of things seems to
work correctly and comes back with no errors.

What I'm having trouble with is how to tell the application to execute
this event. The event needs to read the hardware and then return to
the main program.
You're confusing "event" with "event handler". An event is a Win32 object
that can be signaled to indicate something has happened or should happen. An
event handler is a higher-level concept (not present in the Win32 API
itself) for "function that executes in response to an event".

What you want to do is wait for the event to become signaled. You can use
WaitForSingleObject() or any of the related Wait...() functions for this. If
your application is a managed application, you can also use the
ManualResetEvent and AutoResetEvent classes, and
ThreadPool.RegisterWaitForSingleObject() to register a wait without tying up
a thread.
I could use a timer and continually poll the hardware but as the
hardware could signal at any time there is the danger I wouldn't be
able to pick up the messages quickly enough.
Don't use polling, it's evil. You'll peg the processor without doing
anything meaningful.
When an action is required the DLL uses SetEvent to signal the app.
This will set the event to the signaled state and release any threads
waiting on the event.
My event creation code is:
(this is performed inside a button control on Form1)
HANDLE hGlobalLDVEvent;
hGlobalLDVEvent=CreateEventW(NULL,FALSE,FALSE,TEXT ("LDVEVENT"));
hGlobalLDVEvent=OpenEventW(EVENT_ALL_ACCESS,FALSE, TEXT("LDVEVENT"));

this returns with 0x268 which I believe this indicated the event is
properly
created?
This is the value of the event handle; it it failed, it would have returned
NULL. Don't forget to check for this.

--
J.
Jul 11 '08 #2
Ben
On Jul 11, 12:46*pm, Jeroen Mostert <jmost...@xs4all.nlwrote:
Ben wrote:
I'm trying to setup a project that uses events. I'm creating a windows
forms based application.
The application interfaces to a DLL that controls a piece of external
hardware, whenever an action occurs on the hardware it needs to signal
my application.
The DLL instructions tell me to use CreateEvent to make the event, I
then pass the event handle to the DLL. This side of things seems to
work correctly and comes back with no errors.
What I'm having trouble with is how to tell the application to execute
this event. The event needs to read the hardware and then return to
the main program.

You're confusing "event" with "event handler". An event is a Win32 object
that can be signaled to indicate something has happened or should happen.An
* event handler is a higher-level concept (not present in the Win32 API
itself) for "function that executes in response to an event".

What you want to do is wait for the event to become signaled. You can use
WaitForSingleObject() or any of the related Wait...() functions for this.If
your application is a managed application, you can also use the
ManualResetEvent and AutoResetEvent classes, and
ThreadPool.RegisterWaitForSingleObject() to register a wait without tyingup
a thread.
I could use a timer and continually poll the hardware but as the
hardware could signal at any time there is the danger I wouldn't be
able to pick up the messages quickly enough.

Don't use polling, it's evil. You'll peg the processor without doing
anything meaningful.
When an action is required the DLL uses SetEvent to signal the app.

This will set the event to the signaled state and release any threads
waiting on the event.
My event creation code is:
(this is performed inside a button control on Form1)
HANDLE hGlobalLDVEvent;
hGlobalLDVEvent=CreateEventW(NULL,FALSE,FALSE,TEXT ("LDVEVENT"));
hGlobalLDVEvent=OpenEventW(EVENT_ALL_ACCESS,FALSE, TEXT("LDVEVENT"));
this returns with 0x268 which I believe this indicated the event is
properly
created?

This is the value of the event handle; it it failed, it would have returned
NULL. Don't forget to check for this.

--
J.- Hide quoted text -

- Show quoted text -
Thanks for your reply.

I put a WaitForSingleObject() function in the routine and this worked;
it stopped the execution of the program until the hardware sent a
signal, but it tied up the rest of the application.
What I would like to know is:
Can I set up another thread that will wait for the signal(from the
hardware) read the hardware and then return to waiting for the next
signal?
Whilst it is doing that I need the main application window to still
allow me update control,etc.
What commands should I use?

Thanks your help, this is my first attempt at multithreaded apps. I'm
more used to embedded systems and using interrupts to jump to specific
routines.

Ben
Jul 11 '08 #3
Hi Ben,
I put a WaitForSingleObject() function in the routine and this worked;
it stopped the execution of the program until the hardware sent a
signal, but it tied up the rest of the application.
What I would like to know is:
Can I set up another thread that will wait for the signal(from the
hardware) read the hardware and then return to waiting for the next
signal?
Yes, that is the correct approach.
Whilst it is doing that I need the main application window to still
allow me update control,etc.
What commands should I use?
Use PostMessage to post update information to your main window.
Do not use HWNDs of your main thread directly in your background
thread. HWNDs can only be accessed by the thread which created it.

How these message look like depends on your app design.
They might be as simple as invalidating your main window to force
redraws which takes new data updated by your background thread.
Or you might post control specific messages to update some control
state or you might invent your own messages to send some custom
data structure to your main window which has to be processed in the
window proc of the receiving window.

--
SvenC
Jul 11 '08 #4
Ben
On Jul 11, 1:39*pm, "SvenC" <Sv...@nospam.nospamwrote:
Hi Ben,
I put a WaitForSingleObject() function in the routine and this worked;
it stopped the execution of the program until the hardware sent a
signal, but it tied up the rest of the application.
What I would like to know is:
Can I set up another thread that will wait for the signal(from the
hardware) read the hardware and then return to waiting for the next
signal?

Yes, that is the correct approach.
Whilst it is doing that I need the main application window to still
allow me update control,etc.
What commands should I use?

Use PostMessage to post update information to your main window.
Do not use HWNDs of your main thread directly in your background
thread. HWNDs can only be accessed by the thread which created it.

How these message look like depends on your app design.
They might be as simple as invalidating your main window to force
redraws which takes new data updated by your background thread.
Or you might post control specific messages to update some control
state or you might invent your own messages to send some custom
data structure to your main window which has to be processed in the
window proc of the receiving window.

--
SvenC
Ok,

I tried to use the CreateThread Function
hThread = CreateThread(NULL, 0,
(LPTHREAD_START_ROUTINE)TFunction(),NULL,CREATE_SU SPENDED ,
&IDThread);

In my form_load routine, but don't seem to have it right. It
immediatly executes TFunction but the program doesn't continue to
execute the rest of my code. Am I correct in thinking that each thread
should run concurrently? or am I missing something?

I was hoping to have something along the lines of
DWORD TFunction()
{
unsigned int x;
while (TRUE)
{
x=WaitForSingleObject(hGlobalLDVEvent,INFINITE) ;
ldv_return=NiRead(buff);
resetstate(hGlobaleLDVEvent);

}
return 0;
}

Ben
Jul 11 '08 #5
Hi Ben,
I tried to use the CreateThread Function
hThread = CreateThread(NULL, 0,
(LPTHREAD_START_ROUTINE)TFunction(),NULL,CREATE_SU SPENDED ,
&IDThread);
Only specify TFunction without parenthesis. That hands in the address of
that function
and does not execute it.
In my form_load routine, but don't seem to have it right. It
immediatly executes TFunction but the program doesn't continue to
execute the rest of my code. Am I correct in thinking that each thread
should run concurrently? or am I missing something?

I was hoping to have something along the lines of
DWORD TFunction()
Use DWORD WINAPI TFunction(LPVOID p)

p will contain what you gave a 4th param to CreateThread.

When you use the CRT you might need to use _beginthreadex to initialize per
thread CRT state correctly.

--
SvenC

Jul 11 '08 #6
SvenC wrote:
Hi Ben,
>I tried to use the CreateThread Function
hThread = CreateThread(NULL, 0,
(LPTHREAD_START_ROUTINE)TFunction(),NULL,CREATE_S USPENDED ,
&IDThread);

Only specify TFunction without parenthesis. That hands in the address
of that function
and does not execute it.
And remove the (LPTHREAD_START_ROUTINE) cast. If you defined the function
right, no cast is needed. The address-of (&) operator is optional but
recommended. Only specify CREATE_SUSPENDED if you intend to change thread
properties, like priority, before it starts to run. i.e.:

hThread = CreateThread(NULL, 0, &ThreadProc, NULL, 0, &IDThread);

>
>In my form_load routine, but don't seem to have it right. It
immediatly executes TFunction but the program doesn't continue to
execute the rest of my code. Am I correct in thinking that each
thread should run concurrently? or am I missing something?

I was hoping to have something along the lines of
DWORD TFunction()

Use DWORD WINAPI TFunction(LPVOID p)

p will contain what you gave a 4th param to CreateThread.

When you use the CRT you might need to use _beginthreadex to
initialize per thread CRT state correctly.

Jul 18 '08 #7
My event creation code is:
(this is performed inside a button control on Form1)
HANDLE hGlobalLDVEvent;
hGlobalLDVEvent=CreateEventW(NULL,FALSE,FALSE,TEXT ("LDVEVENT"));
hGlobalLDVEvent=OpenEventW(EVENT_ALL_ACCESS,FALSE, TEXT("LDVEVENT"));
After CreateEvent, you do not also need to do OpenEvent. OpenEvent is
designed for using an event created by name in a different program. You
also don't need to name your event, because you and the DLL are sharing it
using the handle, not the name.
Jul 18 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: TogaKangaroo | last post by:
Hi, I'm trying to get intimate with my new copy of the .NET Pro developing environment, specifically C++ Visual Studio. I have a book (VC++ in 21 days) that I am going through following their...
3
by: VC | last post by:
I created an usercontrol ACTRL. and I fired an event from the ACTRL. in my form application, do all ACTRL controls have to do "+=" to sign up the event? my problem is if the ACTRL control in the...
3
by: Ashok Kumar K | last post by:
Hi all, Where can I get some insight on using the __hook, __unhook, event_source and event_receiver for specifically COM events. The documentation given in MSDN is very minimal. I have the...
0
by: Filippo Bettinaglio | last post by:
Hya, VC++ 2005, I have imported a type library in my MFC application (.exe) i can create the object and execute the methods. But I need to catch an event, i do how how to interface with it. ...
4
by: tlemcenvisit | last post by:
Hi, I program a windows form application with VC++.NET, I wish to program an event which occurs when the user uses the mouse wheel in a pictureBox control. Which event should I use? And how can...
1
by: simon | last post by:
I'm using VC++ 2003. My code throws lots of exceptions but the debugger's handling of these makes the program (and hence debugging process) unusably slow; this was not the case in VC++ 6. So my...
0
by: =?Utf-8?B?YW5rMmdv?= | last post by:
Hi, Thanks in advance for reading this. Not sure where to post this question, but I hope someone in here can help. Trying to write to Event Log in VS 2005 (.NET 2.0) using Enterprise Library...
7
by: Norman Diamond | last post by:
A project depends on VC runtime from Visual Studio 2005 SP1, and DotNet Framework 2. Options are set in the setup project properties, so if these two dependencies are not already installed then...
3
by: Ben | last post by:
Hi I'm trying to setup a project that uses events. I'm creating a windows forms based application. The application interfaces to a DLL that controls a piece of external hardware, whenever an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.