13 Replies - 1161 Views - Last Post: 01 February 2013 - 12:08 PM Rate Topic: -----

#1 iamcpc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 15-January 13

VS 2008 Sleep() not working and I can't figure out why

Posted 29 January 2013 - 10:15 PM

I have created a test application to test my multithreading by creating threads which put the numbers 1 to 100 in columns of an excel spreadsheet. I use a form1 button click to start the threads. One thread starts and then, after 2 seconds the next thread starts so I can see all the threads running at the same time

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim testthread As New System.Threading.Thread(AddressOf Module1.loop3)
        Dim testthread1 As New System.Threading.Thread(AddressOf Module1.loop3)
        Dim testthread2 As New System.Threading.Thread(AddressOf Module1.loop3)
        Dim testthread3 As New System.Threading.Thread(AddressOf Module1.loop3)
        Dim testthread4 As New System.Threading.Thread(AddressOf Module1.loop3)
        testthread.Start(1)
        System.Threading.Thread.Sleep(2000)

        testthread1.Start(2)
        System.Threading.Thread.Sleep(2000)
        testthread2.Start(3)
        System.Threading.Thread.Sleep(2000)
        testthread3.Start(4)
        System.Threading.Thread.Sleep(2000)
        testthread4.Start(5)
        System.Threading.Thread.Sleep(2000)
        Call Module1.loop3(6)
    End Sub
End Class



Public Module Module1
    Dim xl = CreateObject("excel.application")
    Delegate Sub testo()




    Public Sub loop3(ByVal column As Integer)




        If xl.visible <> True Then
            xl.visible = True
        End If



        Try
            If Not xl.activeworkbook.name = "Spenddown IN Mdcd.xlsm" Then
                xl.workbooks.open("C:\Documents and Settings\BMcGuir1\Desktop\Test\spenddown in mdcd.xlsm")
                GoTo done
            End If

        Catch
            xl.workbooks.open("C:\Documents and Settings\BMcGuir1\Desktop\Test\spenddown in mdcd.xlsm")
        End Try

done:






        Dim y As Long
        For y = 1 To 100
            xl.activeworkbook.sheets("sheet1").cells(y, column).value = y
            System.Threading.Thread.Sleep(100)
        Next
        'MsgBox("done")
    End Sub

End Module



I have this EXACT same code on form2 on another project and the sleeps between the thread starts don't work. When I run it on another project all 6 threads start after the buttonclick event is over. when 6 threads try to open an excel spreadsheet at the same time problems start. Why is sleep() working on one application but not another? Any help is appreciated. I tried to attach the project in which sleep is not working but the server returned an error during upload. If you would like I can e-mail you a copy.

Is This A Good Question/Topic? 0
  • +

Replies To: VS 2008 Sleep() not working and I can't figure out why

#2 kai_itz me  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 159
  • Joined: 03-August 12

Re: VS 2008 Sleep() not working and I can't figure out why

Posted 30 January 2013 - 12:02 AM

CHANGE xlsm TO xlsx IN ALL THREE PLACES.



Quote

Try
If Not xl.activeworkbook.name = "Spenddown IN Mdcd.xlsm" Then
xl.workbooks.open("C:\Documents and Settings\BMcGuir1\Desktop\Test\spenddown in mdcd.xlsm")
GoTo done
End If

Catch
xl.workbooks.open("C:\Documents and Settings\BMcGuir1\Desktop\Test\spenddown in mdcd.xlsm")
End Try


ALSO CHANGE YOUR SLEEP TIMER. coz (100) will not make differece. its a very short time interval

Quote

System.Threading.Thread.Sleep(100)


System.Threading.Thread.Sleep(2000)

that will let u see some time difference of 2 scc.

ACTUALLY IM NOT SURE WHAT EXECTLY U WANT.. IF I HAVE TAKEN SOMETHING WRONG PLESE DO CORRECT ME..

This post has been edited by kai_itz me: 30 January 2013 - 12:11 AM

Was This Post Helpful? 0
  • +
  • -

#3 iamcpc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 15-January 13

Re: VS 2008 Sleep() not working and I can't figure out why

Posted 30 January 2013 - 08:13 AM

What i'm asking is why System.Threading.Thread.Sleep(2000) in my code and in my project is not sleeping the thread for 2 seconds
Was This Post Helpful? 0
  • +
  • -

#4 iamcpc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 15-January 13

