[Question]Events

Posts 115 of 22 · Page 1 of 2
[Question]Events
Is there an event in vb that will last from the start till it will be closed?
And by event i mean, form_load, form_closing etc etc...

Cuz I want my trainer to be doing something while it is in use, ie, i want my trainer to search for files while it is in use.

Is there such a event?
Make your own event in a separate thread with an infinite loop?

[php]
Private Sub MyLoop()
Do
'//something here
Loop
End Sub
[/php]

Then to make a new thread for this infinite loop:

[php]
CheckForIllegalCrossThreadCalls = False
Dim loopThread As New Threading.Thread(AddressOf MyLoop)
loopThread.Priority = Threading.ThreadPriority.BelowNormal '//I set below normal priority because you don't want it to hog all the memory'
loopThread.Start()
[/php]

Alternatively, you could use a backgroundworker which is similar.
Can you explain those codes , since im still a noob at vb.

And does backgroundworker does it job till the event is done?

Since, i want my application to search files in all drives, and deletes the specific file.
I want it to be done while the application is in use, not in form load since it will take time.

Quote Originally Posted by .Celtics View Post
Can you explain those codes , since im still a noob at vb.

And does backgroundworker does it job till the event is done?

Since, i want my application to search files in all drives, and deletes the specific file.
I want it to be done while the application is in use, not in form load since it will take time.

A Background worker is probably your best bet then. Add a BGW control to your form, add the code you want to execute to it's main event (the event that pops up when you double click the control) Then on form_load you can do

BackGroundWorker1.RunWorkerAsync()
Okay thanks

Now a new question...
Can you tell me how to search for an application(searches through filename) in all drives?
And if it exist, it will delete it

Thanks
Took a bit of playing, but this will get all available drives on your computer,

[php]
For Each s As IO.DriveInfo In My.Computer.FileSystem.Drives
MsgBox(s.Name)
Next
[/php]

So now you know how to get all the drives on the computer.

[php]
For Each s As String in FileIO.FileSystem.GetFiles(s, FileIO.SearchOption.SearchAllSubDirectories, "filename.txt")
FileIO.FileSystem.Delete(s)
Next
[/php]

Note: The seconds snippet is untested, I just made it up then so you'll have to double check it works.
Thanks, ill try them later (currently playing)
Hope it works
Multithreading gives programs the ability to do several things at a time. Each stream of execution is called a thread. Multithreading is used to divide lengthy tasks into different segments that would otherwise abort programs. Threads are mainly used to utilize the processor to a maximum extent by avoiding it's idle time. Threading lets a program seem as if it is executing several tasks at once. What actually happens is, the time gets divided by the computer into parts and when a new thread starts, that thread gets a portion of the divided time. Threads in VB .NET are based on the namespace System.Threading.
Source: StartVBDotNet

-----------------------------------------------

Adding a bit of my knowledge:

So basically, you can do multiple things at once using this. A backgroundworker is nothing more than a multithread as a control. Let's say you are trying to connect to a database. This will for sure take 2-4 seconds(unless it is localhost you are connecting to).

Button_Click

[php]Try
conn.open

conn.close
Catch ex as mysqlexception
end try[/php]

Now, this would freeze the application while it is trying to connect. To avoid this freezing and to run even more subs/actions at the same time, you can use multithreading or backgroundworker.

We create a sub for what we want to do:

[php]Private Sub Connect
Try
conn.open

conn.clse
Catch ex as mysqlexception
end try
End sub[/php]

Button_Click

[php]dim t as new threading.thread(addressof Connect)
t.start

'Further Code[/php]

The application will not freeze and your further code will be executed after the thread is started, not when the tread is done

As you see, multithreading can come in very handy. Especially in larger progresses.
Im having an error with your code Jason.
I removed the msgbox, since i dont want them to know im searching for something.

The error is in the deleting part


Do you know whats the problem?
And another error poped-up
Quote Originally Posted by .Celtics View Post
And another error poped-up
Use variable s after GetFiles

So it becomes GetFiles(s)
Too lazy to make another SS.

Theres another error, blue error line in (s)
"Value type of 'System.IO.DriveInfo' cannot be converted to 'string'"
Quote Originally Posted by .Celtics View Post
Too lazy to make another SS.

Theres another error, blue error line in (s)
"Value type of 'System.IO.DriveInfo' cannot be converted to 'string'"
Make it:

(S.Name)
Its working now

And can you help me put it in an IF function
Since it will have an error if the file doesnt exist..

Thanks
Posts 115 of 22 · Page 1 of 2
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?