VB中的多线程

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VB中的多线程相关的知识,希望对你有一定的参考价值。

请问用VB中的多线程是怎么样一个情况,能否给个示例?

定义:
Dim g_ReceiveThread As System.Threading.Thread
创建:
g_ReceiveThread = New System.Threading.Thread(AddressOf ReceiveProc)
开始:
g_ReceiveThread.Start()
结束:
Dim waitStart As Long = My.Computer.Clock.TickCount + 1000 ‘超时
While (g_ReceiveThread.ThreadState <> System.Threading.ThreadState.Stopped)
If My.Computer.Clock.TickCount >= waitStart Then
g_ReceiveThread.Abort()
End If
Application.DoEvents()
End While
参考技术A VB中没有多线程
要么你用VB.NET追问

那这张图红框里的东西是什么意思?

参考技术B 可以用CreateThread创建一个线程,不过VB太不稳定了,运行一会
就挂了,或者根本运行不起来,网上有稳定的代码,你找一下。

汇编语言中的多线程

【中文标题】汇编语言中的多线程【英文标题】:multiple thread in assembly language 【发布时间】:2015-08-07 17:34:41 【问题描述】:
override func viewDidLoad() 
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    //***********************************************************************************************************
    // requesting access and autherzation and availability
    let healthStore = HKHealthStore()
    if HKHealthStore.isHealthDataAvailable() 
        calciumTracker.setProgress(0.1, animated: true)
        let ShareTypes : Set = [HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!]
        let ReadTypes : Set = [HKCharacteristicType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBiologicalSex)!]
        healthStore.requestAuthorizationToShareTypes(ShareTypes, readTypes: ReadTypes , completion:
            (success,error) in
            if !success 
                if let x = error 
                    print(x.description)
                

            
        )
    
    else 
        print("Health data are not available on this device")
    
    //***********************************************************************************************************
    let endDate = NSDate()
    let startDate = NSDate()
    var v : HKQuantitySample?
    let stepsCount:HKQuantityType = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!
    let predicate:NSPredicate = HKQuery.predicateForSamplesWithStartDate(startDate, endDate: endDate, options: .None)
    let limit = 1
    let query = HKSampleQuery(sampleType: stepsCount, predicate: predicate, limit: limit, sortDescriptors: nil, resultsHandler: 
        (query, results, error) in
            if results == nil 
                print(error)
            
            v = results!.first as! HKQuantitySample?
        )
    healthStore.executeQuery(query)
    let l = Float(v!.quantity.doubleValueForUnit(HKUnit(fromString: "count")))
    stepsTracker.setProgress(l/300, animated: true)

当我运行应用程序时,它卡在启动屏幕上,Xcode 告诉我有线程列表,我不明白它们中的任何一个,因为我认为它们是汇编语言

【问题讨论】:

您需要阅读异步方法在 iOS 中的工作原理。您应该将依赖于任何异步方法完成的代码放在完成处理程序中。 【参考方案1】:

崩溃是由于从 nil 对象中解包值引起的。该应用程序尝试访问 HKQuantitySample,即使它为 nil,它也会在以下行中崩溃:

let l = Float(v!.quantity.doubleValueForUnit(HKUnit(fromString: "count")))

你应该重新组织逻辑并执行requestAuthorizationToShareTypes函数回调中的后半部分代码。

例如:

override func viewDidLoad() 
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    //***********************************************************************************************************
    // requesting access and autherzation and availability
    let healthStore = HKHealthStore()
    if HKHealthStore.isHealthDataAvailable() 
        calciumTracker.setProgress(0.1, animated: true)
        let ShareTypes : Set = [HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!]
        let ReadTypes : Set = [HKCharacteristicType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierBiologicalSex)!]

        healthStore.requestAuthorizationToShareTypes(ShareTypes, readTypes: ReadTypes , completion:
            (success,error) in
            if !success 
                if let x = error 
                    print(x.description)
                
             else 
                //***********************************************************************************************************
                let endDate = NSDate()
                let startDate = NSDate()
                var v : HKQuantitySample?
                let stepsCount:HKQuantityType = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!
                let predicate:NSPredicate = HKQuery.predicateForSamplesWithStartDate(startDate, endDate: endDate, options: .None)
                let limit = 1
                let query = HKSampleQuery(sampleType: stepsCount, predicate: predicate, limit: limit, sortDescriptors: nil, resultsHandler: 
                    (query, results, error) in
                    if results == nil 
                        print(error)
                    
                    v = results!.first as! HKQuantitySample?
                )
                healthStore.executeQuery(query)
                let l = Float(v!.quantity.doubleValueForUnit(HKUnit(fromString: "count")))
                self.stepsTracker.setProgress(l/300, animated: true)
            
        )
    
    else 
        print("Health data are not available on this device")
    

理想情况下,在你请求授权之前,你应该先检查授权状态,即调用authorizationStatusForType()。

【讨论】:

以上是关于VB中的多线程的主要内容,如果未能解决你的问题,请参考以下文章

vb 怎么实现多线程

vb中的timer\多线程有关问题

python中的多线程

详解python中的多线程

java中的多线程

Python中的多线程并行运行