当应用程序不是以 UserInteractive 模式运行时显示模式对话框或窗体是无效操作。

问题:当应用程序不是以 UserInteractive 模式运行时显示模式对话框或窗体是无效操作。请指定 ServiceNotification DefaultDesktopOnly 样式,以显示服务应用程序发出的通知。

   System.Windows.Forms.MessageBox.ShowCore(IWin32Window owner, String text, String caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options, Boolean showHelp)

   System.Windows.Forms.MessageBox.Show(String text)

   prjBOI_TRO.Shipping.frmTransin.Gen_QSMC_UPS_SCS_Transin(String strLoadID, Int32 intI)

   prjBOI_TRO.Shipping.frmTransin.btnTransin_Click(Object sender, EventArgs e)

 

解决:根据Exception的StackTrace信息可以看出,问题出在MessageBox身上,于是将程式里的MessageBox.Show替换掉,问题解决。

PS:报错的程式是从winform系统中抽取出来的,然后设定了Windows Scheduled Task,而MessageBox没有对应修改掉。

您可以使用以下方法将后台服务打开的 WinForm 程序设置为前台显示: 1. 在 WinForm 应用程序的 Program.cs 文件中,将启动应用程序的代码更改为以下内容: ```C# using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace WinFormApp { static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); // 创建 WinForm窗体 var frmMain = new MainForm(); // 如果当前进程是后台进程,则将主窗体设置为前台窗口 if (Environment.UserInteractive == false) { NativeMethods.AttachConsole(NativeMethods.ATTACH_PARENT_PROCESS); frmMain.Show(); } else { Application.Run(frmMain); } } } static class NativeMethods { public const int ATTACH_PARENT_PROCESS = -1; [System.Runtime.InteropServices.DllImport("kernel32.dll")] public static extern bool AttachConsole(int dwProcessId); } } ``` 2. 在 MainForm 窗体的 Load 事件中添加以下代码: ```C# private void MainForm_Load(object sender, EventArgs e) { // 如果当前进程是后台进程,则将当前窗口设为前台窗口 if (Environment.UserInteractive == false) { NativeMethods.PostMessage(new HandleRef(this, this.Handle), NativeMethods.WM_SYSCOMMAND, NativeMethods.SC_RESTORE, 0); } } static class NativeMethods { public const int WM_SYSCOMMAND = 0x0112; public const int SC_RESTORE = 0xF120; [System.Runtime.InteropServices.DllImport("user32.dll")] public static extern bool PostMessage(HandleRef hWnd, int Msg, int wParam, int lParam); } ``` 这样,您就可以将后台服务打开的 WinForm 程序设置为前台显示了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值