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
Code:
01 Public Class Form1
02
03 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
04 Dim testthread As New System.Threading.Thread(AddressOf Module1.loop3)
05 Dim testthread1 As New System.Threading.Thread(AddressOf Module1.loop3)
06 Dim testthread2 As New System.Threading.Thread(AddressOf Module1.loop3)
07 Dim testthread3 As New System.Threading.Thread(AddressOf Module1.loop3)
08 Dim testthread4 As New System.Threading.Thread(AddressOf Module1.loop3)
09 testthread.Start(1)
10 System.Threading.Thread.Sleep(2000)
11
12 testthread1.Start(2)
13 System.Threading.Thread.Sleep(2000)
14 testthread2.Start(3)
15 System.Threading.Thread.Sleep(2000)
16 testthread3.Start(4)
17 System.Threading.Thread.Sleep(2000)
18 testthread4.Start(5)
19 System.Threading.Thread.Sleep(2000)
20 Call Module1.loop3(6)
21 End Sub
22 End Class
01 Public Module Module1
02 Dim xl = CreateObject("excel.application")
03 Delegate Sub testo()
04
05
06
07
08 Public Sub loop3(ByVal column As Integer)
09
13 If xl.visible <> True Then
14 xl.visible = True
15 End If
19 Try
20 If Not xl.activeworkbook.name = "Spenddown IN Mdcd.xlsm" Then
21 xl.workbooks.open("C:\Documents and Settings\BMcGuir1\Desktop\Test\spenddown in mdcd.xlsm")
22 GoTo done
23 End If
24
25 Catch
26 xl.workbooks.open("C:\Documents and Settings\BMcGuir1\Desktop\Test\spenddown in mdcd.xlsm")
27 End Try
28
29 done:
36 Dim y As Long
37 For y = 1 To 100
38 xl.activeworkbook.sheets("sheet1").cells(y, column).value = y
39 System.Threading.Thread.Sleep(100)
40 Next
41 'MsgBox("done")
42 End Sub
43
44 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. I've attached the application to this post. I've also researched to try to find out why sleep() would not work and I have not found anything.