关闭主线程。

4

我正在使用smo执行恢复过程。成功恢复后,我重新启用了我的数据库:

currentDb.SetOnline();

然而问题是,在那之后数据库实例不起作用了,连接失败。所以我认为为什么不关闭我的应用程序呢?

    Application.Exit();

但是编写以上代码时,我的应用程序并不存在,我得到了异常。详细信息如下:

enter code here系统.NullReferenceException未经处理

Message="Object reference not set to an instance of an object."
  Source="VitoProject"
  StackTrace:
       at VitoProject.backAndRestore.restoreDatabase_Click(Object sender, EventArgs e) in C:\WorkSpace\VitoProject\VitoProject\backAndRestore.cs:line 176
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at VitoProject.Program.Main() in C:\WorkSpace\VitoProject\VitoProject\Program.cs:line 18
       at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
1个回答

1

这可能不是最好的解决方案,但如果您尝试退出应用程序并且出现了卡顿,您可以尝试以下方法;

Environment.Exit(1);

注意:刚发现传递1代表你期望错误。更干净的方法是传递0。 - Kestami

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接