What is the Best Practice for converting a vb6 standard exe to a activex exe? - multithreading

i have a legacy massive vb6 editor with plenty of 3rd party libraries and controls and recently there is a need to set it up to make it multi thread so i can run a few other forms independently from the main editor form. Basically there's minimum communication between these other forms and the main editor except for running the other forms on a click of a button from the main page.
So from a lot of googling i found a method which converts the current app into a multi threading one by setting it up as an activex exe and adding a class set to global-multi-use to allow this to happen. Now while doing editing and testing via debugging mode, i found that when i exit a lot of weird crash will happen sometimes.
'main.frm - button click call
'On the button click, create a new object
Set obj = CreateObject("MyApp.clsThread")
Call obj.NewThread
'clsThread
' Create a new form and load it to run on a new thread
Public Sub NewThread()
Dim frm As Object
Set frm = New frmDogbert
Load frmDogbert
frm.show
Set frm = Nothing
End Sub
So what do i absolutely must know when i do this,i.e. potential problems,etc?, as i'm fearing that the app seems to be getting more unstable. Or is there a better way to do this?
Updates:
Instead of forcefully hacking my app into a pseudo multithreading app, i've taken the advice from the good people here and refactor out the component into standard exe and reverted back my app to a standard exe and call them via shell. Works beautifully :)
Thanks

Visual Basic 6 is not multi threading in of itself. The difference in the multi-tasking behavior between a ActiveX EXE and a ActiveX DLL is only apparent when referenced from another VB6 EXE. Objects instantiated from a Global Multi-Use Class defined in a ActiveX EXE run in their own context. Object instantiated from a ActiveX DLL are run in the context of the calling EXE.
Now multi-threading can be hacked into VB6 but it is very touchy and has a lot of don't (for example you can't use the stop button in the IDE to stop a debugging session or the program will crash).
What a ActiveX EXE is good is for spawning a separate but related instance that can run separately without halting the main program. Based on your brief description I would say your best best is to keep your EXE as is but more the forms/modules/classes to a separate EXE and have the original EXE reference the ActiveX EXE.
Note that you don't have to compile down to .EXE you can change the extension to .DLL and it is still a activeX EXE. You do this so that the user doesn't mistakely run the ActiveX EXE by itself.

Can you clarify the question a bit? I don't understand why it needs to be multi-threaded? Why can't you just display the forms non-modal, then they will all be visible and responsive at the same time. Unless you have some background processing operating all the time in your main form, which sounds unlikely from your question as it would lock up the main form as well as the others.
Melaos says: Well the main editor frm is the main thing here and I need to allow the user to run additional forms to do some other stuff like uploading things to our server and convert video files to other formats. And these forms use shell to call other exes. And the running time takes quite a while.
In that case I recommend making the other forms into separate EXEs rather than use multithreading, which is difficult. You say there's little communication from the main form to the subforms - it just shows them. Therefore you don't even need to make the subforms ActiveX EXEs. Just make them standard EXEs and shell them from the main form.

Related

what is the use of AfxInitRichEdit2(); in mfc application

I want to know what are the different scenarios where AfxInitRichEdit2() method can be called.
It just loads a specific DLL and with this DLL some window classes are registered into the current process.
In the past calling AfxInitRichEdit and AfxInitRichEdit2 was the way to distinguish between two existing RTF window classes (Version 1.0:RICHED32.DLL and version 2.0: RICHED20.DLL). Just read the MSDN about the Rich Edit window class.

Who is calling VisualStateManager.GoToState behind the scenes?

I am a newbie to xaml and windows app dev so sorry if this question might seem silly.
I created a textbox and in the designer I right clicked it and selected edit template=>edit a copy and put it into my custom dictionary.
In the control template for this textbox I saw visual states like disabled,focused and so on. And I modified them and run the mobile app and observed that my changes work like changing border color when textbox is focused.
But in order for this to work somebody has to call
VisualStateManager.GoToState("Focused")
when the textbox is focused so who is calling this because I don't see any visual transitions in the control template so how is this happening?
The code in the control itself is calling VisualStateManager.GoToState(...) .
When you start implementing your own custom controls, you might subscribe to events you have available and transition states based on your own logic. Here is an example of a custom control with its own two custom states.
https://github.com/xyzzer/WinRTXamlToolkit/blob/master/WinRTXamlToolkit/Controls/WatermarkTextBox/WatermarkTextBox.cs
XAML is a compiled language, and if you've looked extra close, what happens under the hood, is that the class behind your xaml has the same namespace as your xaml code.
This means (for no practical purpose) that compiling your program turns all of that XAML into C# code before then going over to MSIL and eventually execute as a binary program.
Much of the state changes that happen are event based, and TextBox, like all other user controls, will transmit a message and listen to messages. The Page that contains the TextBox will probably be the one that transmits a state change whenever one of it's children gets focus, and as a good control, the TextBox listens for this event and reacts to it.