Re: VS 2008 Sleep() not working and I can't figure out why

Posted 30 January 2013 - 09:10 AM

View Postkai_itz me, on 30 January 2013 - 12:02 AM, said:

CHANGE xlsm TO xlsx IN ALL THREE PLACES.



Quote

Try
If Not xl.activeworkbook.name = "Spenddown IN Mdcd.xlsm" Then
xl.workbooks.open("C:\Documents and Settings\BMcGuir1\Desktop\Test\spenddown in mdcd.xlsm")
GoTo done
End If

Catch
xl.workbooks.open("C:\Documents and Settings\BMcGuir1\Desktop\Test\spenddown in mdcd.xlsm")
End Try


ALSO CHANGE YOUR SLEEP TIMER. coz (100) will not make differece. its a very short time interval

Quote

System.Threading.Thread.Sleep(100)


System.Threading.Thread.Sleep(2000)

that will let u see some time difference of 2 scc.

ACTUALLY IM NOT SURE WHAT EXECTLY U WANT.. IF I HAVE TAKEN SOMETHING WRONG PLESE DO CORRECT ME..




The system.threading.thread.sleep(2000) is not sleeping the thread for 2 seconds. I can e-mail you the application if you want to show you the code.
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9392
  • View blog
  • Posts: 35,271
  • Joined: 12-June 08

Re: VS 2008 Sleep() not working and I can't figure out why

Posted 30 January 2013 - 09:17 AM

Quote

I can e-mail you the application if you want to show you the code.

Keep the discussions and relevant information on the thread so everyone has a chance to help and/or benefit from what is found.
Was This Post Helpful? 0
  • +
  • -

#6 iamcpc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 15-January 13

Re: VS 2008 Sleep() not working and I can't figure out why

Posted 30 January 2013 - 09:19 AM

View Postmodi123_1, on 30 January 2013 - 09:17 AM, said:

Quote

I can e-mail you the application if you want to show you the code.

Keep the discussions and relevant information on the thread so everyone has a chance to help and/or benefit from what is found.


I had an upload error over and over again when I tried to attach the application. Is there any way that I can send this application to someone else and have them attach it?
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9392
  • View blog
  • Posts: 35,271
  • Joined: 12-June 08

Re: VS 2008 Sleep() not working and I can't figure out why

Posted 30 January 2013 - 09:47 AM

There are plenty of sites you can drop the all into... or you can just paste the code here.
Was This Post Helpful? 0
  • +
  • -

#8 iamcpc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 15-January 13

Re: VS 2008 Sleep() not working and I can't figure out why

Posted 30 January 2013 - 10:32 AM

View Postmodi123_1, on 30 January 2013 - 09:47 AM, said:

There are plenty of sites you can drop the all into... or you can just paste the code here.


Here's the application in which sleep is not working.


http://www.yourfilel....php?fid=831569
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9392
  • View blog
  • Posts: 35,271
  • Joined: 12-June 08

Re: VS 2008 Sleep() not working and I can't figure out why

Posted 30 January 2013 - 10:39 AM

Ah.. a "testapplication.vbproj" is not your application. That is *a* file that helps make up a solution. Again.. just post your code here.
Was This Post Helpful? 0
  • +
  • -

#10 iamcpc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 15-January 13

Re: VS 2008 Sleep() not working and I can't figure out why

Posted 30 January 2013 - 10:45 AM

View Postmodi123_1, on 30 January 2013 - 10:39 AM, said:

Ah.. a "testapplication.vbproj" is not your application. That is *a* file that helps make up a solution. Again.. just post your code here.

