Main Restorations Software Audio/Jukebox/MP3 Everything Else Buy/Sell/Trade
Project Announcements Monitor/Video GroovyMAME Merit/JVL Touchscreen Meet Up Retail Vendors
Driving & Racing Woodworking Software Support Forums Consoles Project Arcade Reviews
Automated Projects Artwork Frontend Support Forums Pinball Forum Discussion Old Boards
Raspberry Pi & Dev Board controls.dat Linux Miscellaneous Arcade Wiki Discussion Old Archives
Lightguns Arcade1Up Try the site in https mode Site News

Unread posts | New Replies | Recent posts | Rules | Chatroom | Wiki | File Repository | RSS | Submit news

  

Author Topic: Calling all DEVS.... ShellandWait broken?  (Read 4875 times)

0 Members and 1 Guest are viewing this topic.

Howard_Casto

  • Idiot Police
  • Trade Count: (+1)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 19427
  • Last login:Yesterday at 11:01:57 am
  • Your Post's Soul is MINE!!! .......Again??
    • The Dragon King
Calling all DEVS.... ShellandWait broken?
« on: February 12, 2008, 05:03:24 am »
I hadn't noticed this because I keep my pc on 24/7 but there seems to be a very odd interaction going on between mame and the tried and true "shellandwait" function.  For those who are confused with the term (it's called other things) a shellandwait function is when you launch a program with the "shell" command, capturing the PID as it's launched and then use a waitforsingleobject api call to wiat until the app is done. 

MAME (and I think ONLY mame) is doing something really odd.  Upon first boot, the first app to launch mame via shellandwait hangs.  ANY app that uses a shellandwait hangs when trying to launch mame on a first boot.  To make sure that it wasn't shoddy programming on my part, I downloaded a demo app that only does shell and wait and confirmed that it hangs too.  Mame actually gets launched, but it sits there in a locked state, it must get through some of it's code because the title in task manager is changed to reflect the game that's supposed to be running.

Can anyone confirm for me that this is the case and I don't have something screwey going on with my pc?  I'm runing xp sp2 with the latest version of mame.  I tried a slighly earlier version of mame and it hung as well.  I would try more, but after the 12th or so reboot (as I said, it only happens upon the first call, so you have to reboot to test) I've gotten sick of debugging this one myself. 

In case there is any question of the code I'm referring to, here is the vb6 source to a typical shellandwait function (and the one I'm using).  This is vb code, but this can also be done in vc++ or practically any langauge.  Figuring out if this is a mame thing, a vb6 thing or a shellandwait thing would be nice as this is a problem that could potentially effect a lot of applications. 

=================================================================================
Option Explicit

Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Const SYNCHRONIZE = &H100000
Private Const INFINITE = -1&

' Start the indicated program and wait for it
' to finish, hiding while we wait.
Private Sub ShellAndWait(ByVal program_name As String)
Dim process_id As Long
Dim process_handle As Long

    ' Start the program.
    On Error GoTo ShellError
    process_id = Shell(program_name, vbNormalFocus)
    On Error GoTo 0

   

    ' Wait for the program to finish.
    ' Get the process handle.
    process_handle = OpenProcess(SYNCHRONIZE, 0, process_id)
    If process_handle <> 0 Then
        WaitForSingleObject process_handle, INFINITE
        CloseHandle process_handle
    End If

       Exit Sub

ShellError:
    MsgBox "Error running '" & program_name & _
        "'" & vbCrLf & Err.Description
End Sub

' Start the program.
Private Sub cmdRun_Click()
Dim cmd As String
    ChDrive "h:"
    ChDir "h:\mame\"
   cmd = "mame.exe pacman"

    ' Shell this command and wait for it to finish.
    ShellAndWait cmd

   
End Sub
=================================================================================

Please anyone with the ability to do so test this asap!


youki

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 1612
  • Last login:November 19, 2016, 01:07:33 pm
  • Atomic Front End Creator
    • Atomic Front End
