Do not receive messages after

I am using JNA to access User32 features (I donโ€™t think this is due to Java here, there is more of a problem with the concept). In my application, I have a Java process that interacts with the Canon SDK. To send any messages, I use the following function:

 private void peekMessage(WinUser.MSG msg) throws InterruptedException { int hasMessage = lib.GetMessage(msg, null, 0, 0); if (hasMessage != 0) { lib.TranslateMessage(msg); lib.DispatchMessage(msg); } Thread.sleep(1); } 

peekMessage is called in a loop and everything works well. Whenever the image is taken from the camera, I get an event and do the rest.

But I observed, say, after about 15 seconds (sometimes never, or sometimes just at startup) no activity with the camera, shooting does not give me any loading event. Later, the entire application becomes unusable because it does not receive any events from the camera.

What could be the reason for this? Please let me know about any other necessary information, I can insert the corresponding code.

Edit:

Initialization:

 Map<String, Integer> options = new HashMap<String, Integer>(); lib = User32.INSTANCE; hMod = Kernel32.INSTANCE.GetModuleHandle(""); options.put(Library.OPTION_CALLING_CONVENTION, StdCallLibrary.STDCALL_CONVENTION); this.EDSDK = (EdSdkLibrary) Native.loadLibrary("EDSDK/dll/EDSDK.dll", EdSdkLibrary.class, options); private void runNow() throws InterruptedException { while (!Thread.currentThread().isInterrupted()) { Task task = queue.poll(); if (task != null) { int taskResult = task.call(); switch (taskResult) { case (Task.INITIALIZE_STATE): break; case (Task.PROCESS_STATE): break; case (Task.TERMINATE_STATE): { //queue.add(new InitializeTask()); Thread.currentThread().interrupt(); break; } default: ; } } getOSEvents(); } } WinUser.MSG msg = new WinUser.MSG(); private void getOSEvents() throws InterruptedException { if (isMac) { receiveEvents(); } else { peekMessage(msg); } } 

Above, whenever I get an event of my camera, I add it to the queue , and in each loop I check the queue to handle any Task . Another important piece of information: this is a process running on cmd and not having a window. I just need events from my camera and nothing more.

Code in which I register callback functions:

 /** * Adds handlers. */ private void addHandlers() { EdSdkLibrary.EdsVoid context = new EdSdkLibrary.EdsVoid(new Pointer(0)); int result = EDSDK.EdsSetObjectEventHandler(edsCamera, new NativeLong(EdSdkLibrary.kEdsObjectEvent_All), new ObjectEventHandler(), context).intValue(); //above ObjectEventHandler contains a function "apply" which is set as callback function context = new EdSdkLibrary.EdsVoid(new Pointer(0)); result = EDSDK.EdsSetCameraStateEventHandler(edsCamera, new NativeLong(EdSdkLibrary.kEdsStateEvent_All), new StateEventHandler(), context).intValue(); //above StateEventHandler contains a function "apply" which is set as callback function context = new EdSdkLibrary.EdsVoid(new Pointer(0)); result = EDSDK.EdsSetPropertyEventHandler(edsCamera, new NativeLong(EdSdkLibrary.kEdsStateEvent_All), new PropertyEventHandler(), context).intValue(); //above PropertyEventHandler contains a function "apply" which is set as callback function } 
+6
source share
2 answers

You receive ALL messages from ALL windows belonging to this stream, including all mouse movements, paints, etc. If you do not call this function, your message queue will overflow and cause the behavior that you describe.

A sleep that you definitely do not want, as GetMessage gives if the message is not expecting.

So if this thread (s) has a normal message flow (i.e. GetMessage / DispatchMessage) somewhere else, then you should let this pump do most of the work, perhaps use wMsgFilterMin, wMsgFilterMax, just to get the event message that you require; or even better in this case use peekmessage with PM_NOREMOVE (then you need your dream call as peekmessage returns immediately).

Alternatively, specify the hWnd of the window that generates the event to reduce the workload.

Use spy ++ to see which windows belong to this stream and which messages are generated.

To answer this question, please provide answers to the following questions: what else does this stream do and what windows does it have; is this message only one pumping, or are you calling the SDK API, where can it also forward messages?

+1
source

There is an OpenSource project that wraps EDSDK with JNA, and it has a version of your code that probably works better:

https://github.com/kritzikratzi/edsdk4j/blob/master/src/edsdk/api/CanonCamera.java#L436

Unfortunately, this does not depend on the platform and specifically how everything works on windows. I am currently trying to get a version of MacOS that works on:

https://github.com/WolfgangFahl/edsdk4j

0
source

Source: https://habr.com/ru/post/944621/


All Articles