2015-04-08 25 views
0

我正在使用Windows窗體應用程序由於添加的安全措施,我必須在我的應用程序中解決會話。目前我正在使用定時器來實現功能我能夠關閉窗體但我需要再次重新啓動應用程序返回到登錄form.I現在用的是下面的代碼Windows窗體中的會話實現

Private Sub sessionTimer_Tick(sender As Object, e As EventArgs) Handles sessionTimer.Tick 
    Try 
     Me.sessionTimer.Stop() 
     Me.sessionTimer.Enabled = False   
     Process.Start(Application.StartupPath + "\application.exe") 
     Process.GetCurrentProcess().Kill() 
    Catch ex As Exception 

    End Try 

End Sub 

我得到一個異常時,我用上面的方法,並沒有達到目的,我也已經嘗試使用Application.Restart沒有工作出來。請幫助我對Windows窗體新。此外,爲了重置計時器,我使用下面的代碼。

Private Sub frmMain_MouseMove(sender As Object, e As MouseEventArgs) Handles MyBase.MouseMove 
    Me.sessionTimer.Stop() 
    Me.sessionTimer.Start() 
End Sub 

但這似乎並沒有工作的主要形式有菜單的,我現在用的導航到其他形式,因此空閒時間不應包括在其中通過菜單的打開其他形式所花費的時間。我應該在frmMain中使用哪個事件來處理此問題。謝謝

+2

什麼是例外到底是什麼?而'C#'標籤似乎沒有必要。 –

+0

你爲什麼想要殺死應用程序?僅僅顯示登錄表單並關閉當前打開的所有其他表單是不是更好? –

+0

我已經嘗試使用下面的代碼@ZoharPeled,而不是Process.Start&殺死我.Hide() 昏暗的f1作爲新的frmLogin f1.Show()但這似乎並不工作,雖然登錄頁面顯示,但不顯示主表單可能是因爲在登錄表單之前有其他表單鏈接。我很難找到入口點或優先的第一組代碼。 –

回答

0

只是讓框架做的工作。

Application.Restart() 

如果從後臺線程會話定時器火災(也許你使用的System.Timers.Timer代替System.Windows.Forms.Timer)你propable必須與你的主線程同步。

Me.Invoke(new MethodInvoker(Addressof Application.Restart)) 

如果Application.Restart無法正常工作,那麼您的應用程序可能存在問題。你應該嘗試以下。

  • 如果你創建的線程,確保它們與「thread.IsBackground =真正的」創建,否則他們將讓你的進程中打開
  • 停止你的定時器和後臺工作人員。
  • 請確保您沒有處理FormClosing事件並設置e.Cancel = true的窗體。在這種情況下,您必須考慮e.CloseReason
  • 用戶馬克赫德發佈了關於Application.Restart期間會發生什麼偉大的職位,看看here
+0

我試了一個Application.restart沒有鍛鍊我導入'System.Windows.Forms.Timer'到我的frmMain你能告訴我如何同步我的timer.I從我的工具箱中拖動一個計時器到frmMain。也爲'Me.Invoke(新MethodInvoker(Application.Restart))'它會引發編譯錯誤 –

+0

@RoyDevjyoti對不起,我把C#轉換爲VB.NET,並沒有嘗試過。它應該是'Me.Invoke(新MethodInvoker(Addressof Application.Restart))'我更新了我的答案。我還添加了一些建議,接下來要做什麼。 –

0

Iam使用此代碼重新啓動我的應用程序。它工作得很好。

System.Diagnostics.Process.Start(Application.ExecutablePath) 'First start a new instance 
    Me.Close() 'Close the current application 

如果這不起作用。我認爲沒有辦法比使用另一個重新啓動Process的應用程序。下面是一個代碼示例(第二個應用程序)

Private Shared Sub RestartApp(pid As Integer, applicationName As String, arguments As String) 
    ' Wait for the process to terminate 
    Dim process__1 As Process = Nothing 
    Try 
     process__1 = Process.GetProcessById(pid) 
     process__1.WaitForExit(1000) 
      ' ArgumentException to indicate that the 
      ' process doesn't exist? 
    Catch ex As ArgumentException 
    End Try 
    Process.Start(applicationName, arguments) 
    'Arguments? 
End Sub 

Source of the code

+0

試圖它沒有鍛鍊剛剛結束的應用程序@manuchao –

+0

哪個.Net框架你使用和從哪裏是你運行代碼(Mainform !?)? – C0d1ngJammer

+0

我正在使用.net Framework 2.0.I正在運行代碼主要形式 –

相關問題