VB.Net中的多线程 [英] Multithreading in VB.Net

查看:115
本文介绍了VB.Net中的多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是这个网站的新手,如果我违反了这里的任何规则,请原谅我...
我正在编写一个测试应用程序来帮助我学习Threading类
每当我调用(出于线程安全性)该进程实际上在同一线程上运行时,我一直在网上到处寻找答案但没有骰子...
我的源代码如下...

Hello im new to this site so forgive me if I have broken any rules here...
I am writing a test application to aid me in the Threading class
how ever when i invoke (for thread safety) the process actualy runs on the same thread, I have looked everywhere on line for an answer but no dice...
My source code is as follows...

Imports System.Threading
Public Class Form

    Private pass As Integer = 0
    Private Delegate Sub CallBack(ByVal No As Integer)

    Public Sub New()
        InitializeComponent()
        Me.Text = "MultiThreading - Main Thread ID: " & Thread.CurrentThread.ManagedThreadId
    End Sub

    Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button.Click
        Dim t As Thread = New Thread(AddressOf AddItem, 0)
        t.GetHashCode()
        t.SetApartmentState(ApartmentState.MTA)
        t.IsBackground = True
        t.Start(pass)
        pass = pass + 1
    End Sub

    Private Sub AddItem(ByVal ItemNo As Integer)
        If ListBox.InvokeRequired = True Then
            ListBox.BeginInvoke(New CallBack(AddressOf AddItem), ItemNo)
        Else
            ListBox.Items.Add("Item Number " & ItemNo & " - Added by Thread ID: " & Thread.CurrentThread.ManagedThreadId)
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim t As Thread = New Thread(AddressOf MSGTest)
        t.Start("Test Message")
    End Sub

    Private Sub MSGTest(ByVal MSG As String)
        MsgBox(MSG & "...   MsgBox Generated by Thread ID: " & Thread.CurrentThread.ManagedThreadId, MsgBoxStyle.Information, "Thread Test")
    End Sub
End Class



测试功能有效,但不使用任何调用,
任何帮助都是很好的,因为我花了一些时间解决这个问题.

感谢高级... Blingking01



The test function works but that does not use any invoke,
Any help would be great as I have spent some time working this one out.

Thanks in advanced... Blingking01

推荐答案

您以错误的方式持有它. ;)

线程处于流程中,而不是相反,因此很难确定您要问的是什么(您是否混淆了问题,概念或助手?).

这是有关VB.NET中线程化的CP入门项目:
制作多线程VB.NET应用程序的简介 [
干杯.
You''re holding it the wrong way. ;)

Threads are in processes, not the other way around, so it''s hard to be sure what you''re asking (have you confused the question, the concept or the helper?).

Here''s a CP starter project on threading in VB.NET:
Introduction to making multithreaded VB.NET Apps[^]

So, if you''ve read through the above and you''re still thinking it''s not working, perhaps clarify your question a little as to what you expect, exactly what you get (including any errors) and we''ll see what we can do.

Cheers.


这篇关于VB.Net中的多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