Re: Calling all DEVS.... ShellandWait broken?
« Reply #1 on: February 12, 2008, 05:41:44 am »
I can not test.

but from very quick first view.

Why do you call Shell , then OpenProcess , the waitforsingleobject.


simply call:

CreateProcess (not openProcess)   and the waitforsingobject   , it could solve your problem.



headkaze

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2943
  • Last login:August 14, 2023, 02:00:48 am
  • 0x2b|~0x2b?
Re: Calling all DEVS.... ShellandWait broken?
« Reply #2 on: February 12, 2008, 08:06:13 am »
Check out this code I wrote for MinWah. It includes some functions handy for launching programs from a FE in VB6.
« Last Edit: February 12, 2008, 08:09:03 am by headkaze »

Minwah

  • Trade Count: (+3)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 7662
  • Last login:January 18, 2019, 05:03:20 am
    • MAMEWAH
Re: Calling all DEVS.... ShellandWait broken?
« Reply #3 on: February 12, 2008, 12:55:01 pm »
Haven't got the latest (or anything too close) version of Mame...will download it and give it a try...

Howard_Casto

  • Idiot Police
  • Trade Count: (+1)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 19427
  • Last login:Yesterday at 11:01:57 am
  • Your Post's Soul is MINE!!! .......Again??
    • The Dragon King
Re: Calling all DEVS.... ShellandWait broken?
« Reply #4 on: February 12, 2008, 02:56:32 pm »
While I appreciate the advice youki and hk the point of this isn't to find a different way to launch programs (I know of at least 3, one of which I use regularly) it's to figure out why a bit of code myself and countless other people have been using for years without fail has suddenly stopped working. 

Just for the record, shell is used because it's faster, requires less headers (no api to call or constants to define) and takes less memory.  Not by much, but every bit of optimization counts. 

I do need to try one of my apps that uses create process though to see if this is vb's shell function acting up or something else so I'll try that and get back to you. 
« Last Edit: February 12, 2008, 03:24:25 pm by Howard_Casto »

Howard_Casto

  • Idiot Police
  • Trade Count: (+1)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 19427
  • Last login:Yesterday at 11:01:57 am
  • Your Post's Soul is MINE!!! .......Again??
    • The Dragon King
Re: Calling all DEVS.... ShellandWait broken?
« Reply #5 on: February 12, 2008, 03:32:08 pm »
Crap guys this is bad!  I surely hope this is just something odd going on with my personal computer because this problem occurs with createprocess as well! I assumed like youki that createprocess, which is certainly more robust, would work just fine, but it isn't.  It freezes just like the regular old shell command. 

I also discovered one more piece to the puzzle leading me to think it's mame and not the programming.  If you launch mame once via shell or createprocess WITHOUT using waitforsingleobject and then try it again with a wait it'll work just fine on first boot.  It's like mame has some sort of odd-ball security setting that's throwing everything off. 


I need to unfortunately (UGH MORE REBOOTS) test a little more and see if I can replicate the issue with an app other than mame.  I would still appreciate it if anyone would try this.

Howard_Casto

  • Idiot Police
  • Trade Count: (+1)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 19427
  • Last login:Yesterday at 11:01:57 am
  • Your Post's Soul is MINE!!! .......Again??
    • The Dragon King
Re: Calling all DEVS.... ShellandWait broken?
« Reply #6 on: February 12, 2008, 03:41:00 pm »
Ok maybe I'm not crazy, but in this case I wish I were.  Tried the exact same functions with notepad and they did fine.  Tried em with mame again (literally just changing the command line) and it freezes again! 

So the problem is either specific to mame, or specific to programs like mame (command-line based?  DirectX enabled?  Fullscreen?)

youki

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 1612
  • Last login:November 19, 2016, 01:07:33 pm
  • Atomic Front End Creator
    • Atomic Front End
Re: Calling all DEVS.... ShellandWait broken?
« Reply #7 on: February 12, 2008, 03:46:44 pm »
unfortunaly i don't have time to test for you now.

But if you want try , AtomicFe  use CreateProcess and waitsingleobject to run mame.

