473,320 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to avoid opening the same application twice at the same time?

Hi there!

I am a newbie of .NET and I need help to avoid opening the same application twice at the same time in VB.Net. Thanks for all your help.

Monkiller
Jul 30 '07 #1
9 3247
Frinavale
9,735 Expert Mod 8TB
Hi Monkiller!

I have moved your question to the .NET forum. The .NET Articles section is reserved for articles that are purely informational; like "how-tos" etc. The .NET Forum is the place for your questions. In the future please post your questions here (Blue Menu Along the Top: Forums->.NET).

You should get more help here.

Cheers!
-Frinny
Jul 30 '07 #2
You might use some code like the following -

Expand|Select|Wrap|Line Numbers
  1. Private Function IsRunning() As Boolean
  2.         Dim CurrentProcess As Process = Process.GetCurrentProcess
  3.         For Each p As Process In Process.GetProcessesByName(CurrentProcess.ProcessName)
  4.             If (p.Id <> CurrentProcess.Id) Then
  5.                 If p.MainModule.FileName.ToLower.Equals(CurrentProcess.MainModule.FileName.ToLower) Then
  6.                     Return true
  7.                 End If
  8.             End If
  9.         Next
  10.         Return false
  11.     End Function
  12.  
...and call that function from your constructor before you initialize your components?

Probably not the most efficient route for what you're seeking to accomplish, but it should function :-)
Jul 30 '07 #3
Thank you guys so much, I will try to see!

have a good day!
Aug 2 '07 #4
You might use some code like the following -

Expand|Select|Wrap|Line Numbers
  1. Private Function IsRunning() As Boolean
  2.         Dim CurrentProcess As Process = Process.GetCurrentProcess
  3.         For Each p As Process In Process.GetProcessesByName(CurrentProcess.ProcessName)
  4.             If (p.Id <> CurrentProcess.Id) Then
  5.                 If p.MainModule.FileName.ToLower.Equals(CurrentProcess.MainModule.FileName.ToLower) Then
  6.                     Return true
  7.                 End If
  8.             End If
  9.         Next
  10.         Return false
  11.     End Function
  12.  
...and call that function from your constructor before you initialize your components?

Probably not the most efficient route for what you're seeking to accomplish, but it should function :-)
Hi seligerasmus, it works great, thanks alot. but I have one more problem now and trying to solve so can you look at this when you have time and give me some advise? I want to show the current process if it is running. Thanks
Aug 2 '07 #5
Hi Monkiller!

I have moved your question to the .NET forum. The .NET Articles section is reserved for articles that are purely informational; like "how-tos" etc. The .NET Forum is the place for your questions. In the future please post your questions here (Blue Menu Along the Top: Forums->.NET).

You should get more help here.

Cheers!
-Frinny
OK, thats fine. I'll follow your instruction. Thanks
Aug 2 '07 #6
c83
24
Hi there!

I am a newbie of .NET and I need help to avoid opening the same application twice at the same time in VB.Net. Thanks for all your help.

Monkiller
Hi!
For dealing with this kind of problems there is a class called Mutex, here is an example:
Expand|Select|Wrap|Line Numbers
  1. static void Main()
  2.         {
  3.             s_mutex = new Mutex(true,Assembly.GetExecutingAssembly().GetName().Name);
  4.  
  5.             if (s_mutex.WaitOne(0, false))
  6.             {
  7.             Application.Run(new WinForm());
  8.             }
  9.             else
  10.             MessageBox.Show("already an instance of app running!","Attention");
  11.  
  12.  
  13.  
  14.  
  15.         }
Aug 2 '07 #7
Hi seligerasmus, it works great, thanks alot. but I have one more problem now and trying to solve so can you look at this when you have time and give me some advise? I want to show the current process if it is running. Thanks
Greetings, Monkiller.

I'm glad that it worked for you. I'm a little confused about your question - Do you want to show the active form when it's not running? Or do you mean you wish to display the process ID / process name? Or something else entirely?

My apologies if I'm misunderstanding the scenario. When you have an opportunity, if you could post a clarification it would be most appreciated.

Thanks,

SeligErasmus
Aug 2 '07 #8
Plater
7,872 Expert 4TB
I would guess that what he's looking for is that if he trys to run his program when it's already running, it will show the one currently running.
I am pretty sure that the Process object has something like "Show main window" or something...if not you can do a DLLIMPORT and do something like that.
Do a search for that kinda thing and you should find some info about it.
Aug 2 '07 #9
monkiller,

If you wish to bring the window of the application already running to focus, you might try this modified bit of code -

Expand|Select|Wrap|Line Numbers
  1. Private Declare Function SetForegroundWindow Lib "user32.dll" Alias "SetForegroundWindow" (ByVal hWnd As IntPtr) As IntPtr
  2.  
  3. Private Function IsRunning() As Boolean
  4.         Dim CurrentProcess As Process = Process.GetCurrentProcess
  5.         For Each p As Process In Process.GetProcessesByName(CurrentProcess.ProcessN  ame)
  6.             If (p.Id <> CurrentProcess.Id) Then
  7.                 If p.MainModule.FileName.ToLower.Equals(CurrentProces  s.MainModule.FileName.ToLower) Then
  8.                     SetForegroundWindow(p.MainWindowHandle)
  9.                    Return true
  10.                 End If
  11.             End If
  12.         Next
  13.         Return false
  14.     End Function
  15.  
aannnnnd here's the documentation for the legacy function being used.

Hope that helps.
Aug 2 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

44
by: Carlos Andr?s | last post by:
Hi everybody. I've got a problem. I'd like to avoid opening a new window when you have pressed the shift key and you click in the left button of the mouse. I've tried the next solution, in the...
14
by: D. Alvarado | last post by:
Hello, I am trying to open a window containing an image and I would like the image to be flush against the window -- i.e. have no padding or border. Can I make this happen with a single call to a...
2
by: helpSeekar | last post by:
My application sends out alerts with multiple hyperlinks in it. Each time user clicks on a link, it opens up a new window in case of yahoo, hotmail email clients. In case of lotus notes email...
2
by: srik | last post by:
Dear All, I created a simple ASP.net web application on my desktop system, which has Windows XP as the Operating system. I then moved the ASP.net application to my server which has Windows...
5
by: Peter Rilling | last post by:
I have an interesting issues that, although I not blocking me, is not very performance. Maybe someone can suggest a better way of doing this. 1) I have a page that displays a grid with command...
6
by: Lasse Eskildsen | last post by:
Hi, How can I make sure I don't show the same form twice? If the form is already shown, just focus. I tried using ownedforms, but can't quite make it work... Thanks in advance! -- Lasse
7
by: amit.atray | last post by:
Environement : Sun OS + gnu tools + sun studio (dbx etc) having some Old C-Code (ansi + KR Style) and code inspection shows some big size variable (auto) allocated (on stack) say for ex. char...
8
by: trooper | last post by:
I have a situation where an application is present in the form of one dll. The interface to the application is an api. The code update some global data structures. This dll comes as third party...
16
by: Phil Stanton | last post by:
I have a form with a button which is supposed to open an Excel file (With lots of Macros /VBA) in it. The Excel file gets it's data from the Access program Here is the code Private Sub...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.