2016-08-26 10 views
0

私はメインスレッドでNumericUpDownのコントロールを持っています。BackgroundWorkerと共有クラス

NumericUpDownの値を格納する公開class_Aのインスタンスを作成します。

別スレッドを実行するBackgroundWorkerを作成します。

BackgroundWorkerスレッドでは、class_Aのインスタンスから引数を呼び出すclass_Bのインスタンスを作成します。

class_Aのインスタンスが作成された理由はわかりません。その結果はNothingです。ここで

は、コードは次のとおりです。

Imports System.ComponentModel 
Public Class Form1 
    Dim WithEvents bgw As New BackgroundWorker 
    Dim WithEvents bgw2 As New BackgroundWorker 
    Dim lSide As Label 
    Public nudSide As NumericUpDown 
    Dim bCalculate As Button 
    Dim bCalculate2 As Button 
    Dim tbLog As TextBox 
    Dim calc As calc 
    Public calc2 As calc2 
    Public Delegate Function d_getSide() As Double 
    Public getSide As New d_getSide(AddressOf rungetSide) 
    Public Side As c_Side 
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     Me.Size = New Size(400, 160) 
     bgw.WorkerSupportsCancellation = True 
     bgw2.WorkerSupportsCancellation = True 

     lSide = New Label 
     With lSide 
      .Text = "Side" 
      .Size = New Size(40, 20) 
      .Location = New Point(10, 10) 

     End With 
     Me.Controls.Add(lSide) 
     nudSide = New NumericUpDown 
     With nudSide 
      .Size = New Size(40, 20) 
      .Location = New Point(lSide.Location.X + lSide.Size.Width, lSide.Location.Y) 
      .DecimalPlaces = 0 
      .Minimum = 1 
      .Maximum = 100 
      .Increment = 1 
      .Value = 1 
     End With 
     Me.Controls.Add(nudSide) 
     bCalculate = New Button 
     With bCalculate 
      .Text = "Calculate" 
      .Size = New Size(60, 20) 
      .Location = New Point(nudSide.Location.X + nudSide.Size.Width + 40, nudSide.Location.Y) 
      AddHandler .Click, AddressOf bCalculate_Click 
     End With 
     Me.Controls.Add(bCalculate) 
     bCalculate2 = New Button 
     With bCalculate2 
      .Text = "Calculate 2" 
      .Size = New Size(60, 20) 
      .Location = New Point(bCalculate.Location.X + bCalculate.Size.Width + 10, bCalculate.Location.Y) 
      AddHandler .Click, AddressOf bCalculate2_Click 
     End With 
     Me.Controls.Add(bCalculate2) 

     tbLog = New TextBox 
     With tbLog 
      .Size = New Size(250, 60) 
      .Location = New Point(lSide.Location.X, lSide.Location.Y + 40) 
      .Multiline = True 
      .ScrollBars = ScrollBars.Vertical 

     End With 
     Me.Controls.Add(tbLog) 

    End Sub 
    Private Sub bCalculate_Click() 
     bgw.RunWorkerAsync(nudSide.Value) 
    End Sub 
    Private Sub bgw_Dowork(sender As Object, e As DoWorkEventArgs) Handles bgw.DoWork 
     'example 1) 
     'passing argument throught backGroundWorker 
     calc = New calc(e.Argument) 
    End Sub 
    Private Sub bgw_Runworkercompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles bgw.RunWorkerCompleted 
     getResult() 
    End Sub 
    Private Sub bCalculate2_Click() 
     'here i create an instance of the Side class (expose the side property) 
     Side = New c_Side 
     bgw2.RunWorkerAsync() 
    End Sub 
    Private Sub bgw2_Dowork(sender As Object, e As DoWorkEventArgs) Handles bgw2.DoWork 
     'example 2) 
     ' in the backgroundworker thread i create an instance of the class calc2 
     calc2 = New calc2() 
    End Sub 
    Private Sub bgw2_Runworkercompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles bgw2.RunWorkerCompleted 
     getResult2() 
    End Sub 
    Private Sub write(ByVal message As String) 
     With tbLog 
      .SelectionStart = .Text.Length 
      .SelectedText = vbCrLf & message 
     End With 
    End Sub 
    Private Sub getResult() 
     tbLog.Clear() 
     write("area = " & calc.area & " cm^2") 
     write("volume = " & calc.volume & " cm^3") 

    End Sub 
    Private Sub getResult2() 
     tbLog.Clear() 
     write("area = " & calc2.area & " cm^2") 
     write("volume = " & calc2.volume & " cm^3") 

    End Sub 
    Public Function rungetSide() As Double 
     If Me.InvokeRequired Then 
      Me.Invoke(getSide) 
     Else 
      Return Side.Side 
     End If 
     Return Side.Side 
    End Function 
