c# - 关闭主线程

标签 c# sql-server

我正在通过 smo 进行恢复过程。成功恢复后,我重新启用我的数据库 currentDb.SetOnline();

问题是数据库实例在那之后不工作。实例连接失败。所以我想你不应该关闭我的应用程序。

    Application.Exit();

但是编写上面的代码并不存在我的应用程序而不是我得到了异常。详情是

在此处输入代码未处理 System.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()

最佳答案

这可能不是最好的解决方案,但如果您尝试退出您的应用程序并且出现挂起情况,您可能想尝试一下;

Environment.Exit(1);

关于c# - 关闭主线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11860324/

相关文章:

c# - 如何允许来自 c# Textbox 的 SQL 中带有空格的文本

python - 阻止 Django 插入或更新 SQL Server 计算列

c# - 如何将委托(delegate)函数转换为内联等待

c# - 当类型已知时,类型 'T' 不能用作泛型类型或方法错误中的类型参数

c# - 自动设置 ASP.Net App_Sprites 权限

sql-server - 死锁导致部分数据丢失?

SQL如何处理页面上的大量评论/注释

c# - 在 C# 中手动控制内存分配

c# - 多线程处理集合中具有特定顺序 C# 的项目

SQL Server,结合 LIKE 和 IN?