form 1:



Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim frm3 As New Form2




    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       
        Form2.Show()


    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim frm2 As New Form2
        frm2.Show()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        frm3.Show()

    End Sub

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        loaded = True
    End Sub

    'Public Delegate Sub navigatecallback()
    'Public Sub navigate()
    '    If Me.WebBrowser1.InvokeRequired Then
    '        Me.WebBrowser1.Invoke(New navigatecallback(AddressOf navigate))

    '       Else
    '           Me.WebBrowser1.Navigate(site1)
    '           System.Windows.Forms.Application.DoEvents()
    '       End If
    '   End Sub


    '  Public Sub dowork()
    '      navigate()
    ' End Sub

    Private Sub WebBrowser1_Navigated(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatedEventArgs) Handles WebBrowser1.Navigated
        loaded = False
    End Sub

    Private Sub WebBrowser1_Navigating(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating
        loaded = False
    End Sub
End Class




Public Class Form2








       Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub


 




    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim testthread As New System.Threading.Thread(AddressOf Module1.loop3)
        Dim testthread1 As New System.Threading.Thread(AddressOf Module1.loop3)
        Dim testthread2 As New System.Threading.Thread(AddressOf Module1.loop3)
        Dim testthread3 As New System.Threading.Thread(AddressOf Module1.loop3)
        Dim testthread4 As New System.Threading.Thread(AddressOf Module1.loop3)
        testthread.Start(1)
        System.Threading.Thread.Sleep(2000)

        testthread1.Start(2)
        System.Threading.Thread.Sleep(2000)
        testthread2.Start(3)
        System.Threading.Thread.Sleep(2000)
        testthread3.Start(4)
        System.Threading.Thread.Sleep(2000)
        testthread4.Start(5)
        System.Threading.Thread.Sleep(2000)
        Call Module1.loop3(6)

    End Sub



I already posted the code for module1.loop3()
Was This Post Helpful? 0
  • +
  • -

#11 kai_itz me  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 159
  • Joined: 03-August 12

Re: VS 2008 Sleep() not working and I can't figure out why

Posted 30 January 2013 - 10:40 PM

Actually the above code is working perfect to me.

i just want to confirm ..

if u press button_2 in form1 so exel file should be open and values should enter with sleep time u have mentioned.(similar to form2 with button_5)

is that u want ? if so that is working for me .

PLESE EXPLAIN WHAT U WANT ?
AND IF U GETTING ANY ERROR PLESE MENTION THAT ERROR

NOTE : YOUR FILE MDCD SHOULD BE OF XLSM FORMAT IN YOUR TEST FOLDER ON YOUR DESKTOP.
STEPS:
1)OPEN EXEL
2)SAVE AS EXCEL MACRO-ENABLED WORKBOOK

This post has been edited by kai_itz me: 30 January 2013 - 10:44 PM

Was This Post Helpful? 0
  • +
  • -

#12 iamcpc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 15-January 13

Re: VS 2008 Sleep() not working and I can't figure out why

Posted 31 January 2013 - 01:37 PM

The problem was the loaded boolean changed by the form1.webbrowser1 document completed event.

That was a public variable declared in module1 which affected how my code ran. Can anyone help me understand why this is?
Was This Post Helpful? 0
  • +
  • -

#13 IronRazer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 329
  • View blog
  • Posts: 995
  • Joined: 01-February 13

Re: VS 2008 Sleep() not working and I can't figure out why

Posted 01 February 2013 - 12:04 PM

Hello iamcpc,
I have run into a problem kind of like yours. If you have ever noticed that when you
open a page in a webbrowser it loads some parts of the page and stops for a second and
then starts and stops a few times before the page is really done loading. Every time your
webbrowser stops it tells your program that it is done loading. Then it sets your boolean
before you want it to. Here is some code showing a quick fix i used in one of my programs.
It may become unreliable if the timer is set to low. I used 4 seconds for mine and it
worked good for me. Maybe you can get a few ideas of how to fix your program from it.

    Private Sub WebBrowser1_ProgressChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserProgressChangedEventArgs) Handles WebBrowser1.ProgressChanged
        'Every time the webbrowser stops for a second it will say its done and
        'execute this event. This will happen a few times before the webbrowser
        'is really done loading.

        'Stop the page loaded timer and start it counting from 0 again
        Timer_PageLoaded.Stop()
        Timer_PageLoaded.Start()
    End Sub

    Private Sub Timer_PageLoaded_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer_PageLoaded.Tick
        'If the timer ticks then the WebBrowser progress has stopped changing
        'for 4 seconds. That tells us that it must be finnished loading the page.

        'Stop this timer so it doesn`t run these commands over and over.
        Timer_PageLoaded.Stop()

        'Put your code or sub to execute here:
        MessageBox.Show("I am done loading now.")
    End Sub


Was This Post Helpful? 0
  • +
  • -

#14 iamcpc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 15-January 13

Re: VS 2008 Sleep() not working and I can't figure out why

Posted 01 February 2013 - 12:08 PM

It has nothing to do with the webbrowser. It has to do with a public variable in module1. I'm not sure why but having form1 change the value of a module1 variable affects how threads start module1 functions and subs. I went into this in much greater detail on www.vbforums.com
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1