프로그래밍언어/VB.NET

멀티쓰레드 활용간 오류시 임시처방 CheckForIllegalCrossThreadCalls = False

부산딸랑이 2016. 10. 25. 23:22

스레드로 사용할 함수내에 다음과같이 입력함.

CheckForIllegalCrossThreadCalls = False


예제

Imports System.Threading
Public Class Form1


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

        MessageBox.Show("This is the main thread")

    End Sub
    Private Sub ThreadTask()
        CheckForIllegalCrossThreadCalls = False

        Dim stp As Integer
        Dim newval As Integer
        Dim rnd As New Random()

        Do
            stp = ProgressBar1.Step * rnd.Next(-1, 2)
            newval = ProgressBar1.Value + stp
            If newval > ProgressBar1.Maximum Then
                newval = ProgressBar1.Maximum
            ElseIf newval < ProgressBar1.Minimum Then
                newval = ProgressBar1.Minimum
            End If

            ProgressBar1.Value = newval

            Thread.Sleep(100)
        Loop
    End Sub

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


        Dim trd As Thread = New Thread(AddressOf ThreadTask)
        trd.IsBackground = True
        trd.Start()

    End Sub
End Class