LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Using child-panels as multi threads with an MDI top-level panel

Hi All,

I would like to use several child-panels as separate threads with an 'MDI' top-level panel (like in VC++).
I am using CVI 7.0 & 7.1.

The main problem is that CVI does not allow the Load Panel or any other operation to be performed if the
Child panel is not created on the same thread as the top-level parent panel.

I tested it using the National Instruments "Multi Panel" example that I have altered.
The example creates several child panels in separate threads; they DO NOT have a top-level parent panel.
I set their parent panel handle to be the MainPanel (instead of "0") and I got a run-time error.
I am attaching the example project with the small change in the 'ChartPanel.C' file, (line 32 - you
can't miss it).

I'll appreciate your help,

Regards,
Gil
0 Kudos
Message 1 of 5
(3,797 Views)
Hi Gil,

If I understand your problem correctly, the way I approach such things is to post a deferred procedure call in the main thread that runs your GUI. This is done from the secondary thread that wants to create the panel using PostDeferredCallToThreadAndWait() from the Programmer's Toolbox, or PostDeferredCall() or PostDeferredCallToThread() from the User Interface Library.

The function that you post to the main thread will actually perform the action that can't be done in your secondary thread, e.g. LoadPanelEx().

You can pass parameters via the void* parameter so your deferred procedure knows what to do.

I use this technique when I have a secondary thread that wants to install a popup, and I want that popup to be modal to the rest of the GUI, i.e. I want it to remain at the top of other windows, and lock user input until it is closed. If there is a better way I have not found it.

Hope this helps.

Regards

Jamie
Message 2 of 5
(3,773 Views)
Hi Jamie,

I tried using the PostDeferredCallToThreadAndWait() you suggested and it solved the problem !!
I still don't fully understand it, but I was able to make adjustments to functions that need to use or has any relations to the main thread from the child-thread.
Can you explain it more clearly?

I am attaching the changed example (look at the ChildPanel.C file).


Thank you very much,

Gil
0 Kudos
Message 3 of 5
(3,758 Views)
Hi Gil,

OK, I'll try and explain but I am not an expert in this area.

Some GUI functions need to be called from a particular thread, for example in the thread that the parent panel was created in as you have discovered with LoadPanelEx(). This will be to do with variables stored on the stack local to the particular thread and not accessible by other threads.

Now, CVI may implement this slightly differently to the way I describe here, but it will be basically the same result. When you queue an asynchronous procedure call (APC) under Windows, you tell the Windows Thread Scheduler that when it next runs the thread you have posted the APC to, you want it to execute the APC first, before resuming the thread at the point it left off. This means you can have a particular thread do something at the request of a secondary thread next time it runs, which is what you want to do in your case. I think CVI handles things slightly differently because the PostDeferredProcedureCall() help says the posted function runs the next time RunUserInterface() executes, rather than as soon as the thread that RunUserInterface() is in executes. Any NI engineers may be able to explain what the subtle difference might be.

So, what you are doing in your code is getting your secondary thread to tell your main thread to load a child panel to the main panel, which it can do because it is in the correct context, i.e. it can access the correct variable stack. There are other cases where this is useful. For example, if you want to display a popup, and you want that popup to block user access to the GUI until the popup is closed, you must create it in the main GUI thread. It can be displayed from a secondary thread, but if the user clicks another panel, the popup can get hidden, which is rarely what you would want.

Hope this is of some help.

Regards

Jamie Fraser
Message 4 of 5
(3,724 Views)
Hi Jamie

I would like to thank you again for your exponentiation.
I found it very useful.

Regards,

Gil
0 Kudos
Message 5 of 5
(3,707 Views)