So may be do a quick test with Atomic to see if my code have the same effect with your mame or in your context.

headkaze

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2943
  • Last login:August 14, 2023, 02:00:48 am
  • 0x2b|~0x2b?
Re: Calling all DEVS.... ShellandWait broken?
« Reply #8 on: February 12, 2008, 04:37:33 pm »
You should avoid using WaitForSingleObject with the INFINITE paramter. Normally you would place something like that in a Thread but since VB6 doesn't support threading you should place it in a loop. If you have a form open you need to process the message pump otherwise it can cause the form to hang.
Eg.

Code: [Select]
While Not bDone And Not bFinished ' do while our launched app and main app is still running
   WaitForSingleObject pi.hProcess, 500 ' wait a while
   GetExitCodeProcess pi.hProcess, lpExitCode ' get the process exit code
   If (lpExitCode <> STILL_ACTIVE) Then bDone = True ' check if process is still active
   ' If (GetAsyncKeyState(VK_ESCAPE) And &H8001) Then bDone = True
   Call FastDoEvents ' DoEvents
Wend

Just for the record, shell is used because it's faster, requires less headers (no api to call or constants to define) and takes less memory.  Not by much, but every bit of optimization counts. 

All you have to do is write an API defintion. Faster? Shell is just a wrapper for the same API functions anyway so how can it be faster? What do you mean "requires less headers"? API functions are in dll's not headers and don't link any additional code to your project. Less memory? Nope! Otimization? Only in your head!

I don't know why your program is crashing but I doubt it's Mame otherwise other FE's would be breaking too. Have you done other tests with older versions of Mame? Did you try writing a C program and seeing if that would crash?

Howard_Casto

  • Idiot Police
  • Trade Count: (+1)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 19427
  • Last login:Yesterday at 11:01:57 am
  • Your Post's Soul is MINE!!! .......Again??
    • The Dragon King
Re: Calling all DEVS.... ShellandWait broken?
« Reply #9 on: February 12, 2008, 05:19:51 pm »
Your getting of topic hk, but wrong wrong wrong!

To use shell I need.... nothing... shell is built in.

To use create process I need:

Const INFINITE = &HFFFF
Const STARTF_USESHOWWINDOW = &H1
Private Enum enSW
    SW_HIDE = 0
    SW_NORMAL = 1
    SW_MAXIMIZE = 3
    SW_MINIMIZE = 6
End Enum
Private Type PROCESS_INFORMATION
    hProcess As Long
    hThread As Long
    dwProcessId As Long
    dwThreadId As Long
End Type
Private Type STARTUPINFO
    cb As Long
    lpReserved As String
    lpDesktop As String
    lpTitle As String
    dwX As Long
    dwY As Long
    dwXSize As Long
    dwYSize As Long
    dwXCountChars As Long
    dwYCountChars As Long
    dwFillAttribute As Long
    dwFlags As Long
    wShowWindow As Integer
    cbReserved2 As Integer
    lpReserved2 As Byte
    hStdInput As Long
    hStdOutput As Long
    hStdError As Long
End Type
Private Type SECURITY_ATTRIBUTES
    nLength As Long
    lpSecurityDescriptor As Long
    bInheritHandle As Long
End Type
Private Enum enPriority_Class
    NORMAL_PRIORITY_CLASS = &H20
    IDLE_PRIORITY_CLASS = &H40
    HIGH_PRIORITY_CLASS = &H80
End Enum
Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long


See all those variables I otherwise wouldn't need?  They take up memory.  See those types?  Also more memory.  This causes the program to take longer loading (thus the faster comment).  Also there's all that typing you don't have to do, which means it's faster to code and easier to read.  So yeah, using shell is a more optimized (both code-wise and performance wise) than using create process.

It doesn't matter anyway... as I've said, createprocess and shell are behaving the same way. 


I'm one step ahead of you...  tried a do loop instead of infinate... still hangs.  I'm trying a modified version now just to make sure. 

