|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
App already running?Hey all,
Another convert to vb.net here. My vb6 program was an application launcher that checked for previous instances of said programs and would alert the user with a message box. I can't, for the life of me, figure out how to do this in vb.net. Let's say notepad.exe (as an example) is already running. My program tries to launch it again, but I need to check if notepad.exe is already running before I launch it. Does anyone have a snipped to code to help me out here? I've googled my arse off and the closest I can get is to check for a previous instance of /my/ app, and I can't even get /that/ to work. Sounds simple, but I'm stumped. Thanks for any help! -b I'm sure there's a windows API that will do just what you want. I may be able
to find it, but don't have time right now. Meanwhile, good luck -- Show quoteHide quoteThanks. roho***@hotmail.com "Bradley" wrote: > Hey all, > Another convert to vb.net here. My vb6 program was an application > launcher that checked for previous instances of said programs and would > alert the user with a message box. I can't, for the life of me, figure > out how to do this in vb.net. Let's say notepad.exe (as an example) is > already running. My program tries to launch it again, but I need to > check if notepad.exe is already running before I launch it. > > Does anyone have a snipped to code to help me out here? I've googled > my arse off and the closest I can get is to check for a previous > instance of /my/ app, and I can't even get /that/ to work. > > Sounds simple, but I'm stumped. > > Thanks for any help! > > -b > Here's some code I created for VB6 --
Public Declare Function GetWindow Lib "user32" ( _ ByVal hwnd As Long, _ ByVal wCmd As Long _ ) As Long Public Declare Function GetWindowText Lib "user32" _ Alias "GetWindowTextA" ( _ ByVal hwnd As Long, _ ByVal lpString As String, _ ByVal cch As Long _ ) As Long Public Declare Function GetWindowTextLength Lib "user32" _ Alias "GetWindowTextLengthA" ( _ ByVal hwnd As Long _ ) As Long Public Const GW_HWNDFIRST = 0 Public Const GW_HWNDLAST = 1 Public Const GW_HWNDNEXT = 2 Public Const GW_HWNDPREV = 3 Public Const GW_OWNER = 4 Public Const GW_CHILD = 5 Sub LoadTaskList(FHandle As Long, Wins$()) 'loads captions and handles of all windows in Wins$ array Dim hwnd As Long, L, cap$, n 'Dim grd As MSFlexGrid ReDim Wins$(2, 0) n = 0 hwnd = GetWindow(FHandle, GW_HWNDFIRST) Do While hwnd <> 0 L = GetWindowTextLength(hwnd) cap$ = Space$(L + 1) L = GetWindowText(hwnd, cap$, L + 1) If L > 0 Then n = n + 1 ReDim Preserve Wins$(2, n) Wins$(1, n) = cap$ Wins$(2, n) = hwnd End If hwnd = GetWindow(hwnd, GW_HWNDNEXT) DoEvents Loop End Sub Function GetWinHandle(ByVal cap$, FHandle As Long) As Long Dim n, i, t$, hwnd As Long, Found As Boolean, Wins$() GetWinHandle = 0 LoadTaskList FHandle, Wins$() cap$ = UCase(cap$) n = UBound(Wins, 2) For i = 1 To n t$ = Wins$(1, i) t$ = UCase(t$) If InStr(t$, cap$) > 0 Then Found = True hwnd = Val(Wins$(2, i)) Exit For End If Next i If Found Then GetWinHandle = hwnd End Function Function IsAppActive(ap$, whandle As Long) As Boolean Dim h h = GetWinHandle(ap$, whandle) IsAppActive = (h > 0) End Function (I'm not an expert, so check this out thoroughly yourself!) -- Show quoteHide quoteGood Luck! roho***@hotmail.com "Bradley" wrote: > Hey all, > Another convert to vb.net here. My vb6 program was an application > launcher that checked for previous instances of said programs and would > alert the user with a message box. I can't, for the life of me, figure > out how to do this in vb.net. Let's say notepad.exe (as an example) is > already running. My program tries to launch it again, but I need to > check if notepad.exe is already running before I launch it. > > Does anyone have a snipped to code to help me out here? I've googled > my arse off and the closest I can get is to check for a previous > instance of /my/ app, and I can't even get /that/ to work. > > Sounds simple, but I'm stumped. > > Thanks for any help! > > -b > Thanks Bob, I'm see how this imports into vb.net.
-b Bob Homes wrote: Show quoteHide quote > Here's some code I created for VB6 -- > > Public Declare Function GetWindow Lib "user32" ( _ > ByVal hwnd As Long, _ > ByVal wCmd As Long _ > ) As Long > > Public Declare Function GetWindowText Lib "user32" _ > Alias "GetWindowTextA" ( _ > ByVal hwnd As Long, _ > ByVal lpString As String, _ > ByVal cch As Long _ > ) As Long > > Public Declare Function GetWindowTextLength Lib "user32" _ > Alias "GetWindowTextLengthA" ( _ > ByVal hwnd As Long _ > ) As Long > > Public Const GW_HWNDFIRST = 0 > Public Const GW_HWNDLAST = 1 > Public Const GW_HWNDNEXT = 2 > Public Const GW_HWNDPREV = 3 > Public Const GW_OWNER = 4 > Public Const GW_CHILD = 5 > > > Sub LoadTaskList(FHandle As Long, Wins$()) > 'loads captions and handles of all windows in Wins$ array > Dim hwnd As Long, L, cap$, n > 'Dim grd As MSFlexGrid > ReDim Wins$(2, 0) > > n = 0 > hwnd = GetWindow(FHandle, GW_HWNDFIRST) > Do While hwnd <> 0 > L = GetWindowTextLength(hwnd) > > cap$ = Space$(L + 1) > L = GetWindowText(hwnd, cap$, L + 1) > > If L > 0 Then > n = n + 1 > ReDim Preserve Wins$(2, n) > > Wins$(1, n) = cap$ > Wins$(2, n) = hwnd > End If > > hwnd = GetWindow(hwnd, GW_HWNDNEXT) > DoEvents > Loop > End Sub > > Function GetWinHandle(ByVal cap$, FHandle As Long) As Long > Dim n, i, t$, hwnd As Long, Found As Boolean, Wins$() > GetWinHandle = 0 > > LoadTaskList FHandle, Wins$() > > cap$ = UCase(cap$) > > n = UBound(Wins, 2) > For i = 1 To n > t$ = Wins$(1, i) > t$ = UCase(t$) > If InStr(t$, cap$) > 0 Then > Found = True > hwnd = Val(Wins$(2, i)) > Exit For > End If > Next i > > If Found Then GetWinHandle = hwnd > End Function > > Function IsAppActive(ap$, whandle As Long) As Boolean > Dim h > h = GetWinHandle(ap$, whandle) > IsAppActive = (h > 0) > End Function > > > (I'm not an expert, so check this out thoroughly yourself!) > > Bradley,
Beside using an Api you can of course use as well .Net. After many discussion in this newsgroup is this probably the best solution http://www.vb-tips.com/default.aspx?ID=59135549-e5dd-4501-9526-343ac05a7617 I hope this helps, Cor Show quoteHide quote "Bradley" <Brad***@buyer.nut> schreef in bericht news:XkDgg.49326$mh.890@tornado.ohiordc.rr.com... > Hey all, > Another convert to vb.net here. My vb6 program was an application > launcher that checked for previous instances of said programs and would > alert the user with a message box. I can't, for the life of me, figure > out how to do this in vb.net. Let's say notepad.exe (as an example) is > already running. My program tries to launch it again, but I need to check > if notepad.exe is already running before I launch it. > > Does anyone have a snipped to code to help me out here? I've googled my > arse off and the closest I can get is to check for a previous instance of > /my/ app, and I can't even get /that/ to work. > > Sounds simple, but I'm stumped. > > Thanks for any help! > > -b Thanks, Cor,
What does "xvcjsdf67AS124#$3" refer to? Anything in particular? -b > Public Sub main() Cor Ligthert [MVP] wrote:> > Dim owned As Boolean > Dim mut As New System.Threading.Mutex(True, "xvcjsdf67AS124#$3", owned) > If owned Then > Application.Run(New Form1) > mut.ReleaseMutex() > Else > MessageBox.Show("A previous instance is already running") > End If > End Sub Show quoteHide quote > Bradley, > > Beside using an Api you can of course use as well .Net. > > After many discussion in this newsgroup is this probably the best solution > > http://www.vb-tips.com/default.aspx?ID=59135549-e5dd-4501-9526-343ac05a7617 > > I hope this helps, > > Cor > > > "Bradley" <Brad***@buyer.nut> schreef in bericht > news:XkDgg.49326$mh.890@tornado.ohiordc.rr.com... >> Hey all, >> Another convert to vb.net here. My vb6 program was an application >> launcher that checked for previous instances of said programs and would >> alert the user with a message box. I can't, for the life of me, figure >> out how to do this in vb.net. Let's say notepad.exe (as an example) is >> already running. My program tries to launch it again, but I need to check >> if notepad.exe is already running before I launch it. >> >> Does anyone have a snipped to code to help me out here? I've googled my >> arse off and the closest I can get is to check for a previous instance of >> /my/ app, and I can't even get /that/ to work. >> >> Sounds simple, but I'm stumped. >> >> Thanks for any help! >> >> -b > > The following function should return true if another instance is running.
You can check this in the Sub Main or whever using "Application.Exit" or Form.Close methods depending on how your are starting your application. Private Function PrevInstance() As Boolean If UBound(System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then Return True Else Return False End If End Function -- Show quoteHide quoteDennis in Houston "Bradley" wrote: > Thanks, Cor, > What does "xvcjsdf67AS124#$3" refer to? Anything in particular? > > -b > > > Public Sub main() > > > > Dim owned As Boolean > > Dim mut As New System.Threading.Mutex(True, "xvcjsdf67AS124#$3", owned) > > If owned Then > > Application.Run(New Form1) > > mut.ReleaseMutex() > > Else > > MessageBox.Show("A previous instance is already running") > > End If > > End Sub > > Cor Ligthert [MVP] wrote: > > Bradley, > > > > Beside using an Api you can of course use as well .Net. > > > > After many discussion in this newsgroup is this probably the best solution > > > > http://www.vb-tips.com/default.aspx?ID=59135549-e5dd-4501-9526-343ac05a7617 > > > > I hope this helps, > > > > Cor > > > > > > "Bradley" <Brad***@buyer.nut> schreef in bericht > > news:XkDgg.49326$mh.890@tornado.ohiordc.rr.com... > >> Hey all, > >> Another convert to vb.net here. My vb6 program was an application > >> launcher that checked for previous instances of said programs and would > >> alert the user with a message box. I can't, for the life of me, figure > >> out how to do this in vb.net. Let's say notepad.exe (as an example) is > >> already running. My program tries to launch it again, but I need to check > >> if notepad.exe is already running before I launch it. > >> > >> Does anyone have a snipped to code to help me out here? I've googled my > >> arse off and the closest I can get is to check for a previous instance of > >> /my/ app, and I can't even get /that/ to work. > >> > >> Sounds simple, but I'm stumped. > >> > >> Thanks for any help! > >> > >> -b > > > > > Dennis wrote:
> The following function should return true if another instance is running. Thanks all, great help! Question Dennis: How would the above code look > You can check this in the Sub Main or whever using "Application.Exit" or > Form.Close methods depending on how your are starting your application. > > Private Function PrevInstance() As Boolean > If > UBound(System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then > Return True > Else > Return False > End If > End Function for a second instance of, say, notepad.exe? (I'll admit it, I'm greener than green on vb.net...) -b Perhaps you are looking for something like this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim prg As String prg = InputBox("what program are we looking for?", "", "notepad") MessageBox.Show(HowManyRunning(prg) & " instances of " & prg & " are currently running") End Sub Private Function HowManyRunning(ByVal program As String) As Int32 Return UBound(System.Diagnostics.Process.GetProcessesByName(program)) + 1 End Function Bradley wrote: Show quoteHide quote > Dennis wrote: >> The following function should return true if another instance is >> running. You can check this in the Sub Main or whever using >> "Application.Exit" or Form.Close methods depending on how your are >> starting your application. >> >> Private Function PrevInstance() As Boolean >> If >> UBound(System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess.ProcessName)) >> > 0 Then >> Return True >> Else >> Return False >> End If >> End Function > > Thanks all, great help! Question Dennis: How would the above code look > for a second instance of, say, notepad.exe? (I'll admit it, I'm greener > than green on vb.net...) > > -b Hello Bradley,
The Mutex option will not work. If someone opens the target application using a means other than the launcher then the Mutex will not exist and the app will now be open twice. Conversely, someone could open the app from the launcher and then open it using another means. The mutex would exist but the 'other means' doesnt give two hoots about the mutex. -Boo Show quoteHide quote > Thanks, Cor, > What does "xvcjsdf67AS124#$3" refer to? Anything in particular? > -b > >> Public Sub main() >> >> Dim owned As Boolean >> Dim mut As New System.Threading.Mutex(True, "xvcjsdf67AS124#$3", >> owned) >> If owned Then >> Application.Run(New Form1) >> mut.ReleaseMutex() >> Else >> MessageBox.Show("A previous instance is already running") >> End If >> End Sub > Cor Ligthert [MVP] wrote: > >> Bradley, >> >> Beside using an Api you can of course use as well .Net. >> >> After many discussion in this newsgroup is this probably the best >> solution >> >> http://www.vb-tips.com/default.aspx?ID=59135549-e5dd-4501-9526-343ac0 >> 5a7617 >> >> I hope this helps, >> >> Cor >> >> "Bradley" <Brad***@buyer.nut> schreef in bericht >> news:XkDgg.49326$mh.890@tornado.ohiordc.rr.com... >> >>> Hey all, >>> Another convert to vb.net here. My vb6 program was an application >>> launcher that checked for previous instances of said programs and >>> would >>> alert the user with a message box. I can't, for the life of me, >>> figure >>> out how to do this in vb.net. Let's say notepad.exe (as an example) >>> is >>> already running. My program tries to launch it again, but I need to >>> check >>> if notepad.exe is already running before I launch it. >>> Does anyone have a snipped to code to help me out here? I've >>> googled my arse off and the closest I can get is to check for a >>> previous instance of /my/ app, and I can't even get /that/ to work. >>> >>> Sounds simple, but I'm stumped. >>> >>> Thanks for any help! >>> >>> -b >>> > Hello Bradley, Ah, herein lies the trick. Don't open the app from a launcher, but open it > > The Mutex option will not work. If someone opens the target > application using a means other than the launcher then the Mutex will > not exist and the app will now be open twice. Conversely, someone > could open the app from the launcher and then open it using another > means. The mutex would exist but the 'other means' doesnt give two > hoots about the mutex. directly. On the open, check the mutex. If it exists, the app is already running. If not this is a first instance. If you are using 2005, SingleInstance is extremely easy, otherwise it is a bit more tricky and interesting. I have a write-up at http://devauthority.com/blogs/jwooley/archive/2005/07/28/318.aspx. Jim Wooley http://devauthority.com/blogs/jwooley/default.aspx > What does "xvcjsdf67AS124#$3" refer to? Anything in particular? Something that probably does not exist already> :-) CorShow quoteHide quote > -b > > > Public Sub main() > > > > Dim owned As Boolean > > Dim mut As New System.Threading.Mutex(True, "xvcjsdf67AS124#$3", owned) > > If owned Then > > Application.Run(New Form1) > > mut.ReleaseMutex() > > Else > > MessageBox.Show("A previous instance is already running") > > End If > > End Sub > > Cor Ligthert [MVP] wrote: >> Bradley, >> >> Beside using an Api you can of course use as well .Net. >> >> After many discussion in this newsgroup is this probably the best >> solution >> >> http://www.vb-tips.com/default.aspx?ID=59135549-e5dd-4501-9526-343ac05a7617 >> >> I hope this helps, >> >> Cor >> >> >> "Bradley" <Brad***@buyer.nut> schreef in bericht >> news:XkDgg.49326$mh.890@tornado.ohiordc.rr.com... >>> Hey all, >>> Another convert to vb.net here. My vb6 program was an application >>> launcher that checked for previous instances of said programs and would >>> alert the user with a message box. I can't, for the life of me, figure >>> out how to do this in vb.net. Let's say notepad.exe (as an example) is >>> already running. My program tries to launch it again, but I need to >>> check if notepad.exe is already running before I launch it. >>> >>> Does anyone have a snipped to code to help me out here? I've googled my >>> arse off and the closest I can get is to check for a previous instance >>> of /my/ app, and I can't even get /that/ to work. >>> >>> Sounds simple, but I'm stumped. >>> >>> Thanks for any help! >>> >>> -b >> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schrieb: ACK. However, I don't think that your answer applies to the OP's question.>> What does "xvcjsdf67AS124#$3" refer to? Anything in particular? > > Something that probably does not exist already -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> Herfried,
Not as it is described, but I try forever not to read characters and words, but what it can be. And than it is for me the best solution. I think that the OP means with notepad a sample, not the exact application. Cor Show quoteHide quote "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> schreef in bericht news:%23KfzxPIiGHA.3408@TK2MSFTNGP05.phx.gbl... > "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schrieb: >>> What does "xvcjsdf67AS124#$3" refer to? Anything in particular? >> >> Something that probably does not exist already > > ACK. However, I don't think that your answer applies to the OP's > question. > > -- > M S Herfried K. Wagner > M V P <URL:http://dotnet.mvps.org/> > V B <URL:http://classicvb.org/petition/> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schrieb: That's true, but the mutex sample will only work for your own application > Not as it is described, but I try forever not to read characters and > words, but what it can be. And than it is for me the best solution. I > think that the OP means with notepad a sample, not the exact application. whereas the OP wants to write a launcher application that prevents starting applications more than once. -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> Herfried,
> That's true, but the mutex sample will only work for your own application I know, but why would you want to stop the behaviour from programs from > whereas the OP wants to write a launcher application that prevents > starting applications more than once. > others. Are you than not in fact changing behaviour of software from others, having the change that somebody bails you because you can give a wrong idea about that software or just claims the cost by instance after long calls with the servicedesk from that software. Just my thought, Cor "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schrieb: Huh? Who except you wants to change the behavior of software here? The OP >> That's true, but the mutex sample will only work for your own application >> whereas the OP wants to write a launcher application that prevents >> starting applications more than once. > > I know, but why would you want to stop the behaviour from programs from > others. Are you than not in fact changing behaviour of software from > others, having the change that somebody bails you because you can give a > wrong idea about that software or just claims the cost by instance after > long calls with the servicedesk from that software. simply wants to start an application only if it is not already running. This is a common feature in application launcher applications. -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> Herfried,
> So you start an instance of Internet Explorer and you want to start a second > Huh? Who except you wants to change the behavior of software here? The > OP simply wants to start an application only if it is not already running. > This is a common feature in application launcher applications. > instance (assuming you have not IE 7 beta, what you probably use) or by instance Excel or Word. And it will not start. What do you do, call Microsoft, go to a newsgroup, Google? Can you remember that guy/girl some years ago that did want to do some; what he was calling funny things for his collegues? If it is his own program, than that what I showed is in my idea the best. Not mine however what I remember me as a discussions between You, Jon and Tom and in my idea in a better way written on our website by Ken. Cor "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schrieb: Well, you are starting it through a launcher application which certainly >> Huh? Who except you wants to change the behavior of software here? The >> OP simply wants to start an application only if it is not already >> running. This is a common feature in application launcher applications. > > So you start an instance of Internet Explorer and you want to start a > second instance (assuming you have not IE 7 beta, what you probably use) > or by instance Excel or Word. And it will not start. What do you do, call > Microsoft, go to a newsgroup, Google? provides a checkbox to turn this behavior off. I remember I once had a 3rd-party application launcher application installed which provided exactly this behavior and I found it useful. -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> Hello Bradley,
Consider System.Diagnostics.Process.GetProcesses() You could compare the executable names. -Boo Show quoteHide quote > Hey all, > Another convert to vb.net here. My vb6 program was an application > launcher that checked for previous instances of said programs and > would > alert the user with a message box. I can't, for the life of me, > figure out how to do this in vb.net. Let's say notepad.exe (as an > example) is already running. My program tries to launch it again, but > I need to check if notepad.exe is already running before I launch it. > > Does anyone have a snipped to code to help me out here? I've googled > my arse off and the closest I can get is to check for a previous > instance of /my/ app, and I can't even get /that/ to work. > > Sounds simple, but I'm stumped. > > Thanks for any help! > > -b > "Bradley" <Brad***@buyer.nut> schrieb: Check out 'System.Diagnostics.Process.GetProcess*'.> Another convert to vb.net here. My vb6 program was an application > launcher that checked for previous instances of said programs and would > alert the user with a message box. I can't, for the life of me, figure > out how to do this in vb.net. Let's say notepad.exe (as an example) is > already running. My program tries to launch it again, but I need to > check if notepad.exe is already running before I launch it. -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/>
Calling forms from a Form
years,months,days XML Encoding woes... renameing Visual basic 2005 express projects transparent labels on pictureboxes "Key not found" Exception Assigning to DataSource Property of Grid aspnet vb webproject Is Visual Studio Installer available with Visual Basic 2005 Expres Setting autosize to false When changing combo box all buttons become disabled. |
|||||||||||||||||||||||