close the console window

masmith

Member
Joined
Jul 13, 2005
Messages
7
Programming Experience
3-5
Hi,

I'm trying to figure out how to get the console window to close itself after the code has been executed.

My Sub Main() ends with:

Console.ReadLine()

which I seem to need in order to close the window manually without getting a message from windows saying that I can't close it down...

Thanks in advance for the advice.
 
just sits there

thanks for the reply but now the console just sits there... and if I try to manually close it I get a message from windows saying that I shouldn't close it b/c a process may still be running...

any thoughts?
 
If you already have a Console window open then that window will stay open once your program exits. If you simply run your app from a desktop shortcut or the like, it will open a window, execute and close the window when done.

Edit:
This should be what happens. If not then you have done something wonky and code would be in order, as Kulrom suggests.
 
what I do to execute the program is double-click the executable...
here is my code:

Imports
ESRI.ArcGIS.DataSourcesFile
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.Carto
Imports System.Web.Mail
Module Module1

Sub Main()

Dim layers(6, 4) AsString

' Atlas Layer
layers(0, 0) = "atlas" ' name
layers(0, 1) = "atlas" ' shapefile name
layers(0, 2) = "C:\MNS\Assets\Property" ' file location
layers(0, 3) = "roll_ = ' '" ' where expression
layers(0, 4) = 0 ' test number; 0 = look for orphan records

' Buildings Layer
layers(1, 0) = "buildings" ' name
layers(1, 1) = "nccbldg" ' shapefile name
layers(1, 2) = "C:\MNS\Assets\Property" ' file location
layers(1, 3) = "class = ' '" ' where expression
layers(1, 4) = 0 ' test number; 0 = look for orphan records

' Boardwalks Layer
layers(2, 0) = "boardwalks" ' name
layers(2, 1) = "structure_boardwalks" ' shapefile name
layers(2, 2) = "C:\MNS\Assets\Property" ' file location
layers(2, 3) = "asset_id = 0" ' where expression
layers(2, 4) = 1 ' test number; 1 = look for asset_id = 0

' Bridges Layer
layers(3, 0) = "bridges" ' name
layers(3, 1) = "structure_bridges" ' shapefile name
layers(3, 2) = "C:\MNS\Assets\Property" ' file location
layers(3, 3) = "asset_id = 0" ' where expression
layers(3, 4) = 1 ' test number; 1 = look for asset_id = 0

' Dams Layer
layers(4, 0) = "dams" ' name
layers(4, 1) = "structure_dams" ' shapefile name
layers(4, 2) = "C:\MNS\Assets\Property" ' file location
layers(4, 3) = "asset_id = 0" ' where expression
layers(4, 4) = 1 ' test number; 1 = look for asset_id = 0

' Liftstations Layer
layers(5, 0) = "liftstations" ' name
layers(5, 1) = "structure_liftstations" ' shapefile name
layers(5, 2) = "C:\MNS\Assets\Property" ' file location
layers(5, 3) = "asset_id = 0" ' where expression
layers(5, 4) = 1 ' test number; 1 = look for asset_id = 0

' Walls Layer
layers(6, 0) = "walls" ' name
layers(6, 1) = "structure_walls" ' shapefile name
layers(6, 2) = "C:\MNS\Assets\Property" ' file location
layers(6, 3) = "asset_id = 0" ' where expression
layers(6, 4) = 1 ' test number; 1 = look for asset_id = 0

' declare ArcObjects references
Dim pWorkspaceFactory As IWorkspaceFactory
Dim pFeatureWorkspace As IFeatureWorkspace
Dim pFeatureLayer As IFeatureLayer
Dim pFeatureSelection As IFeatureSelection
Dim pQueryFilter As IQueryFilter
Dim pCur As IFeatureCursor
Dim pFeat As IFeature

' misc. variables
Dim sSubjectTxt, sBodyTxt, sAsset_IDs AsString
Dim iFeatCount AsInteger
Dim j AsInteger

Console.Write("starting to process layers..." & vbCrLf)

For j = 0 To layers.GetUpperBound(0)

Console.Write("processing " & layers(j, 0) & "..." & vbCrLf)

sAsset_IDs = ""

'Create a new ShapefileWorkspaceFactory object and open a shapefile folder

pWorkspaceFactory = New ShapefileWorkspaceFactory

pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(layers(j, 2), 0)

'Create a new FeatureLayer and assign a shapefile to it

pFeatureLayer = New FeatureLayer

pFeatureLayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass(layers(j, 1))

pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName

' Select features from the layer based on a query filter

