VB.NET Windows Service Main Code - vb.net

I am trying to learn how a Windows Service works using vb.net and I have no clue where to put my main code. what I mean by main code is the code that needs to be run every couple of seconds. So far, I was able to write a text file using onStart method provided by the service its self. I have installed the service manually and made it run.
do i need to create some kind of threading that trigger the main code? and in which method.
Thanks.

Use a Timer. They start an event on every tick. The tick can be modified to the length you want it to be.(Interval)
Here's a great article/tutorial on them: http://www.dreamincode.net/forums/topic/58033-using-timer/
Add a timer to your application by going to the toolbox and clicking a Timer and adding it to the form. Then go to the properties and add what interval you would like it to do an event(in milliseconds) the set the enabled property to true.
Click on the timer icon below the form designer and you will be directed to the code editor with the Timer_Tick event. Then in when I put the comment 'what you want it to do, this is the code that will be executed every tick(the interval what you selected, for example 1000 is a second)
Code:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'what you want it to do
End Sub
If I am being unclear here is a tutorial: https://www.youtube.com/watch?v=6wWZIuOAyM4

Related

How can I run code every time a form appears (after being hidden instead of closed)?

I am building a program that allows you to build a custom PC. Every time a user chooses a part to add to their list, the program returns to a form containing their list. I need the program to check for compatibility issues every time this list form appears. How can I run a block of code every time a form is shown?
The form does not get closed, it only gets hidden.
If you're in Visual Studio 17, if you double click your user form in the Designer mode you'll be taken to the code view and a sub will be created saying something like Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load. Anything you put in this sub will run when the form loads.

Nothing works anymore after building project in vb.net

I was going to build my project when i noticed i didn't put an icon,
and since i couldn't access the icon value of the original form because i used a theme i C+X the container and access it from the grey form, change it, C+V the container, built the project.
Nothing changed, all the name of the buttons and stuff are the same, but i feel like nothing is connected to the code anymore, i don't know what happened, i just recently got re-interested into coding, and i have no idea what to do, i tried some things but nothing worked, so here i am, desperate (i spent 3 days on this, i'm REALLY starting from bottom)
link to the project: http://www.mediafire.com/file/2zrbe32lzpx2qhz/SchedulerProjectVBNET.rar
Thanks in advance
It sounds like you have lost all the Handles clauses off your event handlers. As an example, if you add a Button to your form and double-click it, you will get a Click event handler like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Note the Handles clause at the end, which is what connects the code to the event of the control. If you cut that Button from your form to the Clipboard then it no longer exists as part of the form, so that Handles clause will be automatically removed. If you paste the Button back onto the form, the Handles clause is not restored, so the method no longer handles the event.
If that's what has happened - which you can easily check just by looking at the code - then you need to restore all those Handles clauses. You can do that manually, i.e. write them all yourself in the code window, or you can have the designer regenerate them for you. To do the latter, select a control in the designer, open the Properties window, click the Events button, select the desired event and then select the appropriate method from the drop-down list.
Note that you can also double-click an event there to generate a new event handler, which can be useful for handling events other than the default event. You can generate a handler for the default event simply by double-clicking the control.

Why doesn't a MsgBox trigger a lost focus event VB.net?

I have a timer that each time displays a message box saying "Hello." I also have the code configured so whenever the window loses focus, it should stop the timer that keeps the boxes coming. However, they keep coming.
I have tried a similar thing in a similar program with way too long of code to post here, but what it did was it paused the first time, stop the timer, and when the timer was stopped again, it didn't work correctly. There was also some other code there that had a random element, that displayed a different prompt when a certain number was generated, but once it was generated, it kept using that same different prompt every time.
Is this a error of not enough time to process all the code and it "overlaps" some? I can delay the timer without that much different effects, but I think that my [lower end] CPU that it is running this program on, that with 1.6 GHz that it could handle a timer with a few message boxes. Though, VS is running at the same time, but I shouldn't have to export my code and close VS everytime that I need to test it.
If the problem is not enough time, is there a way that I can prevent my program from "multithreading" or whatever it is doing? It seems like a weird problem, but computers are very weird too. :P
Edit:
By "Focus" I mean the selected window that is the most apparent. For example, my browser is now "focused." I have been informed that the correct term is "selected." I must have been using the wrong type of event trigger... :P
It doesn't generate a lost-focus event because the form doesn't have the focus in the first place. A control on the form always gets the focus, like a Button or TextBox. You could use the Deactivate event instead.
Or just not display the message box when the Tick event fires again. Roughly:
Private ShowingMsgBox As Boolean
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'' Do stuff
''
If Not ShowingMsgBox Then
ShowingMsgBox = True
MsgBox("yada")
ShowingMsgBox = False
End If
End Sub
The underlying reason for this behavior is that MsgBox pumps a message loop. It keeps normal Windows messages getting delivered, like WM_PAINT that keeps the windows painted. And WM_TIMER, the one that generates the Tick event. The only kind of messages that it blocks are input events, mouse and keyboard messages. Otherwise the reason that Application.DoEvents() is so very dangerous. It does the same thing as MsgBox() does, without disabling input.
Create a new project with a Timer (Timer1) and write this code:
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
If (Me.Focused) Then
MessageBox.Show("Hello")
End If
End Sub
If you put the mouse over your form, you would see that a message box will popup after the given Interval is over. If you don't click on the accept button and keep the mouse on the form, you would see that no further messages appear: Me.Focus is False. If you click on the accept button, the messages would start poping up; you don't even need to select the form (the focus is transferred automatically from the MessageBox to the Form).
Summary: the MessageBox does make the Form to lose the focus, although it is a kind of a "tricky" lost as far as will automatically come back after clicking on the accept button.
UPDATE: the proposed configuration does trigger a LostFocus event of the form:
Private Sub Form1_LostFocus(sender As Object, e As System.EventArgs) Handles Me.LostFocus
MsgBox("lost")
End Sub
Unlikely the other answers/comments, what I understood from your question is that you want to know the reason and if this is a normal behaviour, rather than getting a working solution to make the form to lose the focus (you are not even describing the exact conditions under which you want this to happen).

