How to know a call back method execution ended - visual-c++

I my using a VC++ DLL from our C# Application .
In DLL there is a method that invoked a call back method....
Before call back method execution end...control comes in the C# progarm....
MY 1 Question is it ok?
But i need a method call from our C# Application after Call back method executed success fully
in DLL.
MY 2 Question is how to know that called Call back Method is success fully executed ???
I am trying with the help of A a Variable keeping inside the call back method by assign, value "1" and in our C# application using while loop checking the value of that Variable
for 1.After that i put the C# Method.....
BUT THIS Approach is not working fine

It all sounds a bit too messy, but I don't really know what you're try to achieve so, I can't tell you it "it is ok".
Here's an idea for your 2nd question:
The method in c++ that is being called from c# can call the method that invokes the callback, and than WaitForSingleObject() waiting for an even to be called (create an Event handle using CreateEvent()).
The call back function can do whatever it is doing, and at the end, you can SetEvent() to the event the original thread is waiting for.
The SetEvent() will release the original thread.
Make sure you always SetEvent() in the callback function, or your thread will get stuck!
Check out: http://msdn.microsoft.com/en-us/library/ms686360%28v=VS.85%29.aspx for synchronization functions.
Good Luck!

Related

Is there a way to get all the executed methods?

I am a bit new to c# so sorry if it is a dumb question, but i need to get all methods, that were executed. I know about Stacktrace class, but if i understand it correctly it only shows "path" through which program had to run to get to the method, where the stacktrace was called. But that doesn't have to show all the methods, that were actually used while the programs run. So can you for example add an event that is triggered every time another method is called during runtime or some other ways?

What is call back function in Node.js?

As per my knowledge, A callback is a function which is called when a task is completed, thus helps in preventing any kind of blocking and a callback function allows other code to run in the meantime. Callback is called when task get completed and is asynchronous equivalent for a function.
But, i am not able to implement in real time, can anyone here can help me to how to write a call back function for simple app.js application? and i need to know how to call a function.

OpenFileDialoug Current Thread Must Be STA before OLE calls made

Can someone explain to me what this error I'm seeing is?
Current thread must be set to single thread apartment (STA) mode before OLE calls can be made.
Specifically, I'm trying to open the SaveFileDialog/OpenFileDialog within C++/CLI on a form.
SaveFileDialog^ saveFileDialog1 = gcnew SaveFileDialog;
saveFileDialog1->ShowDialog();
if (saveFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
{
s = saveFileDialog1->OpenFile();
}
s->Close();
}
The error that is throwing is
An unhandled exception of type 'System.Threading.ThreadStateException' occurred in System.Windows.Forms.dll
Additional information: Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process.
I'm not really familiar with what this error is saying. I know just a bit about threading, but I'm not sure how threading would be an issue here. I've seen some people reference things like STAThread without providing a clear explanation as to what it does, and Microsoft's documentation makes no mention of having this exception thrown when calling SaveFileDialog/OpenFileDialog, or how to handle it.
Thanks!
When you use OpenFileDialog then a lot of code gets loaded into your process. Not just the operating system component that implements the dialog but also shell extensions. Plugins that programmers write to add functionality to Windows Explorer. They work in that dialog as well. There are many, one you are surely familiar with is the extension that makes a .zip file look like a folder.
One thing Microsoft did when they designed the plug-in interface is to not force an extension to be thread-safe. Because that is very hard to do and often a major source of bugs. They made the promise that the thread that creates the plugin instance is also the thread on which any call to the plugin is made. Thus ensuring that the plugin is always used in a thread-safe manner.
That however requires a little help from you. You have to make a promise that your thread, the one that calls OpenFileDialog::Show(), observes the requirements of a single-threaded apartment. STA for short. You make the promise with the [STAThread] attribute on your program's Main() entrypoint. Or if it is a thread that you created yourself then by calling Thread::SetApartmentState() before you start it.
That's just a promise however, you also have to implement what you promised. Takes two things, you promise to never block the thread and you promise to pump a message loop. Application::Run() in a .NET program. The never-block promise ensures that you won't cause deadlock. And the message loop promise says that you implement a solution to the producer-consumer problem.
This should never be a problem, it is very unclear how this got fumbled in your project. Another implicit requirement for a dialog is that it must have an owner. Another window on which it can be on top of. If it doesn't have one then there are very high odds that the user never sees the dialog. Covered by another program's window, the user can only ever find it back by accident. When you create windows then you always also must call Application::Run() so the windows can respond to user input. Use the boilerplate code in a C++/CLI app so this is done correctly.

Low level keyboard Hook not at UI thread

I want to create a good library for keyboard hook. I use a method SetWindowsHookEx and I have noticed that method hookProc, which should be called at any system KeyDown event, is not executed if the main thread of my app is bussy. I think the hook shold be made so, that the other thread would be responsible for it. Is that possible? How can I do it?
Microsoft help page of LowLevelKeyboardProc mentions that
If the hook procedure times out, the system passes the message to the
next hook. However, on Windows 7 and later, the hook is silently
removed without being called. There is no way for the application to know whether the hook is removed.
I suspect this is what’s happening to you. Your HookProc function should be extremely fast: what mine does is just pushing the key event in a std::vector. The real code is executed in another thread.

How to make asynchronous HTTP call with callback in Lua?

I need to make asynchronous HTTP call to my server in order to receive XML response.
After I get the response I will call a [previously specified] function if it is success or some other function if it's an error.
So what I thought about in the first place was coroutines. Unfortunately, after I make the http.get call I cannot yield, as it will wait for the whole thing to finish. I know I can use separate functions to read the response, however I have to wait at least for the first bytes of data in order for this function to be triggered which would allow me to yield. Anyway, for what I wan to do using coroutines doesn't look like the way to go.
Then I've tried calling a C function from lua, creating separate thread to get the XML and then call a function in Lua, however this doesn't work because of lua_state changing after a new thread is created. Before the thread is created I can see 3 parameters on the stack, and after creation of the new thread [I am passing lua_State as the argument] it has only one. Anyway, from what I understand lua_State will be closed once the original cfunction call is finished, so I won't be able to call back.
Since I'm just starting with lua and I'm even less familiar with lua to c bindings I can only hope I'm making some stupid mistakes and it will be easy to solve. For now however I'm stuck with no idea on how to progress further.
The story behind this issue:
I'm porting my game from Cocos2D objective C framework to Cocos2d-X C++ framework. I want to use Lua bindings as I think I will fail to port it to C++. Anyway I want to do it in Lua.
So I've got a scene where someone accesses a list of inventory they have in the game. If the response is immediate they will basically see a window opened with list of inventory. However, if it takes a tad bit longer to get the data [connection issues, sever overload... whatever] screen will fade out and some animation indicating data transfer will be shown on screen. At least this is how it works on the objc version of the game and I want the same thing.
Is there something I have missed during my research, is it possible to do it?
BTW I have seen Lua socket asynchronous calls and it doesn't help me because it still waits for the beginning of the transfer before it will start another one.
Something like Luvit ?
Luvit is an attempt to do something crazy by taking nodeJS's awesome
architecture and dependencies and seeing how it fits in the Lua
language.
This project is still under heavy development, but it's showing
promise. In initial benchmarking with a hello world server, this is
between 2 and 4 times faster than nodeJS.
I was able to do it using https://github.com/Neopallium/lua-llthreads
This seems to work fine on both iOS and Android platforms.

Resources