
这样是不行的,Windows里面必须维护一个主线程不能结束,不然整个程序就结束运行了,所以你在form1里面显示form2然后又想把form1关掉是不可以的,结果会是整个程序都结束。
所以正确方法是运行form2,在form2里面使用ShowDialog来显示form1,最后根据form1的DialogResult来判断form2还要不要继续显示。
using System;
using SystemCollectionsGeneric;
using SystemComponentModel;
using SystemData;
using SystemDrawing;
using SystemText;
using SystemWindowsForms;
namespace WebBrowserTest
{
// This first namespace is required for the ComVisible attribute used on the ScriptManager class
using SystemRuntimeInteropServices;
using SystemWindowsForms;
// This is your form
public partial class Form1 : Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private SystemComponentModelIContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
componentsDispose();
}
baseDispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
thisWebBrowser1 = new SystemWindowsFormsWebBrowser();
thisSuspendLayout();
//
// WebBrowser1
//
thisWebBrowser1Dock = SystemWindowsFormsDockStyleFill;
thisWebBrowser1Location = new SystemDrawingPoint(0, 0);
thisWebBrowser1MinimumSize = new SystemDrawingSize(20, 20);
thisWebBrowser1Name = "WebBrowser1";
thisWebBrowser1Size = new SystemDrawingSize(544, 300);
thisWebBrowser1TabIndex = 0;
//
// Form1
//
thisAutoScaleDimensions = new SystemDrawingSizeF(6F, 12F);
thisAutoScaleMode = SystemWindowsFormsAutoScaleModeFont;
thisClientSize = new SystemDrawingSize(544, 300);
thisControlsAdd(thisWebBrowser1);
thisName = "Form1";
thisText = "Form1";
thisResumeLayout(false);
}
#endregion
private SystemWindowsFormsWebBrowser WebBrowser1;
// This nested class must be ComVisible for the JavaScript to be able to call it
[ComVisible(true)]
public class ScriptManager
{
// Variable to store the form of type Form1
private Form1 mForm;
// Constructor
public ScriptManager(Form1 form)
{
// Save the form so it can be referenced later
mForm = form;
}
// This method can be called from JavaScript
public void MethodToCallFromScript()
{
// Call a method on the form
mFormDoSomething();
}
// This method can also be called from JavaScript
public void AnotherMethod(string message)
{
MessageBoxShow(message);
}
}
// This method will be called by the other method (MethodToCallFromScript) that gets called by JavaScript
public void DoSomething()
{
// Indicate success
MessageBoxShow("I will close!");
thisClose();
}
// Constructor
public Form1()
{
// Boilerplate code
InitializeComponent();
// Set the WebBrowser to use an instance of the ScriptManager to handle method calls to C#
WebBrowser1ObjectForScripting = new ScriptManager(this);
// Create the webpage
WebBrowser1DocumentText = @"<html>
<head>
<title>Test</title>
</head>
<body>
<input type=""button"" value=""Go!"" onclick=""windowexternalMethodToCallFromScript();"" />
<br />
<input type=""button"" value=""Go Again!"" onclick=""windowexternalAnotherMethod('Hello');"" />
</body>
</html>";
}
}
}
你这样做是没用的,你这个要完全关闭的话,需要做几件事情:
第一:Abort你创建的并且还在运行的线程,
第二:关闭你的Socket对象,也就是关闭你的连接
第三:请调用EnvirmentExit(0);的方式进行程序的退出!!
温馨提示:系统内置的退出方法只会关闭当前线程(主线程)的所有动作并释放资源,如果你创建了其他的线程,而且这些子线程还在运行,那么程序将等待你的所有子线程运行结束后,然后关闭进程,如果你的子线程中出现死循环的 *** 作,那么你的程序将永远不会退出!!就就造成你的你这个关不掉的情况!!
以上就是关于(c#)如何关闭启动窗体而不关闭程序全部的内容,包括:(c#)如何关闭启动窗体而不关闭程序、c# 网站嵌套在winform里面 那么如何通过网站怎么关闭winform程序、winform安全退出程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)