CodedUI Visual Studio: How to wait for browser load

I was wondering if anyone new how to make the Automation Script in CodedUI wait for the web page to load before continuing on with the script.
So I fill out a form and press the "Continue" button, then the script times out because it gives up waiting. I realise I could use Playback.wait but then I would have code this each time I click continue.... and I have hundreds of scripts! Something similar to Seleniums WaitforBrowserLoad but for CodedUI
C# CodedUI in VS 2012
Any help would be appreciated.
In my experience, Coded UI is excellent about waiting for page loads to be complete before continuing test execution. That said, you can force a wait for a specific control to exist with WaitForControlExist method. This method is a member of the base class of all UI controls, UITestControl.
You could also use:
Playback.PlaybackSettings.WaitForReadyLevel = WaitForReadyLevel.AllThreads;
Sounds like your issue could be fixed if you create a settings file and increase the timeout.
Directions Here: Specifying Test Settings in Visual Studio
You could also call the WaitForControlReady method on the browser window object.
ex:
_browserWindow.WaitForControlReady(60000); //Should wait for 1 min

Visual C Multi-Threading (Please Help)?

I have written a .NET program, using Windows form Application.
My application is fairly simple.
Basically, I have two simple buttons on my form.
When the form is first loaded, I set up a global variable (bool run = true).
And my first button is essentially a very simple while loop.
while(run)
{
// do some code
}
And what I want to do, is have the second button set the value of the boolean to false (bool run = false).
But, once I clicked the first button, I cannot even touch the second button!
I have read quite about this, and I think my solution is to use a multi-threading.
I have found some example codes on line for multi-threading, and I tried to incorporate those, but I don't know why I cannot get it to work. Once I click button #1, there is no way for me to be able to click button #2.
Your UI thread should not have any infinite or a wait-on-something - never! You must not block the UI for anything (other than simple calculations, validations, user confirmation etc). You should make a thread for performing length task, and let that thread communicate to UI thread using asynchronous (or synchronous, if you prefer) communication.
On native Windows GUI application, you can use PostMessage (async), or SendMessage (sync). For .NET GUI applications, you can use Control.BeginInvoke (async), or Control.Invoke (sync).
Please read this article on how this is done.

MFC/3rd party multithreading hang

I'm currently working on a program using MFC. The current third party function starts a thread after an action has been completed using MFC (ie. Checking a checkbox, which starts a MFC thread I believe).
The problem occurs when I check the checkbox, at which point the entire program hangs. I read a few interesting discussions on CProgramming and msdn, it seems that the problem occurs because the new third party thread is calling WaitToSomething() when MFC is updating a control.
Something interesting to note:
When I debug the program, the program hangs (aka. repeatedly calls WaitToRead() ) after I check the checkbox and a new thread is trying to start
When I run the program without debugger, the program is fine UNTIL I switch to another window (ie. Internet browser, Notepad, etc)
My hypothesis:
check to make sure that MFC has finished updating the control before starting a new thread
If anyone has any suggestions or solutions, please leave a comment. Thanks.
Edit:
MFC is not thread-safe at object level (only at class level), so problem occurs when two threads work on the same CButton object.
Q: How do I make it thread safe?
A colleague helped me figure out what the problem was.
The reason why it was hanging is because that the control containing the checkbox is a child dialog, and when it finished updating the message never got passed up to its parent (so when 3rd party thread calls WaitFor(), the MFC thread never completes because a parent dialog thinks its child is still updating the controls).
Fix:
Under 'Properties' in the child dialog's control, set the 'Control' flag to true (and if it has children, set the 'Control Parent' flag to true as well).
Hope this helps.

Resources