Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

vb.net not making sense

Posted on 2004-09-30
3
Medium Priority
?
243 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

715 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