End Class 
Class calc 
    Sub New(ByVal Side As Double) 
     _area = Side^2 
     _volume = Side^3 
    End Sub 
    Private _area As Double 
    Public Property area As Double 
     Get 
      Return Math.Round(_area, 2) 
     End Get 
     Set(value As Double) 
      _area = Math.Round(value, 2) 
     End Set 
    End Property 
    Private _volume As Double 
    Public Property volume As Double 
     Get 
      Return Math.Round(_volume, 2) 
     End Get 
     Set(value As Double) 
      _volume = Math.Round(value, 2) 
     End Set 
    End Property 
End Class 
Public Class calc2 
    Sub New() 
     'the constructor, recall the value from the instance (public) of the class 'Side' just built in the main thread 
     'but i don't understand why the instance it's nothing 
     _area = Form1.Side.Side^2 
     _volume = Form1.Side.Side^3 
    End Sub 
    Private _area As Double 
    Public Property area As Double 
     Get 
      Return Math.Round(_area, 2) 
     End Get 
     Set(value As Double) 
      _area = Math.Round(value, 2) 
     End Set 
    End Property 
    Private _volume As Double 
    Public Property volume As Double 
     Get 
      Return Math.Round(_volume, 2) 
     End Get 
     Set(value As Double) 
      _volume = Math.Round(value, 2) 
     End Set 
    End Property 
End Class 
Public Class c_Side 
    Sub New() 
     _Side = Form1.nudSide.Value 
     '_Side = Form1.rungetSide 
    End Sub 
    Private _Side As Double 
    Public Property Side As Double 
     Get 
      Return Math.Round(_Side, 2) 
     End Get 
     Set(value As Double) 
      _Side = Math.Round(value, 2) 
     End Set 
    End Property 
End Class 

私が探している何をメインスレッドでclass_Aのインスタンスを作成し、NumericUpDown値を格納し、別のスレッド(BackgroundWorker)でインスタンスを作成することですのインスタンスに格納される直前にNumericUpDownコントロールの値を取得します。

+0

@SirRufo:1つのタグを削除するなどの簡単な編集はしないでください。特に、比較的新しい投稿では、他の誰かが編集作業をしている可能性があります。投稿を編集する必要があると思われる場合は、編集が必要な_everything_を編集してください。それ以外の場合は、進行中の編集を中断し、強制的に編集をやり直します。 –

+0

申し訳ありません.. ??私のメッセージは決して編集しません。 – Marcello

+1

'Form1.nudSide'。 Sigh、VB.NETはVB.NETプログラマがスレッドコードを正しく書くことができないように設計されています。その式は、ワーカースレッドから呼び出されたときにForm1クラスの* new *インスタンスを作成します。ワーカースレッドからUIコンポーネントにアクセスしないでください。 bCalculate_Click()でどのように正しく行ったかに注意してください。 –

答えて

-1

解決策を見つけました。 ちょうど公開、共有として

Public Shared Side As c_Side 

を変数を宣言だから、すべてのスレッドから、すべてのアプリケーションから見える、などです。 スレッドを開始するとき(または必要なときに)、backGroundWorkerスレッドから '読み取り'できるパブリッククラスのパブリック共有インスタンスのUIコントロールのすべての値をバックアップします。

関連する問題