pFeatureSelection = pFeatureLayer

pQueryFilter =
New QueryFilter

pQueryFilter.WhereClause = layers(j, 3)

pFeatureSelection.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew,
False)

iFeatCount = pFeatureSelection.SelectionSet.Count()

'Make sure at least one feature is selected

If iFeatCount < 1 Then

Console.Write("NO features selected in layer " & pFeatureLayer.Name)

ExitSub

EndIf

' depending on the test type send the appropriate e-mail

SelectCase layers(j, 4)

Case 0 ' test for orphaned records

'Get a cursor from the selected features

pFeatureSelection.SelectionSet.Search(Nothing, False, pCur)

' start the body of the email

sBodyTxt = "The following Asset_IDs from " & layers(j, 1) & " have no relation in ORACLE: "

'Loop through the features using the cursor

pFeat = pCur.NextFeature

DoUntil pFeat IsNothing

sAsset_IDs = sAsset_IDs & pFeat.Value(pFeat.Fields.FindField("Asset_id")) & ", "

pFeat = pCur.NextFeature

Loop

' clean up the asset id string, removing the last comma

sAsset_IDs = sAsset_IDs.Remove((sAsset_IDs.Length - 2), 2)

' append Asset IDs to the body text

sBodyTxt = sBodyTxt & sAsset_IDs

' set e-mail subject line

sSubjectTxt = "orphan records in " & layers(j, 1)

Case 1 ' test for records with asset_id = 0

' set the body text of the e-mail

sBodyTxt = "There are " & iFeatCount & " features in " & layers(j, 1) & " that have an Asset ID of 0."

' set e-mail subject line

sSubjectTxt = "invalid Asset IDs in " & layers(j, 1)

EndSelect

' send an e-mail alert about orphans

SendEmail(sSubjectTxt, sBodyTxt)

Next

Console.Write("notification e-mails sent" & vbCrLf)

Console.ReadLine()



EndSub

Sub SendEmail(ByVal subject AsString, ByVal body AsString)

Try

Dim insMail AsNew MailMessage

With insMail

.From = "msmith@ncc-ccn.ca"

.To = "msmith@ncc-ccn.ca"

.Subject = Trim(subject)

.Body = Trim(body)

EndWith

SmtpMail.SmtpServer = "8.2.10.51"

SmtpMail.Send(insMail)

Catch

Console.Write("failed to send e-mail")

EndTry

EndSub

End
Module

 
Basically I want to create an executable that can be run by a nightly process. I went with a console window because I thought it could be called by a batch file, do its thing, and then go away.
By winform do you mean a form with a button on it? I originally made one of those to test the code but it isn't what I want in the long run...
 
well, the body and subject of the e-mail are created by the code, and the To and From e-mail addresses will probably just be hard-coded into the module (as they are now).

if a console is not the way to go what are my options for creating a stand alone .exe?

thanks for your time
 
if you look at my code there is a sub-routine at the bottom that creates and sends the mail. I pass it the subject text and the body text as arguments:

Sub SendEmail(ByVal subject As String, ByVal body As String)

Try
Dim insMail As New MailMessage
With insMail
.From = msmith@ncc-ccn.ca
.To = msmith@ncc-ccn.ca
.Subject = Trim(subject)
.Body = Trim(body)
End With

SmtpMail.SmtpServer = "8.2.10.51"
SmtpMail.Send(insMail)
Catch
Console.Write("failed to send e-mail")
End Try
End Sub


I'm not having any problems with this... it works fine. All I need to know is how this code can be compiled into an executable that a windows scheduled task can execute.
 
I know where the exe is - the whole reason I started this thread was because when I double clicked the exe a console popped up, executed the code, sent a bunch of e-mails and then just sat there. I was hoping it would close itself after finishing.

So let me start again because I am not insisting on using a console, if you can tell me how to do it I'm all ears :)

I just need to create some kind of executable that a scheduled windows task can call in the wee hours of the morning. The only thing it can NOT be is a windows form because that would require someone to click a button on the form to execute the code.

When I start a new project in Visual Studio what kind of project would you suggest I use? It can't be a Windows Application for the reason stated above, and it can't be a web-based project. Maybe it should be a Windows Service....
 
Finally i got you ... Ok, Create a Windows Service and use the System.Timers.Timer class cuz it's built precisely for this task.
Also note that your claim that win form would require someone to click a button ain't true because you could put all that code in load event and send certain email without problem.

Cheers ;)

note: it's easier for me to help you out when i know your final intention/idea and if you tend not to involve me in reading all the code ... just simply say what you need.
 
Back
Top