Howard_Casto

  • Idiot Police
  • Trade Count: (+1)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 19427
  • Last login:Yesterday at 11:01:57 am
  • Your Post's Soul is MINE!!! .......Again??
    • The Dragon King
Re: Calling all DEVS.... ShellandWait broken?
« Reply #10 on: February 12, 2008, 05:57:54 pm »
Ugh... I finally found a work-around but man is it sloppy. 

Just to recap....

shell and infinate wait hangs
createprocess and infinate hangs
even the check for exit code loop hk suggested hangs
dumb loop (waitforsingle object of 1ms repated until you get an error) hangs

what i ended up doing was making a dumb loop and sleeping for several millisceonds between each waitforsingle object call. 

I have no idea why this is working over checking for the exit code other than mame is just getting hung up on something and although there's no way programatically to fix it a bit of breathing room is letting it get passed the point where it hangs. 

I can assure you guys thought that this is something fairly new and it is indeed related to mame in some way.  Dk is quite old and I first found the issue there... the launching code has remained pretty much untouched since around 2000.  I then went in to some of my other apps which use different functions  for various reasons (such as wanting to pipe data ect...) and forced them to launch the mame.exe instead of what they are designed for and they all hung.  Also keep in mind that alot of the times the functions aren't even mine.  That example I sent you guys was ripped directly from codeguru to make sure the tweaks I do to functions weren't the cause. 

I'm not saying that it's mame's code specifically that's doing it though rather some indirect cause, like windows releasing a security update that breaks one of the vb dlls or something and such a strange way that it only effects mame or apps very similar to mame in the way they are compiled/coded.  It'd still be nice to know what's going on as having the program running in the background (even if it is just a simple loop) sure is inefficient when you don't need it to. 

Howard_Casto

  • Idiot Police
  • Trade Count: (+1)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 19427
  • Last login:Yesterday at 11:01:57 am
  • Your Post's Soul is MINE!!! .......Again??
    • The Dragon King
Re: Calling all DEVS.... ShellandWait broken?
« Reply #11 on: February 12, 2008, 06:07:41 pm »
Crap... well nevermind....


Looks like that one hangs too, it's just every so often it doesn't.


Nevermind the nevermind.... a doevents fixed the glitchiness. 
« Last Edit: February 12, 2008, 06:27:13 pm by Howard_Casto »

youki

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 1612
  • Last login:November 19, 2016, 01:07:33 pm
  • Atomic Front End Creator
    • Atomic Front End
Re: Calling all DEVS.... ShellandWait broken?
« Reply #12 on: February 12, 2008, 06:18:15 pm »
Use a real language not VB6...  ;D   

Quote
See all those variables I otherwise wouldn't need?  They take up memory.  See those types?  Also more memory.  This causes the program to take longer loading (thus the faster comment).  Also there's all that typing you don't have to do, which means it's faster to code and easier to read.  So yeah, using shell is a more optimized (both code-wise and performance wise) than using create process.


Sorry your are wrong here. Ok, you have more code to type. I agree. Shell is more readable in VB.  But your type definition does not type memory. It is only definition it only consume space in your source code not in the executable or in memory (even in the pseudo-executable of Vb).  And anyway , i bet Shell somewhere inside will call CreateProcess somewhere.
But it is of topic.

Did you try to write your own DLL in a real language like C , C++ or Delphi or else  that exports your home made Shell function that call CreateProcess and call it from VB?

Quote
like windows releasing a security update that breaks one of the vb dlls


If it broke VB Dlls, it would work if you use the CreateProcess method instead of shell. Because you call directly the API.
So i don't think it is something with VB Dll.

