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
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.
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.