Click here to Skip to main content
16,012,153 members
Home / Discussions / C#
   

C#

 
GeneralRe: Class Not Registered Error (HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)) Pin
Dave Kreskowiak4-Mar-10 8:10
mveDave Kreskowiak4-Mar-10 8:10 
GeneralRe: Class Not Registered Error (HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)) Pin
Clifford Anup4-Mar-10 8:25
Clifford Anup4-Mar-10 8:25 
GeneralRe: Class Not Registered Error (HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)) Pin
Dave Kreskowiak4-Mar-10 9:05
mveDave Kreskowiak4-Mar-10 9:05 
QuestionC# Code example for adding File Attachments (when using Zetafax) Pin
But_Im_a_Lady4-Mar-10 3:35
But_Im_a_Lady4-Mar-10 3:35 
QuestionDetect if Application.Restart Fails. Pin
kevinnicol4-Mar-10 3:26
kevinnicol4-Mar-10 3:26 
AnswerRe: Detect if Application.Restart Fails. Pin
Alan N4-Mar-10 8:09
Alan N4-Mar-10 8:09 
GeneralRe: Detect if Application.Restart Fails. Pin
kevinnicol4-Mar-10 8:17
kevinnicol4-Mar-10 8:17 
GeneralRe: Detect if Application.Restart Fails. [modified] Pin
Alan N4-Mar-10 13:19
Alan N4-Mar-10 13:19 
Hi,
These two solutions may be applicable to your situation. In both the main form sets a flag if the user chooses to allow a restart and Application.Restart() is called just before the Main() method returns.

Method1() is mutex-less and makes the assumption that the first instance will exit before instance two 'gets going' (That's a very vague term!).

Method2() will make a second instance wait for 10 seconds and then give up if the first has not released the mutex.

static void Main() {
  //Method1();
  Method2();
}

private static void Method1() {
  Form1 f = new Form1();
  Application.Run(f);
  if (f.RestartRequested()) {
    MessageBox.Show("Click to restart");
    Application.Restart();
  }
}

private static void Method2() {
  const String UniqueIdentifier = "{8F6F0AC4-B9A1-45fd-A8CF-72F04E6BDE8F}";
  Mutex mutex;
  Boolean firstInstance;
  mutex = new Mutex(true, UniqueIdentifier, out firstInstance);
  if (!firstInstance) {
    if (!mutex.WaitOne(TimeSpan.FromSeconds(10))) {
      MessageBox.Show("2nd instance did not aquire the mutex\n" +
                      "and cannot start");
      return;
    }
  }
  Form1 f = new Form1();
  Application.Run(f);
  if (f.RestartRequested()) {
    Application.Restart();
    MessageBox.Show("Click within 10 seconds to start the new instance");
    mutex.ReleaseMutex();
  }
  mutex.Close();
}


Alan.
modified on Thursday, March 4, 2010 7:34 PM

QuestionMy service must run my form Pin
Andy_R4-Mar-10 2:37
Andy_R4-Mar-10 2:37 
AnswerRe: My service must run my form Pin
Gonzoox4-Mar-10 3:35
Gonzoox4-Mar-10 3:35 
GeneralRe: My service must run my form Pin
Andy_R4-Mar-10 3:42
Andy_R4-Mar-10 3:42 
GeneralRe: My service must run my form Pin
Andy_R5-Mar-10 2:08
Andy_R5-Mar-10 2:08 
GeneralRe: My service must run my form Pin
Andy_R5-Mar-10 3:10
Andy_R5-Mar-10 3:10 
Questionparameter count mismatch listBox. Pin
arsendem4-Mar-10 2:30
arsendem4-Mar-10 2:30 
AnswerRe: parameter count mismatch listBox. Pin
Not Active4-Mar-10 3:04
mentorNot Active4-Mar-10 3:04 
AnswerMessage Closed Pin
4-Mar-10 3:05
stancrm4-Mar-10 3:05 
GeneralRe: parameter count mismatch listBox. Pin
arsendem4-Mar-10 3:20
arsendem4-Mar-10 3:20 
Questionappend to excel file c# !! Pin
noamtzu004-Mar-10 1:42
noamtzu004-Mar-10 1:42 
AnswerRe: append to excel file c# !! Pin
Not Active4-Mar-10 1:57
mentorNot Active4-Mar-10 1:57 
GeneralI know this, but more comfortable working with ..... Pin
noamtzu004-Mar-10 2:41
noamtzu004-Mar-10 2:41 
GeneralRe: I know this, but more comfortable working with ..... Pin
Not Active4-Mar-10 2:59
mentorNot Active4-Mar-10 2:59 
GeneralRe: I know this, but more comfortable working with ..... Pin
noamtzu004-Mar-10 7:16
noamtzu004-Mar-10 7:16 
GeneralRe: I know this, but more comfortable working with ..... Pin
Mirko19804-Mar-10 3:15
Mirko19804-Mar-10 3:15 
GeneralRe: I know this, but more comfortable working with ..... Pin
noamtzu004-Mar-10 7:15
noamtzu004-Mar-10 7:15 
GeneralRe: I know this, but more comfortable working with ..... Pin
Dave Kreskowiak4-Mar-10 3:45
mveDave Kreskowiak4-Mar-10 3:45 

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.