How to: Use a C # detection application has stopped response (from MSDN)

xiaoxiao2021-03-06  59

The release number of this article has been CHS304991

This article discusses a Beta version of Microsoft products. The information in this article is provided as "as", if there is any change without notice.

For this Beta product, Microsoft does not provide formal product support. For information on gain support for Beta versions, see the documentation included in the beta product file or view the site you downloaded.

This task content

Summary Start Application Determines if the application responds to the shutdown application to generate this example for this example

Summary In some cases, you may need to detect if the application is blocked. For example, when Microsoft Internet Explorer is automatically run, you may want to know if Internet Explorer has stopped responding.

This article describes how to detect if the automatic instance of Internet Explorer has stopped responding (hang) and how to close Internet Explorer. Although this code is written for Internet Explorer and Visual C # .NET, you can also use this approach to other applications.

The first three sections of this article summarizes the three important coding concepts necessary to complete this task. Section IV Demonstrate How to Generate Example Applications.

Back to top

Launching the application This article uses Internet Explorer as a test application. The following code starts Internet Explorer and uses

GetProcessByname method Gets the handle of the process.

Browser = new internetExplorer ();

Browser.visible = true;

Browser.gohome ();

Procs = process.getProcessesbyName ("IExPlore");

Back to top

Determine if the application responds to the following code check

GetProcessByname Method Returns

The first element in the PROCS array to determine if the process responds. This article assumes only an Internet Explorer instance.

IF (Browser! = NULL)

IF (procs [0] .responding)

