我正在尝试创建一个将在启动时运行的 WPF 应用程序。 我在注册表中添加了我的应用程序,并使用以下代码:
public void RunOnStartup(bool isChecked)
{
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey
("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
if (isChecked)
{
registryKey.SetValue("My Program", """ + System.Reflection.Assembly.GetExecutingAssembly().Location +""");
}
else
{
registryKey.DeleteValue("777xUtility");
}
}
我确认它在注册表上正确注册。但是一旦我重新启动系统并登录。我的应用程序未运行。我还有一个 UI,所以我认为我不能将其作为服务来做。
检查事件查看器时。我收到此错误:
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.InvalidOperationException
at System.Windows.Window.VerifyNotClosing()
at System.Windows.Window.InternalClose(Boolean, Boolean)
at System.Windows.Application.DoShutdown()
at System.Windows.Application.ShutdownImpl()
at System.Windows.Application.ShutdownCallback(System.Object)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate)
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(System.Object)
at MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at MS.Internal.CulturePreservingExecutionContext.Run(MS.Internal.CulturePreservingExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.ProcessQueue()
at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
at MS.Win32.HwndWrapper.WndProc(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr)
at MS.Win32.UnsafeNativeMethods.MessageBox(System.Runtime.InteropServices.HandleRef, System.String, System.String, Int32)
at System.Windows.MessageBox.ShowCore(IntPtr, System.String, System.String, System.Windows.MessageBoxButton, System.Windows.MessageBoxImage, System.Windows.MessageBoxResult, System.Windows.MessageBoxOptions)
at System.Windows.MessageBox.Show(System.String, System.String)
at _777x_Utility.BlackScreen.SetBlackWindowPositionToAux()
at _777x_Utility.BlackScreen.Window_Loaded(System.Object, System.Windows.RoutedEventArgs)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(System.Object, System.Windows.RoutedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(System.Object, System.Windows.RoutedEventArgs, Boolean)
at System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject, System.Windows.RoutedEventArgs)
at System.Windows.UIElement.RaiseEvent(System.Windows.RoutedEventArgs)
at System.Windows.BroadcastEventHelper.BroadcastEvent(System.Windows.DependencyObject, System.Windows.RoutedEvent)
at System.Windows.BroadcastEventHelper.BroadcastLoadedEvent(System.Object)
at MS.Internal.LoadedOrUnloadedOperation.DoWork()
at System.Windows.Media.MediaContext.FireLoadedPendingCallbacks()
at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
at System.Windows.Media.MediaContext.RenderMessageHandlerCore(System.Object)
at System.Windows.Media.MediaContext.RenderMessageHandler(System.Object)
at System.Windows.Media.MediaContext.Resize(System.Windows.Media.ICompositionTarget)
at System.Windows.Interop.HwndTarget.OnResize()
at System.Windows.Interop.HwndTarget.HandleMessage(MS.Internal.Interop.WindowMessage, IntPtr, IntPtr)
at System.Windows.Interop.HwndSource.HwndTargetFilterMessage(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
at MS.Win32.HwndWrapper.WndProc(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr)
at MS.Win32.UnsafeNativeMethods.CallWindowProc(IntPtr, IntPtr, Int32, IntPtr, IntPtr)
at MS.Win32.HwndSubclass.DefWndProcWrapper(IntPtr, Int32, IntPtr, IntPtr)
at MS.Win32.UnsafeNativeMethods.CallWindowProc(IntPtr, IntPtr, Int32, IntPtr, IntPtr)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr)
我手动运行它没有问题。这仅在启动期间发生。 现在,我想在启动时打开应用程序之前有一个延迟,因为我认为某些系统文件尚未加载。我在这里找到了解决方案,但我仍然遇到同样的问题。
这是我在这里得到的解决方案,但它对我不起作用。
protected override void OnStartup(StartupEventArgs e)
{
//If a command line argument was passed and it was some special string,
// then we want to do the sleep. Otherwise, we don't sleep and just
// continue on right away.
if (e.Args.Length == 1 && e.Args[0] == "startup")
{
//Sleep for 60 seconds
System.Threading.Thread.Sleep(30 * 1000);
}
//Continue on...
base.OnStartup(e);
}
在启动期间,还有其他方法可以延迟我的申请打开吗?或者其他方法可以在启动时启动我的应用程序?
[编辑] 这是我的 SetBlackWindowPositiontoAux 代码
public void SetBlackWindowPositionToAux()
{
try
{
bool isSuccessful;
int result;
//remove window style
result = Libraries.SetWindowLong(blackScreenHandle, Libraries.GWL_STYLE, Libraries.WS_VISIBLE);
if (result == 0)
{
System.Windows.MessageBox.Show("Error when setting black window to maximized. Result");
}
isSuccessful = Libraries.SetWindowPos(blackScreenHandle, Libraries.HWND_TOPMOST, MainWindowValues.Instance.AuxScreen.Bounds.Left, MainWindowValues.Instance.AuxScreen.Bounds.Top, MainWindowValues.Instance.AuxScreen.Bounds.Width, MainWindowValues.Instance.AuxScreen.Bounds.Height, Libraries.SetWindowPosFlags.DoNotSendChangingEvent);
if (!isSuccessful)
{
System.Windows.MessageBox.Show("Error when setting black window position to auxillary screen. Not Successful");
}
}
catch (NullReferenceException e)
{
System.Windows.MessageBox.Show(e.Message, ": SetBlackWindowPositionToAux");
System.Windows.Application.Current.Shutdown();
}
}
谢谢。
我用不同的方法找到了解决方案。
我发现您还可以通过在启动文件夹上创建可执行文件的快捷方式来启动启动应用程序。
这是我在Visual Studio安装程序项目上所做的:
- 右键单击目标计算机。
- 添加特殊文件夹,然后选择用户的启动文件夹
- 在应用程序文件夹中,为主输出创建快捷方式。 如果需要,可以重命名快捷方式。
- 将创建的快捷方式拖到用户的启动文件夹中。
- 构建安装程序
安装后,软件现在将在启动时运行。