473,811 Members | 2,038 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(N ULL,FALSE,FALSE ,TEXT("LDVEVENT "));
hGlobalLDVEvent =OpenEventW(EVE NT_ALL_ACCESS,F ALSE,TEXT("LDVE VENT"));

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 1603
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
WaitForSingleOb ject() or any of the related Wait...() functions for this. If
your application is a managed application, you can also use the
ManualResetEven t and AutoResetEvent classes, and
ThreadPool.Regi sterWaitForSing leObject() 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(N ULL,FALSE,FALSE ,TEXT("LDVEVENT "));
hGlobalLDVEvent =OpenEventW(EVE NT_ALL_ACCESS,F ALSE,TEXT("LDVE VENT"));

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...@xs4al l.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
WaitForSingleOb ject() or any of the related Wait...() functions for this.If
your application is a managed application, you can also use the
ManualResetEven t and AutoResetEvent classes, and
ThreadPool.Regi sterWaitForSing leObject() 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(N ULL,FALSE,FALSE ,TEXT("LDVEVENT "));
hGlobalLDVEvent =OpenEventW(EVE NT_ALL_ACCESS,F ALSE,TEXT("LDVE VENT"));
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 WaitForSingleOb ject() 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 WaitForSingleOb ject() 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.n ospamwrote:
Hi Ben,
I put a WaitForSingleOb ject() 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(NU LL, 0,
(LPTHREAD_START _ROUTINE)TFunct ion(),NULL,CREA TE_SUSPENDED ,
&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=WaitForSingle Object(hGlobalL DVEvent,INFINIT E) ;
ldv_return=NiRe ad(buff);
resetstate(hGlo baleLDVEvent);

}
return 0;
}

Ben
Jul 11 '08 #5
Hi Ben,
I tried to use the CreateThread Function
hThread = CreateThread(NU LL, 0,
(LPTHREAD_START _ROUTINE)TFunct ion(),NULL,CREA TE_SUSPENDED ,
&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(LPVOI D 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(NU LL, 0,
(LPTHREAD_STAR T_ROUTINE)TFunc tion(),NULL,CRE ATE_SUSPENDED ,
&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_SUSPENDE D if you intend to change thread
properties, like priority, before it starts to run. i.e.:

hThread = CreateThread(NU LL, 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(LPVOI D 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(N ULL,FALSE,FALSE ,TEXT("LDVEVENT "));
hGlobalLDVEvent =OpenEventW(EVE NT_ALL_ACCESS,F ALSE,TEXT("LDVE VENT"));
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
2017
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 examples. However the book treats the MS VC++ 6.0 environment and I...as I mentioned am learning .NET. It hasn't been too large a problem so far, I'm have a comp sci degree and have done plenty of c++ so where the two don't match up I've been...
3
1901
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 form app doesn't sign up the event, the application will throw exception "Object reference not set to an instance of an object" during the run time.
3
2081
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 following scenario Server (COM event source) is written in VC++ 6.0 using ATL (event interfaces can be either dispinterface / IDispatch based) Client (COM event receiver) is written in VC++ 2003 using attributed programming and unified event handling
0
821
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. When I have imported the type library VC has created 3 classes, CXXXXXXXX CXXXXXXXXParticipant CIXXXXXXXEvents
4
3848
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 I program that? Thanks in advance
1
1338
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 questions are: 1) is it possible to disable the exception interception mechanism so that the debugger is usable (note that the exception handling to continue with the Exceptions dialogue does not do this)
0
4613
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 2.0 on a XP Pro SP2 Machine
7
8457
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 this installer will install them. But what about the situation where VC runtime has already been installed? In fact it's been installed twice. Although the project was built on a Windows XP system with Visual Studio 2005 SP1 and the results were...
3
1187
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 action occurs on the hardware it needs to signal my application. The DLL instructions tell me to use CreateEvent to make the event, I
0
9605
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10648
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10402
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10135
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9205
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6890
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5554
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3018
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.