Sound like DK will be delayed again!  ;D  (i'm just kidding here too)






Howard_Casto

  • Idiot Police
  • Trade Count: (+1)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 19427
  • Last login:Yesterday at 11:01:57 am
  • Your Post's Soul is MINE!!! .......Again??
    • The Dragon King
Re: Calling all DEVS.... ShellandWait broken?
« Reply #13 on: February 12, 2008, 06:39:36 pm »
I hate to argue on something that really doesn't matter youki, but you guys are really wrong on this one. 

Yeah the type definitions don't take up any memory, but the type variables defined from the types do, not only that, but they take up a lot of extra memory (in each type there's often junk you never use for a standard function but memory is allocated for it anyway).  Also you have your extra public constants, which take up memory, again, public constants not needed for shell... even private constants just used in the function cause for a memory spike and since windoze is slow to re-allocate memory back to other running applications it does make a difference.  Also, while I'm not sure the exact mechanism behind it, just defining apis causes the size of an vb application, even a compiled one, to grow.  You can try this one yourself....  Make an app that doesn't do anything, I mean nothing.  Now add a bunch of random api definitions and compile.... the exe size will grow!  A larger exe means a larger memory footprint.    Now as far as symantics go you guys could be(and most probably are) definately right on, I'm not sure exactly how vb does the stuff it does in terms of memory allocations and which things are pre-allocated and which aren't but I've coded in vb for so long that I do know the end results of doing things one way vs another and while createprocess definately doesn't add any significant overhead, it does add some, and when you have a monster of an api caller like dk, you don't want to be wasting memory, especially when the more optimized alternative actually ends up being quicker to code for a change.  ;)

Cakemeister

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 1002
  • Last login:May 31, 2024, 06:23:16 pm
  • I'm a llama!
Re: Calling all DEVS.... ShellandWait broken?
« Reply #14 on: February 13, 2008, 12:24:07 am »
Seems to me that the variables and stuff would take up stack space rather than system memory, and a good compiler will optimize for putting variables in registers when possible.

I haven't checked lately what MAME does when it exits. I'll look at it.

Old, but not obsolete.

headkaze

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2943
  • Last login:August 14, 2023, 02:00:48 am
  • 0x2b|~0x2b?
Re: Calling all DEVS.... ShellandWait broken?
« Reply #15 on: February 13, 2008, 04:17:10 am »
Nevermind the nevermind.... a doevents fixed the glitchiness. 

What do you think "process the message pump" means? That is the FastDoEvents call. If you had of looked at my code in the first place you wouldn't be in this mess ;)

Code: [Select]
Call FastDoEvents ' DoEvents
I'm one step ahead of you...  tried a do loop instead of infinate... still hangs.  I'm trying a modified version now just to make sure. 

Congrat's on "figuring it out" after I pasted the solution :P
« Last Edit: February 13, 2008, 04:22:34 am by headkaze »

Howard_Casto

  • Idiot Police
  • Trade Count: (+1)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 19427
  • Last login:Yesterday at 11:01:57 am
  • Your Post's Soul is MINE!!! .......Again??
    • The Dragon King
Re: Calling all DEVS.... ShellandWait broken?
« Reply #16 on: February 13, 2008, 09:24:25 pm »
Nevermind the nevermind.... a doevents fixed the glitchiness. 

What do you think "process the message pump" means? That is the FastDoEvents call. If you had of looked at my code in the first place you wouldn't be in this mess ;)

Code: [Select]
Call FastDoEvents ' DoEvents
I'm one step ahead of you...  tried a do loop instead of infinate... still hangs.  I'm trying a modified version now just to make sure. 

Congrat's on "figuring it out" after I pasted the solution :P

SOMEBODY'S a tad cranky....... 

Yes I noticed the FASTDOEVENTS call... a call that isn't an official vb function and you didn't post in the blurb so I could only guess as to what it was actually doing.  I'm a programmer, not a mind reader.  ;)

For the record I DID look at your code, but it never makes any sense to me.  I mean I understood the exit message stuff only because I've personally used it before, but seeing as how you didn't post the definitions of the api or the constants you used, if I hadn't had been familiar with it it might as well said "magicalfunction_9"  And then you throw around terms like "process the message pump" which means just that, to process the exit message call and tell me you meant doevents?  Every time I've ever heard an explaination of doevents it's been referred to as "give it time to breathe" or something to that degree.  It also has less to do with how readable your code is and more to do with how you go about things.  While checking for an exit message is certainly a way to do it, since waitforsingleobject is going to error out the first time you call it to a dead process anyway, you can check that way and save code and resources.  It's by no means bad coding, it's actually very good, but it seems so odd to me that when you show something like that I'm so puzzled as to why you'd do it that way that I pretty much ignore the rest and in this case it was a minor thing that you didn't focus on in your explaination that would have helped and not the method you discussed. 

