2023年11月20日 星期一

VB.NET Multithreading

 參考來源:VB.NET Multithreading

*** Create a new Thread ***


Imports System.Threading 'Imports the System.Threading namespace.   

Module create_Thread  

    Sub Main(ByVal args As String())  

        ' create a new thread   

        Dim th As Thread = New Thread(New ThreadStart(AddressOf PrintInfo))  

        ' start the newly created thread  

        th.Start()  

        Console.WriteLine(" It is a Main Thread")  

    End Sub  

    Private Sub PrintInfo()  

        For j As Integer = 1 To 5  

            Console.WriteLine(" value of j is {0}", j)  

        Next j  

        Console.WriteLine(" It is a child thread")  

        Console.WriteLine(" Press any key to exit...")  

        Console.ReadKey()  

    End Sub  

End Module  



*** VB.NET Thread Life Cycle ***

Imports System.Threading  

Module Thread_cycle  

    Sub Main(ByVal args As String())  

        Dim s As Stopwatch = New Stopwatch()  

        s.Start()  

        Dim t As Thread = New Thread(New ThreadStart(AddressOf PrintInfo))  

        t.Start()  

        ' Halt another thread execution until the thread execution completed  

        t.Join()  

        s.[Stop]()  

        Dim t1 As TimeSpan = s.Elapsed  

        Dim ft As String = String.Format("{0}: {1} : {2}", t1.Hours, t1.Minutes, t1.Seconds)  

        Console.WriteLine(" Total Elapsed Time : {0}", ft)  

        Console.WriteLine("Completion of Thread Execution ")  

        Console.WriteLine("Press any key to exit...")  

        Console.ReadKey()  

    End Sub  

    Private Sub PrintInfo()  

        For j As Integer = 1 To 6  

            Console.WriteLine(" Halt Thread for {0} Second", 5)  

            ' It pause thread for 5 Seconds  

            Thread.Sleep(5000)  

            Console.WriteLine(" Value of i {0}", j)  

        Next  

    End Sub  

End Module  




*** Multithreading ***

 Imports System.Threading  

Module Multi_thread  

    Sub Main(ByVal arg As String())  

        Dim th As Thread = New Thread(New ThreadStart(AddressOf PrintInfo))  

        Dim th2 As Thread = New Thread(New ThreadStart(AddressOf PrintInfo2))  

        th.Start()  

        th2.Start()  

        Console.ReadKey()  

    End Sub  

    Private Sub PrintInfo()  

        For j As Integer = 1 To 5  

            Console.WriteLine(" value of j is {0}", j)  

            Thread.Sleep(1000)  

        Next  

        Console.WriteLine(" Completion of First Thread")  

    End Sub  

    Private Sub PrintInfo2()  

        For k As Integer = 1 To 5  

            Console.WriteLine(" value of k is {0}", k)  

        Next  

        Console.WriteLine(" Completion of First Thread")  

    End Sub  

End Module  


VB.NET Thread 多緒回傳

 ************Thread沒參數************


Public Class Class1

    Event EvnetRecall As EventHandler '用事件回傳結果

    Sub Add()

        Dim Show As Integer = 0

        Dim Add As Integer = 1

        For i = 0 To 10000 Step Add

            Show += i

        Next i

        Debug.WriteLine("Add執行緒:" & System.Threading.Thread.CurrentThread.ManagedThreadId)

        RaiseEvent EvnetRecall(Show, EventArgs.Empty)

    End Sub

End Class



Public Class Form1

    Dim A As New Class1


#Region "多緒"

    Public Sub TH_Add()

        Dim Thread1 As New System.Threading.Thread(New System.Threading.ParameterizedThreadStart(AddressOf A.Add)) '設定Class為多緒

        Thread1.Name = "TH_Add"

        Thread1.Start()

    End Sub


    Public Delegate Sub TH_Add_Show(ByVal sander As Integer) '宣告委派TH_Add_Show


    Sub TH_AIGroupData_EventHandler(ByVal sender As Integer, ByVal e As System.EventArgs) '委派事件接收

        Me.BeginInvoke(New TH_Add_Show(AddressOf TH_Add_Show_Do), sender)

    End Sub

    Public Sub TH_Add_Show_Do(ByVal sender As Integer) '接收到事件後要執行的動作

        TextBox1.Text = sender

    End Sub

#End Region


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

        AddHandler A.EvnetRecall, AddressOf TH_AIGroupData_EventHandler '共用事件

        TH_Add()

    End Sub

End Class









************Thread有參數************



Public Class Class1

    Event EvnetRecall As EventHandler '用事件回傳結果


    Public Structure StarAndStep

        Public Start As Integer

        Public StepInt As Integer

    End Structure


    Sub Add(ByVal Obj As StarAndStep)

        Dim Show As Integer = 0

        Dim Add As Integer = 1

        For i = 0 To 10000 Step Obj.StepInt

            Obj.Start += i

        Next i

        Debug.WriteLine("Add執行緒:" & System.Threading.Thread.CurrentThread.ManagedThreadId)

        RaiseEvent EvnetRecall(Obj.Start, EventArgs.Empty)

    End Sub

End Class



Public Class Form1


    Dim A As New Class1

#Region "多緒"

    Public Sub TH_Add(ByVal Obj As Object)

        Dim Thread1 As New System.Threading.Thread(New System.Threading.ParameterizedThreadStart(AddressOf A.Add)) '設定副程式為多緒

        Thread1.Name = "TH_Add"

        Thread1.Start(Obj)

    End Sub


    Public Delegate Sub TH_Add_Show(ByVal sander As Object) '宣告委派TH_Add_Show


    Sub TH_AIGroupData_EventHandler(ByVal sender As Object, ByVal e As System.EventArgs) '委派事件接收

        Me.BeginInvoke(New TH_Add_Show(AddressOf TH_Add_Show_Do), sender)

    End Sub

    Public Sub TH_Add_Show_Do(ByVal sender As Object) '接收到事件後要執行的動作

        TextBox1.Text = sender

    End Sub

#End Region


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

        AddHandler A.EvnetRecall, AddressOf TH_AIGroupData_EventHandler '共用事件

        Dim StarStep As New Class1.StarAndStep

        StarStep.Start = 0

        StarStep.StepInt = 2

        TH_Add(StarStep)

    End Sub

End Class


2023年11月9日 星期四

2023年11月6日 星期一

資源集區'internal' 中的系統記憶體不足,無法執行此查詢

 sp_configure 'show advanced options', 1;

GO

RECONFIGURE;

GO

sp_configure 'max server memory', 2147483647;

GO

RECONFIGURE;

GO

---------------------


EXEC sp_configure 'show advanced options', '1' RECONFIGURE WITH OVERRIDE;

EXEC sp_configure 'max server memory', 2147483647 RECONFIGURE WITH OVERRIDE;

EXEC sp_configure 'show advanced options', '0' RECONFIGURE WITH OVERRIDE;


---------------------


 DBCC FREESYSTEMCACHE ('ALL'); --釋放所以在快取內,未被使用的項目

 DBCC FREEPROCCACHE; --從計畫快取移除所有元素

 DBCC DROPCLEANBUFFERS; --清除緩衝區裡面的所有暫存項目

 DBCC FREESESSIONCACHE; --清除所有查詢所使用的分散式連線快取