MessageBox.show ("IExplore is responding";

Else

MessageBox.Show (IExplore is not responding);

Else

MessageBox.show ("IExplore is not running";

Back to top

Close the code below to demonstrate how to turn off the application. If the application still responds, you can use it.

Process

ClosemainWindows method closes it. If the application is not responding, you must call

Kill method. You must use the try-catch block to handle an exception that occurs due to the absence of the process.

Try

{

IF (procs [0] .responding)

Procs [0]. closemainwindow ();

Else

Procs [0] .Kill ();

}

Catch

{

MessageBox.show ("Could Not Find The Iexplore Process");

}

Back to top

Generate sample project

About this example This example This article consists of a form containing the following three buttons:

Start Internet Explorer, start an Internet Explorer instance using Automation. Check if Internet Explorer is tested if the browser responds. Turn off Internet Explorer to close your browser. If you want to thoroughly test the code, you can cause the browser to stop the response web page, you can browse this page after opening the browser. Then, try clicking Checking Internet Explorer and

Close Internet Explorer. Wait a little after clicking these buttons; if the browser does not respond, no response immediately.

Back to top

Generate an example step

Start a new Windows C # application in Visual Basic .NET. In the Solution Explorer window, right-click the reference, and then click Add Reference. On the COM tab in the Add Reference dialog box, click Microsoft Internet Controls, and then click Select. Click OK to close the Add Reference dialog. Note: If you see the following dialog box, click Yes to create the package you want:

Could not find a primary interop assembly for the COM component 'Microsoft Internet Controls'.A primary interop assembly is not registered for this type library.Would you like to have a wrapper generated for you? (Can not find the COM component "Microsoft Internet Controls "The main interop assembly. Not registered an Interop assembly for this type of library. Want to generate a package?)

If you do not see this dialog, you may have an intended InteroP package. : If you see the following dialog box, click in the Solution Explorer window, right-click Form1.cs, and then click View Code. : If you see the following dialog box, click Remove all the code in the Form1.cs code window. : If you see the following dialog box, click Paste the following code to the Form1.cs code window: use system;

Using system.drawing;

Using system.collections;

Using system.componentmodel;

Using system.windows.forms;

Using system.data;

Using shdocvw;

Using system.diagnostics;

Namespace AppstoppedResponding_CS

{

Public Class Form1: System.Windows.Forms.form

{

Private system.windows.Forms.Button btnStart;

Private system.windows.Forms.Button Btncheck;

Private system.windows.Forms.Button BtnClose;

Private system.componentmodel.Container Components = NULL;

Private internetExplorer Browser;

Protected process [] procs;

Public Form1 ()

{

InitializationComponent ();

}

Protected Override Void Dispose (BOOL Disposing) {

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

#Region Windows Form Designer Generated Code

Private vidinitiRizeComponent ()

{

This.btncheck = new system.windows.Forms.Button ();

This.btnStart = new system.windows.Forms.Button ();

This.btnclose = new system.windows.Forms.Button ();

THIS.SUSPENDLAYOUT ();

//

// btncheck

//

This.btncheck.location = new system.drawing.point (66, 122);

this.btncheck.name = "btncheck";

this.btncheck.size = new system.drawing.size (152, 23);

this.btncheck.tabindex = 1;

this.btncheck.text = "Check Internet Explorer";

THIS.BTNCHECK.CLICK = New System.EventHandler (this.btncheck_click);

//

// btnStart

//

This.btnStart.location = new system.drawing.point (66, 74);

this.btnStart.name = "btnStart";

This.btnStart.size = new system.drawing.size (152, 23);

this.btnStart.tabindex = 0;

This.btnStart.text = "Start Internet Explorer";

THIS.BTNSTART.CLICK = New System.EventHandler (this.btnstart_click);

//

// btnClose

//

This.btnclose.location = new system.drawing.point (66, 170);

this.btnclose.name = "btnclose";

This.btnclose.size = new system.drawing.size (152, 23);

this.btnclose.tabindex = 2;

this.btnclose.text = "Close Internet Explorer";

This.btnclose.click = new system.eventhandler (this.btnclose_click);

//

// Form1

//

THIS.AUTOSCALEBASESIZE = New System.drawing.size (5, 13);

THIS.CLIENTSIZE = New System.drawing.size (292, 266);

this.controls.addrange (new system.windows.forms.control] {

this.btnclose,

This.btncheck, this.btnStart};

THIS.NAME = "Form1";

THIS.TEXT = "Form1";

This.Closing = new system.componentmodel.canceleventhandler (this.form1_closing);

This.Load = New System.EventHandler (this.form1_load);

This.ResumeLayout (false);

}

#ndregion

// Example Code

/// The main entry point for the application.

[Stathread]

Static void main ()

{

Application.run (New Form1 ());

}

Private void BTNStart_Click (Object Sender, System.Eventargs E)

{

Browser = new internetExplorer ();

Browser.visible = true;

Browser.gohome ();

Procs = process.getProcessesbyName ("IExPlore");

// Build Event delegate to catch browser close event.

SHDOCVW.DWEBBROWSEREVENTS2_ONQUITEVENTHANDLER ODELEGATE = New

SHDOCVW.DWEBBROWSEREVENTS2_ONQUITEVENTHANDLER (Browser_onquit);

Browser.onquit = ODELEGATE;

}

Private void btncheck_click (Object Sender, System.Eventargs E)

{

IF (Browser! = NULL)

IF (procs [0] .responding)

MessageBox.show ("IExplore is responding";

Else

MessageBox.Show (IExplore is not responding);

Else

MessageBox.show ("IExplore is not running";

}

Private void btnclose_click (Object Sender, System.Eventargs E)

{

THIS.CLOSEBROWSER ();

}

Private void form1_closing (Object Sender, System.comPonentmodel.canceleventargs E)

{// if The Browser is Still Open, Close It When The form closes.

IF (Browser! = NULL)

THIS.CLOSEBROWSER ();

}

Private void closebrowser ()

{

Try

{

IF (procs [0] .responding)

Procs [0]. closemainwindow ();

Else

Procs [0] .Kill ();

// deStroy Object Reference and arrright.

THIS.CLANUP ();

}

Catch

{

MessageBox.show ("Could Not Find The Iexplore Process");

}

}

Private void browser_onquit ()

{// Event Delegate - Clean Up if The Browser is Closed ManuallyThis.cleanup ();

}

Private void cleanup ()

{// Destroy Browser Reference Variable and arrrey.

Browser = null;

Procs = NULL;

}

Private Void Form1_Load (Object Sender, System.EventArgs E)

{/ * required method * /}

}

} Note: The above code is automatically drawn, locizes, and nams three buttons on the form, so you don't have to add them manually. : If you see the following dialog box, click this code to the Form1.cs code window, you may want to fold the area labeled "Windows Form Designer Generated Code". : If you see the following dialog box, click F5 to generate and run the project. : If you see the following dialog box, click the button on the Internet Explorer, click the button on the form to test these code.

Back to top

Referind about additional information, click the article number below to view the article in the corresponding Microsoft Knowledge Base:

CHS304990 uses Visual Basic. NET detects whether the application has stopped responding

Back to top

The information in this article applies to:

Microsoft Visual C # .NET Beta 2

Recent Updated: 2001-10-25 (1.0) Keyword Kbhowto KbhowTomaster KB304991

转载请注明原文地址:https://www.9cbs.com/read-109045.html

New Post(0)