Solved

vb.net not making sense

Posted on 2004-09-30
3
221 Views
Last Modified: 2010-04-23
ive been looking into multi threading, and ran across this example.  what it does is makes a progress bar move back and forth while you are still able to click the buttons on the form.  

heres the code.  if you want to paste this in a blank form, and remember to import the threading header file, it should work

    Public Sub pbar()
        Randomize()

        Me.ProgressBar1.Minimum = 1
        Me.ProgressBar1.Maximum = 100
        Me.ProgressBar1.Value = 50
        Dim num As Integer

        While 1
            num = (Rnd() * 23)
            If (num Mod 2) = 1 Then
                If Me.ProgressBar1.Value > 1 Then Me.ProgressBar1.Value -= 1
            Else
                If Me.ProgressBar1.Value < 99 Then Me.ProgressBar1.Value += 1
            End If
            Me.Refresh()
        End While
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim InstanceCaller As New Thread(AddressOf pbar)
        InstanceCaller.Start()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MsgBox("test")
        End
    End Sub

as you can see the while loop is really the heart of the program, and it works perfectly.  but what i dont understand is, if you replace          

If (num Mod 2) = 1 Then

with

If ((Rnd() * 23) Mod 2) = 1 Then

the if statement always fails, and it goes strait to 100%

what i dont understand, is that it is just a simple substitution, but alas, its not.

thanks!

p.s. the number 23 is arbitrary, no rhyme or reason to that choice.

~b
0
Comment
Question by:bramsquad
  • 2
3 Comments
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12194659
Why don't you make "Button1.Enabled=False" during that time ?
0
 
LVL 8

Author Comment

by:bramsquad
ID: 12194732
i hope im not misunderstanding your answer, but i dont think thats the issue

the issue is not with it working...the issue is that it treats what seems like to similar pieces of code differently.

the value of num = (Rnd() * 23)

so

If (num Mod 2) = 1 Then

should be equivalent to

If ((Rnd() * 23) Mod 2) = 1 Then

but its not...
0
 
LVL 18

Accepted Solution

by:
DotNetLover_Baan earned 125 total points
ID: 12194842
lol.. I didn't even read ur whole question...
well, why this is happening... is pretty straight forward. Use this,  ((Cint(Rnd() * 23)) Mod 2) = 1
This will explain everything. "num" is integer. So it is getting the integer value out of    Rnd() * 23.  So in order to make it work same, convert it to integer.
-Baan
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question