My solution is different than yours.... it's a dumb loop with doevents which is more efficient.  No call to get the exit message and then check to see if it has fired which saves prescious processes.  I really appreciate your help hk but you'd have to post all your code if you want to get credit for posting a solution.   :P :P

I know a lot of people aren't concerned about the minimal amount of resources that are taken up by a loop as opposed to freezing the program all-together but I am because mame is so processor intensive.  I've been aware of the warnings against using an infinate wait in vb for years but all I knew was it worked anyway and when a fe is frozen it doesn't eat up resources while a fe in a loop does. 

One of the things I was and am currently worried about due to the changes I had to make to dk in particular is memory allocation.  When a proggie is frozen, it's memory space is given a lower priority and thus when the system starts running out of physical memory it's the first to go to swap space.  Now it'll be running though, so I'm not sure what kind of priority it'll get.

And I do appreciate all the help guys, don't get me wrong.  But the reason I brought this up wasn't really to fix my own personal code, it was to figure out why a prefectly functioning function.... THE shellandwait example upon all examples were built, has suddenly stopped working.  While everyone was randomly throwing alternatives my way, which is very much appreciated but completely missing the point, I was trying to get to the root of the problem.  It is waitforsingleobject with an infinate setting in conjunction with using this call on mame, regardless of the launch method that is freezing mame.  Regardless of bad programming practice, ect.. it worked for years and then all of the sudden it doesn't.    Nevermind though, it's not worth all this arguing and I wasn't trying to get people upset rather to have them try the code I posted and see if it feezes for them to determine it was my computer exclusively, a service pack or ect.  Like I said, nm though....

headkaze

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2943
  • Last login:August 14, 2023, 02:00:48 am
  • 0x2b|~0x2b?
Re: Calling all DEVS.... ShellandWait broken?
« Reply #17 on: February 14, 2008, 01:09:39 am »
SOMEBODY'S a tad cranky.......

You made posts saying "hey I found the solution it's [insert HK's solution here]". I just congratulated you for finding it :P

Yes I noticed the FASTDOEVENTS call... a call that isn't an official vb function and you didn't post in the blurb so I could only guess as to what it was actually doing.  I'm a programmer, not a mind reader.  ;)

I'm confused; you understand what DoEvents means but not what FastDoEvents means? None of the API stuff your using is "an official vb function" so why does it matter? If your going to use API you might as well use API, afterall it's what Windows is, and VB6 "official functions" just wrap those into VB6 syntax anyway.

For the record I DID look at your code, but it never makes any sense to me.  I mean I understood the exit message stuff only because I've personally used it before, but seeing as how you didn't post the definitions of the api or the constants you used, if I hadn't had been familiar with it it might as well said "magicalfunction_9"  And then you throw around terms like "process the message pump" which means just that, to process the exit message call and tell me you meant doevents?  Every time I've ever heard an explaination of doevents it's been referred to as "give it time to breathe" or something to that degree.  It also has less to do with how readable your code is and more to do with how you go about things.

How can it not make sense when every line is commented? Process the "message pump" is standard Windows terminology while "give it time to breathe" isn't. "Message pump" is a standard windows term for a loop that processes messages in a Window. VB6 forms are just wrappers for a standard Window created using CreateWindow() API. I've never heard of "give it time to breathe" and it just sounds like something someone made up to explain something they don't understand.

This is what the API version of a message pump looks like (AKA "DoEvents" in some languages). An example message this will process is WM_CLOSE.

Code: [Select]
MSG msg;

while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

