Click here to Skip to main content
15,902,888 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: How to insert all characters in database Pin
Richard MacCutchan3-Jun-13 22:57
mveRichard MacCutchan3-Jun-13 22:57 
AnswerRe: How to insert all characters in database Pin
Simon_Whale3-Jun-13 23:04
Simon_Whale3-Jun-13 23:04 
SuggestionRe: How to insert all characters in database Pin
Richard Deeming4-Jun-13 1:54
mveRichard Deeming4-Jun-13 1:54 
GeneralRe: How to insert all characters in database Pin
Beiniam4-Jun-13 3:02
Beiniam4-Jun-13 3:02 
QuestionUnable to Update LDAP property "department" Pin
David Mujica3-Jun-13 10:12
David Mujica3-Jun-13 10:12 
AnswerRe: Unable to Update LDAP property "department" Pin
Bernhard Hiller3-Jun-13 21:20
Bernhard Hiller3-Jun-13 21:20 
GeneralRe: Unable to Update LDAP property "department" Pin
David Mujica5-Jun-13 0:23
David Mujica5-Jun-13 0:23 
QuestionHow difficult to switch from DoEvents to Threading? Pin
treddie2-Jun-13 11:19
treddie2-Jun-13 11:19 
This question is a new post prompted by my other thread, "The old DoEvents and UserControls". I created a separate thread here, since it is branching off from the old discussion.

In the other thread, it was looking like converting code from supporting DoEvents, to supporting threading might be a big rewrite. But now I am not so sure. I downloaded an example Pause/Resume Thread program from online and that program had a separate class that was threaded. It then communicated to the main form via a delegate.

But I wanted to find out if it was possible to do threading of a procedure that was NOT in a separate class...But one residing in the main form's class, like any other sub. The reason this was important to me, was to cut down on the amount of code rewriting by keeping my existing code in place, and merely adding on additional code to thread one of those main form procedures. My revision to that original threading example seems to say, "yes", to that question. Here is my version of that little demo. It simply runs a counter that counts from 1 to whatever, and when the thread is suspended, the counter halts, until resumed. Since I am new to threading, is there anything wrong with this idea that could end up biting me in the rear?

Here is the code:

VB
  Imports System.Threading
  Imports System

Public Class Form1
  'Form1 controls:
  'Button1:      Starts/Resumes thread execution.
  'Button2:      Suspends thread execution.
  'txtResults:   Textbox for count results.
  'Quit_cmd:     End program.

  'Define a delegate type for the form's DisplayValue method:
  Private Delegate Sub DisplayValueDelegateType(ByVal txt _
                                                As String)

  'Declare a delegate variable to point to the form's
  'DisplayValue method:
  Private m_DisplayValueDelegate As DisplayValueDelegateType

  Private m_Thread As Thread

  'This value is incremented by the thread:
  Public Value As Integer = 0

  'Add the text to the results.  The form provides this 
  'service because the thread cannot access the form's 
  'controls directly:
  Public Sub DisplayValue(ByVal txt As String)
    txtResults.Text = txt
    'txtResults.Select(txtResults.Text.Length - 1, 0)

  End Sub

  Private Sub Form1_Load(ByVal sender As Object, _
                         ByVal e As System.EventArgs) _
                         Handles MyBase.Load
    'Make a new counter object:
    Dim new_counter As New Counter(Me, my_number:=1)
    '...is the same as:
    'Dim my_number As Integer = 1    'The thread number.
    'Dim new_counter As New Counter(Me, my_number)

    'Make the thread to run the object's Run method:
    m_Thread = New Thread(AddressOf new_counter.Runno)

    'Make this a background thread so it automatically
    'ends when the form's thread ends:
    m_Thread.IsBackground = True
    m_Thread.Start()
    Button1.Enabled = False
    Button2.Enabled = True
  End Sub

  Private Sub Button1_Click(ByVal sender As Object, _
                            ByVal e As System.EventArgs) _
                            Handles Button1.Click
    If (m_Thread.ThreadState And ThreadState.Unstarted) _
        <> 0 Then

       'The thread has never been started. Start it:
      m_Thread.Start()
    Else
      ' The thread is paused. Resume it:
      m_Thread.Resume()    'THIS HAS BEEN DEPRECATED!!!
    End If

    Button1.Enabled = False
    Button2.Enabled = True
  End Sub

  Private Sub Button2_Click(ByVal sender As System.Object, _
                            ByVal e As System.EventArgs) _
                            Handles Button2.Click
    'Suspend the thread:
    Debug.WriteLine("Suspending thread")
    m_Thread.Suspend()    'THIS HAS BEEN DEPRECATED!!!

    Button1.Enabled = True
    Button2.Enabled = False
  End Sub

  Public Sub DoSomething(ByVal m_MyForm As Form1, _
                         ByVal m_DisplayValueDelegate _
                         As Object)

    For loopo = 1 To 10000000
      'Wait 3 seconds:
      Thread.Sleep(3000)

      'Lock the form object. This doesn't do anything to the
      'form, it just means no other thread can lock the form 
      'object until we release the lock.  That means a thread
      'can update m_MyForm.Value and then display its value
      'without interference:

      SyncLock m_MyForm
          'Increment the form's Value:
          'm_MyForm.Value += 1
          m_MyForm.Value = m_MyForm.Value + 1

          'Display the value on the form.  The call to 
          'InvokeRequired returns True if this code is not
          'running on the same thread as the object m_MyForm.
          'In this example, we know that is true so the call 
          'isn't necessary, but in other cases it might not
          'be so clear:
          If m_MyForm.InvokeRequired() Then
              'Invoke the delegate:
              m_MyForm.Invoke(m_DisplayValueDelegate, _ 
                              Str(m_MyForm.Value))
          End If
      End SyncLock

    Next loopo

  End Sub

  Private Sub Quit_cmd_Click(ByVal sender As System.Object, _
                             ByVal e As System.EventArgs) _
                             Handles Quit_cmd.Click
    End
  End Sub

