How to interrupt a running program

G

Guest

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!
 
C

Chris Dunaway

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.
 
G

Guest

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top