2011-03-02 25 views

回答

2

您可以使用BackgroundWorker來做到這一點。

Private WithEvents dataBackgroundWorker As New BackgroundWorker

那麼你就需要調用

dataBackgroundWorker.RunWorkerAsync()

其中在處理器提高了DoWork事件

所以,你會打電話給你的功能,並通過e.Result

歸還

e.Result = yourFunction()

然後在RunWorkerCompleted事件,你會分配e.Result到相應的變量。

+0

謝謝。我無法將數據表作爲e.result返回並在RunWorkerCompleted事件中獲取數據表。你能幫忙嗎?它拋出一個異常可空對象必須有一個值。我可以設置字符串作爲結果 – Akshara 2011-03-03 05:18:18

2

你最好打賭的可能是使用後臺工作者。如果你感覺冒險,但Async CTP看起來非常棒。

http://msdn.microsoft.com/en-us/vstudio/gg316360

你可以在這裏找到樣本:http://www.wischik.com/lu/AsyncSilverlight/AsyncSamples.html

Public Async Function AsyncResponsiveCPURun() As Task 
    Console.WriteLine("Processing data... Drag the window around or scroll the tree!") 
    Console.WriteLine() 
    Dim data As Integer() = Await ProcessDataAsync(GetData(), 16, 16) 
    Console.WriteLine() 
    Console.WriteLine("Processing complete.") 
End Function 


Public Function ProcessDataAsync(ByVal data As Byte(), ByVal width As Integer, ByVal height As Integer) As Task(Of Integer()) 
    Return TaskEx.Run(
     Function() 
      Dim result(width * height) As Integer 
      For y As Integer = 0 To height - 1 
       For x As Integer = 0 To width - 1 
        Thread.Sleep(10) ' simulate processing cell [x,y] 
       Next 
       Console.WriteLine("Processed row {0}", y) 
      Next 
      Return result 
     End Function) 
End Function 
+0

它看起來很棒。我相信這些條款和條件表明你不打算將其部署在生產代碼中......雖然我不能建議你違反該法律協議,但我認爲這是唯一阻礙工作的方式,沒有任何技術(除了不支持) – MarkJ 2011-03-02 17:26:07

相關問題