我试图检测Java应用程序即将关闭的时间,以便我可以执行释放资源的方法,我已经在C#中完成了如下操作:
//Intercept when the application closes
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//Reclaim resources from MIDI usage
if (MIDIControl.CleanUp())
{
Logger.Add("Closed resources successfully on Form Close \n");
}
else
{
Logger.Add("Failed to close all resources on Form Close.");
}
}
Run Code Online (Sandbox Code Playgroud)
我试图在Java版本中执行相同的技术,但它似乎不起作用,我已经尝试过调试,它不会挂在方法名称上的断点上:
//Intercept when the application closes
public void windowClosing(WindowEvent we)
{
//Reclaim resources from MIDI usage
if(_midiInstance.CleanUp())
{
Logger.Add("Closed resources successfully on ShutDown");
}
else
{
Logger.Add("Failed to close all resources on ShutDown");
}
System.exit(0);
}
Run Code Online (Sandbox Code Playgroud)
我在等错事吗?我将如何以与C#版本相同的方式执行适当的方法.
谢谢你的时间.
我有以下代码:
private void form1_closing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
this.Hide();
}
Run Code Online (Sandbox Code Playgroud)
我想要一个关闭表单而不触发此事件的按钮。我该怎么做呢?
该按钮的代码:
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
Run Code Online (Sandbox Code Playgroud)
我希望上面的按钮关闭表单而不隐藏它。
我正在开发一个从串口读取东西的应用程序(COMM-port).简而言之,它的工作方式如下:当您在酒吧或餐厅工作时,在您可以在注册表中输入内容之前,您必须扫描一种卡片.如果此卡返回一个好的号码,您可以输入一些内容.
因此,必须有一个表格可以监听串口并检查是否有人扫描了一张卡,以及它是否是一张拥有良好权利的卡.
如果该人拥有良好的权利,则可以关闭该表格并调用另一种表格.
现在,在代码中:
这里加载了MenuForm(读取正确代码后必须可访问的表单).我打电话给frmWaiterKey出现.
Private Sub frmMenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim oForm As frmWaiterKey = New frmWaiterKey()
oForm.ShowDialog()
End Sub
Run Code Online (Sandbox Code Playgroud)
类frmWaiterKey的代码:
Private Sub frmWaiterKey_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
nameArray = SerialPort.GetPortNames
OpenComPort()
AddHandler myComPort.DataReceived, SerialDataReceivedEventHandler1
End Sub
Sub OpenComPort()
Try
' Get the selected COM port's name
' from the combo box.
If Not myComPort.IsOpen Then
myComPort.PortName = _
nameArray(0).ToString()
' Get the selected bit rate from the …Run Code Online (Sandbox Code Playgroud) 我有一个从父表单启动的子表单,其中包含:
ConfigForm cfg = new ConfigForm();
cfg.ShowDialog();
Run Code Online (Sandbox Code Playgroud)
该子窗体用于配置一些应用程序参数。我想检查是否有一些更改未保存,如果是,则警告用户。所以我的 On OnClosing 事件是这样声明的:
private async void ChildFormClosing(object sender, System.ComponentModel.CancelEventArgs e)
{
// Here i call a function that compare the current config with the saved config
bool isUptated = CheckUnsavedChanges();
// If updated is false, it means that there are unsaved changes...
if (!isUpdated)
{
e.Cancel = true;
// At this point i create a MessageDialog (Mahapps) to warn the user about unsaved changes...
MessageDialogStyle style = MessageDialogStyle.AffirmativeAndNegative;
var metroDialogSettings = new …Run Code Online (Sandbox Code Playgroud) 在用户选择退出WinForms程序后,是否有更好的方法来处理执行某项操作的任务:
:[编辑1 响应由"NoBugz评论 ] 在这种情况下,有形式没有控制盒,并有用于把一个间接层中,当用户选择关闭窗体[/编辑1]会发生什么原因
[编辑2:响应GMT +7 1月20日18:35的所有评论 ] 也许使用淡出MainForm可以简单说明您在关闭应用程序时可能需要做什么:用户无法与之交互:它显然与用户决定终止申请有关.[/ edit 2]
(使用某种形式的线程?)(对多线程应用程序有什么影响?)(这段代码"味道不好"吗?)
// 'shutDown is an external form-scoped boolean variable
//
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// make sure we don't block Windows ShutDown
// or other valid reasons to close the Form
if (e.CloseReason != CloseReason.ApplicationExitCall) return;
// test for 'shutDown flag set here
if (shutDown) return;
// cancel closing the Form this time through
e.Cancel = true;
// user choice …Run Code Online (Sandbox Code Playgroud) 在我的C#WinForms程序中,我有一些表单,我将其中一个显示为对话框:
MyForm mf = new MyForm();
mf.ShowDialog();
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试为他们设置表格结束活动时,它不起作用;
mf.FormClosing += delegate { MessageBox.Show("Dialog is closed.")};
Run Code Online (Sandbox Code Playgroud)
问题是什么?
PS:当我使用mf.Show()方法调用表单时,它工作正常.
谢谢.
我在.NET4.5 Winforms C#中工作.
我的应用程序基于模型视图控制器模式.在主窗体(视图)上,我有一个按钮退出.如果用户按下此按钮,则在控制器上调用一个清除内容然后调用Application.Exit()的方法.这很好用.
当用户在右上方窗口中按下默认窗口exit-cross时,我也想运行此方法.为此,我处理FormClosing事件.在这个事件中,我再次调用调用Application.Exit()的控制器方法;
现在的问题是Application.Exit()也会触发FormClosing事件.这会创建一个双重调用.当然,我可以制作一面旗帜并对此进行测试,但我觉得我做错了.
什么是关闭我的winforms应用程序和清理所需的东西的正确方法.我不想在视图的formclosing事件中清理,看起来很难看.
formclosing ×7
c# ×4
winforms ×4
.net ×1
async-await ×1
delegates ×1
events ×1
java ×1
resources ×1
showdialog ×1
vb.net ×1
wpf ×1