Click here to Skip to main content
15,888,239 members
Home / Discussions / C#
   

C#

 
QuestionRequiring registration code Pin
Edward Hoad14-Nov-08 18:50
Edward Hoad14-Nov-08 18:50 
AnswerRe: Requiring registration code Pin
MavKilla15-Nov-08 1:34
MavKilla15-Nov-08 1:34 
QuestionA question about s singleton applicaiton? Pin
Jason_Shen14-Nov-08 15:14
Jason_Shen14-Nov-08 15:14 
AnswerRe: A question about s singleton applicaiton? Pin
Giorgi Dalakishvili15-Nov-08 0:35
mentorGiorgi Dalakishvili15-Nov-08 0:35 
AnswerRe: A question about s singleton applicaiton? Pin
Wendelius15-Nov-08 0:40
mentorWendelius15-Nov-08 0:40 
GeneralRe: A question about s singleton applicaiton? Pin
Jason_Shen15-Nov-08 3:48
Jason_Shen15-Nov-08 3:48 
GeneralRe: A question about s singleton applicaiton? Pin
Wendelius15-Nov-08 4:13
mentorWendelius15-Nov-08 4:13 
AnswerRe: A question about s singleton applicaiton? Pin
Jason_Shen17-Nov-08 3:27
Jason_Shen17-Nov-08 3:27 
There is a solution to this problem using some unmanaged code as follows:

public static class SingletonApp
{
	[DllImport( "user32.dll" )]
	static extern bool SetForegroundWindow( IntPtr hWnd );
	
	[DllImport( "user32.dll" )]
	static extern bool ShowWindowAsync( IntPtr hWnd , int nCmdShow );

	[DllImport( "user32.dll" )]
	static extern bool IsIconic( IntPtr hWnd ); 

        const int SW_RESTORE = 9;

	static Mutex m_Mutex;

	public static void Run( Form mainForm )
	{
	     bool first = IsFirstInstance( );
	     if( first )
	     {
		  Application.ApplicationExit += OnExit;
		  Application.Run( mainForm );
	     }
	     else
	     {
		  Process current = Process.GetCurrentProcess( );
		  Process[ ] procs = Process.GetProcessesByName( current.ProcessName );
		  IntPtr mainWindowHandle = procs[0].MainWindowHandle;
		  if( IsIconic( mainWindowHandle ) )
		  {
			ShowWindowAsync( mainWindowHandle , SW_RESTORE );
                  }
			SetForegroundWindow( mainWindowHandle ); 
	     }
        }

	static bool IsFirstInstance( )
	{
	      Assembly assembly = Assembly.GetEntryAssembly( );
	      string name = assembly.FullName;

	      m_Mutex = new Mutex( false , name );
	      bool owned = false;
	      owned = m_Mutex.WaitOne( TimeSpan.FromSeconds(0) , false );
	      return owned;
	}

	static void OnExit( object sender , EventArgs args )
	{
	      m_Mutex.ReleaseMutex( );
	      m_Mutex.Close( );
	}
}


But,there seems to be timing issues yet!
It is sometimes required to double-click executable twice in order to set
that existing instance into the foreground,which cannot be a lot understood!

I strongly expect someone to supply a better solution that is completely managed code!
Thank you very much!
Questionproblem in custom paging.. Pin
kamalesh574314-Nov-08 15:11
kamalesh574314-Nov-08 15:11 
AnswerRe: problem in custom paging.. Pin
Guffa14-Nov-08 18:06
Guffa14-Nov-08 18:06 
GeneralRe: problem in custom paging.. Pin
kamalesh574316-Nov-08 13:30
kamalesh574316-Nov-08 13:30 
GeneralRe: problem in custom paging.. Pin
Guffa16-Nov-08 15:49
Guffa16-Nov-08 15:49 
GeneralRe: problem in custom paging.. Pin
kamalesh574316-Nov-08 16:17
kamalesh574316-Nov-08 16:17 
Questionxml file embedded: can´t save changes Pin
nelsonpaixao14-Nov-08 13:46
nelsonpaixao14-Nov-08 13:46 
AnswerRe: xml file embedded: can´t save changes Pin
Dave Kreskowiak14-Nov-08 16:24
mveDave Kreskowiak14-Nov-08 16:24 
GeneralRe: xml file embedded: can´t save changes Pin
nelsonpaixao15-Nov-08 5:09
nelsonpaixao15-Nov-08 5:09 
QuestionHow to scan barcode without an active focus on a textbox Pin
hafij_cse14-Nov-08 8:58
hafij_cse14-Nov-08 8:58 
AnswerRe: How to scan barcode without an active focus on a textbox Pin
Wendelius14-Nov-08 9:11
mentorWendelius14-Nov-08 9:11 
QuestionForm minimize Pin
netJP12L14-Nov-08 8:55
netJP12L14-Nov-08 8:55 
AnswerRe: Form minimize Pin
Wendelius14-Nov-08 9:08
mentorWendelius14-Nov-08 9:08 
QuestionHow to display different texts with different colors in a textbox Pin
Seraph_summer14-Nov-08 7:55
Seraph_summer14-Nov-08 7:55 
AnswerRe: How to display different texts with different colors in a textbox Pin
Wendelius14-Nov-08 7:57
mentorWendelius14-Nov-08 7:57 
AnswerRe: How to display different texts with different colors in a textbox Pin
Paul Conrad14-Nov-08 8:45
professionalPaul Conrad14-Nov-08 8:45 
Questionone prolem for publishing project Pin
Seraph_summer14-Nov-08 7:48
Seraph_summer14-Nov-08 7:48 
AnswerRe: one prolem for publishing project Pin
sph3rex14-Nov-08 7:58
sph3rex14-Nov-08 7:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.