vb.net activated fires multiple times

I want to update a database based on the form that is currently activated. I had originally decided to use the GotFocus event. However I now understand that will not work as the form has controls on it. So I then thought I wouls use the activated event. This works but seems to fire multiple times. I put in the following code:
Private Sub frmNewTicket_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
MsgBox("Form Activated")
End Sub
I select the form and make it activated and the message box appears about 15 times.
Why does it do this? How should I handle this. I only want my code to execute once when the form is activated.
NOTE: There are several forms that the users will be changing between, incuding forms from other applications.
Each time you click OK on the messagebox, the form regains the focus and is activated again.
Put a static Boolean value in your frmNewTicket_Activated like someone has posted here:
Static HasRan As Boolean=False
If Not HasRan Then
HasRan=True
'put code here
End If
It sounds like you are wanting to do something everytime your form gets activated. The Form Activated event will work fine as long as what you are doing doesn't pull focus from the Form which will then trigger another Activation event when the Form gets focus again. Try using something other than a MessageBox for testing like Beep or changing the Form's Color

what is function that i can use instead of Do events (used in vb6.0) in (vb2010)

for a=0 to 1000
lable.text=a
next
Above loop does not update the text status of lable, while loop is running, it updates only at the end of for loop, but I tried with vb6.0 it is possible by using Do events. But I don't know what is the function in VB2010 beta 2.
Any ideas on how to implement this loop in VB2010?
You could try
Application.DoEvents()
Or even
lable.Refresh()
You need to change the way you think about this sort of problem in .NET - DoEvents was a hack to deal with the lack of threading in VB, useful but not a good solution.
To run a processor intensive task while keeping the UI responsive you need to use multi-threading, you kick off the intensive work in a separate thread and update the UI in the main thread.
Easiest way to do this is using the BackgroundWorker class http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker(VS.100).aspx
Your loop would raise ReportProgress events which your UI code would use to update the label.
You can use Application.DoEvents() in .Net - but you really probably shouldn't. It can cause all sorts of strange behaviour that will be a nightmare to debug. I've seen button's OnClick event firing when the button is disabled (as an example) and ultimately traced it back to calling DoEvents().
If your goal is really just to count and update a label - use a Timer.
If you are doing meaningful work - you should look into multithreading.
The BackgroundWorker class makes this pretty darn easy - you can have it report progress and update your control. If you aren't using an old version of the .NET Framework - you can use a Task as well.
Having said all of that - no matter what approach you take - you won't be able to see much of anything inside a loop like you've provided in your example. It will simply happen too fast (unless you use a timer and set the Tick value appropriately).
Example Using a Timer
The easiest way to do this is in the Form Designer - drag a Timer Component (under the Components group) onto your form. You can edit the properties and give it an appropriate name and set the Tick value to something like 500 (that's the tick interval in milliseconds). Set the Enabled value to True.
Now click the Timer control, and pull up the Properties window. Click on the Events Icon and double click the Tick Textbox. This should create the default handler for you.
Modify the code to look like this:
Private A As Integer
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
' Update the text
Label1.Text = A.ToString
A += 1
' Stop the timer
If A >= 1000 Then
Timer1.Enabled = False
End If
End Sub
Your question is not very clear, but I'll try to explain what I understood. The loop actually updates it, but it happens so fast that you only see the final value. I don't know the VB equilevant of sleep() method, but you should be able to find it in web pretty quickly.
Its not the text is not getting changed but its getting overwritten when you loop it each time. If you want to display 0 to 1000 in the label then you have to append the value.