Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / May 2006

Tip: Looking for answers? Try searching our database.

How to interrupt a running program

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
James MA - 09 May 2006 13:31 GMT
I'm writing a program to handle multiple tasks in a single batch, which may
take few minutes to complete all tasks.  To reduce the impact to user, no
windows form will be displayed, instead I have create a NotifiyIcon for the
program, it can show the current status whenever user put the mouse over the
tray icon.

As it takes time to complete all tasks, user may want to terminate the job
for some reason.  I have added the ContextMenu for the NotifyIcon, and user
can choose the option to terminate the job.  Everything seems OKay, and I can
call the menu when the program started before I trigger the job (the job will
starte 1 minute after the proram executed).

However, if the job is started, the NotifyIcon can show the status when user
put the mouse over the icon, but there has no response when i clicked on the
icon, the menu does not show up.  If i remove the application.exit() after I
complete all task, and let the icon remain here.  I found that the menu will
pop up after i finish the task.

I have try to add some code in the loop after each task, to check if user
attempt to stop it.  But it seems that all event on the notifyicon will not
execute unless the function is completed.  So, i cannot set the viriable to
stop the job after one task.  When I'm using other language before, I can add
a function like yield() inside my program to release the CPU, so that i can
handle other events.  

I'd like to know if VB.Net has similiar function to allow the CPU to handle
other event of my program (e.g. mouse click on the notifyicon) while it's
inside a function.

Thanks in advance!
Chris Dunaway - 09 May 2006 14:10 GMT
In VB that function is Application.DoEvents().  But you might also want
to spawn a separate thread to handle your time consuming task so the UI
(in this case the tray icon) will remain responsive.
James MA - 09 May 2006 16:01 GMT
Thanks a lot!

I have no experience in program with thread.  Just read the help from MSDN
("Creating Threads and Passing Data at Start Time "), and I have modified my
program by replacing the orignal script for running all task as,

 Dim myThread As New Thread(AddressOf Me.RunAllTask)
 myThread.Start()

Then put the original code in the function RunAllTask.

   Private Sub RunAllTask()
    :
    <original codes to execute the tasks>
    :
       NotifyIcon.Visible = False
       Me.Close()
       Application.Exit()
   End Sub

As i will close the application after all task completed, i put exit codes
at the end.

I have tested the program, it works fine.  And I can now put the code to
terminate the loop in the menu.

Is it the way you want me to changed?  May I know if there has any error
handling required in this changes?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.