The MFC way uses different syntax but you get the point. Also you can always look things up on Google if you don't understand what a term means. But as a fellow coder, I thought the code snippet would explain itself, and there was also a link to the full source code too in my post incase you missed it.

While checking for an exit message is certainly a way to do it, since waitforsingleobject is going to error out the first time you call it to a dead process anyway, you can check that way and save code and resources.  It's by no means bad coding, it's actually very good, but it seems so odd to me that when you show something like that I'm so puzzled as to why you'd do it that way that I pretty much ignore the rest and in this case it was a minor thing that you didn't focus on in your explaination that would have helped and not the method you discussed.

Using GetExitCodeProcess is the better way to do it IMHO. There is nothing odd about what I showed you, what is odd is the way you explain it away.

My solution is different than yours.... it's a dumb loop with doevents which is more efficient.  No call to get the exit message and then check to see if it has fired which saves prescious processes.  I really appreciate your help hk but you'd have to post all your code if you want to get credit for posting a solution.   :P :P

There was a link to the full source code in my post *rolls eyes*

I know a lot of people aren't concerned about the minimal amount of resources that are taken up by a loop as opposed to freezing the program all-together but I am because mame is so processor intensive.  I've been aware of the warnings against using an infinate wait in vb for years but all I knew was it worked anyway and when a fe is frozen it doesn't eat up resources while a fe in a loop does.

Optimization is important, saving resources is important. But you think doing things in certain ways to save those resources is in fact doing things the wrong way. Sometimes you have to accept a few extra cycles to do things the right way.

One of the things I was and am currently worried about due to the changes I had to make to dk in particular is memory allocation.  When a proggie is frozen, it's memory space is given a lower priority and thus when the system starts running out of physical memory it's the first to go to swap space.  Now it'll be running though, so I'm not sure what kind of priority it'll get.

Don't quite understand your terminology here. Why are you running out of physical memory? Do you mean when you launch an external process? What you should be doing is closing the DirectX device, freeing up all your textures and closing your window and going into a low CPU usage state to wait for the program to exit. It should be looping over not "frozen". You can't use threading in VB6 so there is no other way around it.

And I do appreciate all the help guys, don't get me wrong.  But the reason I brought this up wasn't really to fix my own personal code, it was to figure out why a prefectly functioning function.... THE shellandwait example upon all examples were built, has suddenly stopped working.

You could be alot more humble when people help you instead of trying to make it look like a competition. What's more offensive though is when you say "I'm right and your wrong" when it's clear to me the opposite it true. If you didn't make outrageous claims and acusations there would be no argument here.

While everyone was randomly throwing alternatives my way, which is very much appreciated but completely missing the point, I was trying to get to the root of the problem.  It is waitforsingleobject with an infinate setting in conjunction with using this call on mame, regardless of the launch method that is freezing mame.  Regardless of bad programming practice, ect.. it worked for years and then all of the sudden it doesn't.    Nevermind though, it's not worth all this arguing and I wasn't trying to get people upset rather to have them try the code I posted and see if it feezes for them to determine it was my computer exclusively, a service pack or ect.  Like I said, nm though....

I didn't think I missed the point at all. I got straight to the point and posted the solution in my first reply. I posted the code that solved the problem. I explained why there was a problem. Don't understand how you could say that??
« Last Edit: February 14, 2008, 03:08:43 am by headkaze »

Ummon

  • Trade Count: (+13)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 5244
  • Last login:June 09, 2010, 06:37:18 pm
Re: Calling all DEVS.... ShellandWait broken?
« Reply #18 on: February 15, 2008, 02:03:35 pm »
I don't know about this stuff, but I found something that maybe is related? When my AVG does a scan, there's something in the box. Not an infection. It says it's a 'change', and it's called Shell32.dll.
Yo. Chocolate.


"Theoretical physics has been the most successful and cost-effective in all of science."

Stephen Hawking


People often confuse expressed observations with complaint, ridicule, or - even worse - self-pity.

Pete Rittwage

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 17
  • Last login:June 18, 2024, 09:51:22 pm
