Gopher2011
Well-known member
Hi,
I have a frm_Main, which starts a thread. The thread then goes and calls a module, which has a thread.sleep() in it. This causes the mouse to freeze up while it sleeps, despite being called by the child thread. I thought this would have be on the thread time-slice? However it is causing my form to freeze for the sleep duration.
And the Module Code
The problem is the form lags when its run, it becomes slow while the sleep is going on. Any Advice?
I have a frm_Main, which starts a thread. The thread then goes and calls a module, which has a thread.sleep() in it. This causes the mouse to freeze up while it sleeps, despite being called by the child thread. I thought this would have be on the thread time-slice? However it is causing my form to freeze for the sleep duration.
VB.NET:
Imports System.Threading.Thread
Imports System.ComponentModel
[COLOR="blue"]Public Class frm_Main[/COLOR]
Public WithEvents T1_Thread As BackgroundWorker
[COLOR="blue"]Public Sub New[/COLOR]()
InitializeComponent()
T1_Thread = New BackgroundWorker
T1_Thread.WorkerReportsProgress = True
T1_Thread.WorkerSupportsCancellation = True
[COLOR="blue"]End Sub[/COLOR]
[COLOR="blue"]Private Sub SS_C_Start_smi_Click[/COLOR](ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SS_C_Start_smi.Click
T1_Thread.RunWorkerAsync()
[COLOR="blue"]End Sub[/COLOR]
[COLOR="blue"]Private Sub T1_Thread_DoWork[/COLOR](ByVal sender As System.Object, _
ByVal e As System.ComponentModel.DoWorkEventArgs) _
Handles T1_Thread.DoWork
T1_Thread.ReportProgress(1)
Thread.Sleep(2000) 'This is ok no lag
[COLOR="blue"]End Sub[/COLOR]
[COLOR="blue"]Private Sub NewThread1_ProgressChanged[/COLOR](ByVal sender As Object, _
ByVal e As System.ComponentModel.ProgressChangedEventArgs) _
Handles T1_Thread.ProgressChanged
RTU_Write(2500,"Test Message") 'This time causes lots of screen lag
[COLOR="blue"]End Sub[/COLOR]
[COLOR="blue"]Private Sub NewThread1_RunWorkerCompleted[/COLOR](ByVal sender As System.Object, _
ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) _
Handles T1_Thread.RunWorkerCompleted
[COLOR="blue"]End Sub[/COLOR]
[COLOR="blue"]End Class[/COLOR]
And the Module Code
VB.NET:
[COLOR="blue"]Module RTU_Code[/COLOR]
[COLOR="blue"]Public Function RTU_Write[/COLOR](ByRef SleepTime As Integer, ByVal Msg As String) As Integer
frm_Main.SerialPort1.Write(Msg)
System.Threading.Thread.Sleep(SleepTime)
[COLOR="blue"]End Function[/COLOR]
[COLOR="blue"]End Module[/COLOR]
The problem is the form lags when its run, it becomes slow while the sleep is going on. Any Advice?