End Class





'This class's Run method, Runno(), displays a count in the 
'Output window.
Public Class Counter
    'The form that owns the Value variable:
    Private m_MyForm As Form1

    'Define a delegate type for the form's DisplayValue 
    'method:
    Private Delegate Sub DisplayValueDelegateType( _
                                        ByVal txt As String)

    'Declare a delegate variable to point to the form's 
    'DisplayValue method:
    Private m_DisplayValueDelegate As DisplayValueDelegateType

    'This counter's thread number:
    Private m_Number As Integer

    Public Sub New(ByVal my_form As Form1, _
                   ByVal my_number As Integer)
        m_MyForm = my_form
        m_Number = my_number    'The thread number.

        'Initialize the delegate variable to point to the
        'form's DisplayValue method:
        m_DisplayValueDelegate = AddressOf _ 
                                 m_MyForm.DisplayValue

    End Sub

  'This procedure is directly addressed for threading.  This
  'test is to see whether or not a form1 Class's procedure
  'called from here can respond to a thread pause.  In other 
  'words, does a threaded procedure's pause also apply to any 
  'calls it makes, regardless of what class those calls 
  'reside within?  Can the pause immediately go into effect 
  'if it is applied when the program is presently running 
  'that call?

  'The upshot is, yes it can...Kind of.  The call certainly 
  'was paused, but the Thread.Sleep(3000) line was unaffected 
  'and kept counting regardless of everything else being in a 
  'paused state.  So you have to be careful not to assume that
  'certain functions will necessarily respond to a thread 
  'pause, especially timers, stopwatches, etc., unless the 
  'thread pause/resume state can be used to affect those 
  'objects.
  '
  'IMPORTANT:  The thread pause action was able to occur 
  'immediately while the Thread.Sleep(3000) line was still 
  'suspending execution.  So even though it kept counting for 
  '3sec after the pause was initiated, it still allowed the 
  'pause to go into effect:

  'Also, THE CALLED SUB IS NOT IN THE SAME CLASS AS THE 
  'THREADED PROCEDURE.

  Public Sub Runno()
    Try
      Call Form1.DoSomething(m_MyForm, m_DisplayValueDelegate)
    Catch ex As Exception
      'An unexpected error:
      Debug.WriteLine("Unexpected error in thread " & _
                      m_Number & vbCrLf & ex.Message)
    End Try

  End Sub




End Class

AnswerRe: How difficult to switch from DoEvents to Threading? Pin
Dave Kreskowiak2-Jun-13 19:28
mveDave Kreskowiak2-Jun-13 19:28 
GeneralRe: How difficult to switch from DoEvents to Threading? Pin
treddie2-Jun-13 20:57
treddie2-Jun-13 20:57 
QuestionVB.NET and PHP mysql connection Pin
Amiet_Mhaske2-Jun-13 9:52
Amiet_Mhaske2-Jun-13 9:52 
QuestionThe old DoEvents and UserControls Pin
treddie1-Jun-13 18:41
treddie1-Jun-13 18:41 
AnswerRe: The old DoEvents and UserControls Pin
Dave Kreskowiak2-Jun-13 4:03
mveDave Kreskowiak2-Jun-13 4:03 
GeneralRe: The old DoEvents and UserControls Pin
treddie2-Jun-13 7:21
treddie2-Jun-13 7:21 
GeneralRe: The old DoEvents and UserControls Pin
Dave Kreskowiak2-Jun-13 7:42
mveDave Kreskowiak2-Jun-13 7:42 
GeneralRe: The old DoEvents and UserControls Pin
treddie2-Jun-13 9:26
treddie2-Jun-13 9:26 
GeneralRe: The old DoEvents and UserControls Pin
Dave Kreskowiak2-Jun-13 14:21
mveDave Kreskowiak2-Jun-13 14:21 
GeneralRe: The old DoEvents and UserControls Pin
treddie2-Jun-13 16:33
treddie2-Jun-13 16:33 
GeneralRe: The old DoEvents and UserControls Pin
Dave Kreskowiak2-Jun-13 19:25
mveDave Kreskowiak2-Jun-13 19:25 
GeneralRe: The old DoEvents and UserControls Pin
treddie2-Jun-13 21:02
treddie2-Jun-13 21:02 
Questionlooping through rows and columns of a datarow Pin
hlsc19831-Jun-13 8:21
hlsc19831-Jun-13 8:21 
Questionhow to loop through items(columns) of a row in a dataset? Pin
hlsc198331-May-13 21:49
hlsc198331-May-13 21:49 
QuestionNot Getting cell value for selected row of GridView Pin
ADSCNET30-May-13 7:36
ADSCNET30-May-13 7:36 
QuestionRenci SSH Problem Pin
ToxicF3AR28-May-13 5:50
ToxicF3AR28-May-13 5:50 
AnswerRe: Renci SSH Problem Pin
Dave Kreskowiak28-May-13 5:56
mveDave Kreskowiak28-May-13 5:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.