Re: Calling all DEVS.... ShellandWait broken?
« Reply #19 on: June 25, 2008, 02:20:35 pm »
This exact same thing happens on my UltraMAME FrontEnd when SP2 came out.  It only happens launching the MAME executable and not other programs and I've tried launching via the shell and with CreateProcess.

Once the hang occurs, if you then kill the mame.exe task, it works fine after that.  It's only the first run after the reboot as mentioned.

I never figured it out.  I use CreateProcess and WaitForSingleObject with the INFINITE setting.

I use GCC/MingW and the program is in 'C'.

Cakemeister

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 1002
  • Last login:May 31, 2024, 06:23:16 pm
  • I'm a llama!
Re: Calling all DEVS.... ShellandWait broken?
« Reply #20 on: June 25, 2008, 03:15:48 pm »
I use CreateProcess/WaitForSingleObject with gcc/mingw as well but I use 100 for the timeout instead of INFINITE.

The problem only happens with MAME, and only the first time you launch MAME after a reboot?
Old, but not obsolete.

Pete Rittwage

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 17
  • Last login:June 18, 2024, 09:51:22 pm
Re: Calling all DEVS.... ShellandWait broken?
« Reply #21 on: June 25, 2008, 03:34:28 pm »
Yep, only the first time after a reboot.

Here is my code:

if (CreateProcess(NULL, command, sa, FALSE, TRUE, FALSE, FALSE, workingDir, si, &proc))
   {
           // Wait for the application to finish
             WaitForSingleObject(proc.hProcess, INFINITE);

            if ((exitCode) && (launchingMame))
             {
              if (!GetExitCodeProcess(proc.hProcess, exitCode))
                 MessageBox(hwndMain, "GetExitCodeProcess failed!", "Error", MB_OK);
      }

             CloseHandle(proc.hProcess);
             CloseHandle(proc.hThread);
   }

The hang occurs on WaitForSingleObject().  I rewrote the launcher routine this afternoon after this discussion caught my interest again without that call and it works fine.   My launcher loses focus and properly handles messages anyway after MAME launches, so it isn't wasting CPU cycles.  I take back over when I get the WM_PAINT message and all is fine.

I thought this was some problem with process permissions and SP2 when I first looked at it and went around and around and gave up a year or so ago (my MAME cab runs SP1).  I don't think that's the case, it's something with the process communication and MAME...

Pete Rittwage
UltraMAME - http://rittwage.com/ultramame

Pete Rittwage

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 17
  • Last login:June 18, 2024, 09:51:22 pm
Re: Calling all DEVS.... ShellandWait broken?
« Reply #22 on: June 25, 2008, 10:25:06 pm »
I also added back in the WaitForSingleProcess and just set it to 500ms instead of INFINITE and that also works.  Of course control is returned back to my frontend this way, but it already handles messages properly and isn't in focus at that point.

telengard

  • Trade Count: (+1)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 784
  • Last login:Yesterday at 04:33:52 pm
  • Yeah, it's a classic! 21+ on BYOAC and still goin
    • S T U R C A D E
Re: Calling all DEVS.... ShellandWait broken?
« Reply #23 on: October 08, 2011, 07:31:37 pm »
This is quite an old thread, but I'm wondering if anyone ever figured out exactly what was happening here?  I'm experiencing this w/ advmenu.  From what I can tell, this is related to DirectX 9 (I don't see it with DirectX 8).  I'll poke around the source for advmenu, but I thought I'd check to see if there was an explanation for what was going on...

~telengard
S T U R C A D E     M.A.M.E. Cabinet
http://www.briansturk.com/mame.html

abispac

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 1625
  • Last login:July 15, 2025, 09:27:36 pm
Re: Calling all DEVS.... ShellandWait broken?
« Reply #24 on: October 09, 2011, 01:21:18 pm »
Im not a programmer, but i noticed mame hangs when you fire'it the first time, either with mala, or hyperspin, and i wonder if this might be the problem and if it is, i want to know what was the solution for it? thanks for any help.