Click here to Skip to main content
15,911,711 members
Home / Discussions / Visual Basic
   

Visual Basic

 
QuestionFile Downloader Help Pin
Bradley Sweeten26-Jun-13 4:04
Bradley Sweeten26-Jun-13 4:04 
QuestionCompare two datagridviews Pin
goku9925-Jun-13 11:03
goku9925-Jun-13 11:03 
QuestionOpen CVS file in text Pin
Ashok Kumar997824-Jun-13 22:09
Ashok Kumar997824-Jun-13 22:09 
AnswerRe: Open CVS file in text Pin
Richard MacCutchan24-Jun-13 22:38
mveRichard MacCutchan24-Jun-13 22:38 
AnswerRe: Open CVS file in text Pin
MaulikDusara26-Jun-13 8:55
MaulikDusara26-Jun-13 8:55 
QuestionClick a main thread button from a background thread Pin
treddie24-Jun-13 20:41
treddie24-Jun-13 20:41 
AnswerRe: Click a main thread button from a background thread Pin
Bernhard Hiller24-Jun-13 22:21
Bernhard Hiller24-Jun-13 22:21 
GeneralRe: Click a main thread button from a background thread Pin
treddie24-Jun-13 23:24
treddie24-Jun-13 23:24 
Bernhard Hiller wrote:
However, if UI elements are accessed, you should check for InvokeRequired.


That's exactly where my problem is, I think. I am doing InvokeRequired, but am getting screwed up somewhere. Now, I also just got an error back at one point saying that delegates cannot handle events. I am assuming that means you can pass arguments to a control, but if you need to fire one of its events, like a button click, a delegate won't work. Not sure about that, though.

At any rate, here is my code so far. I took the guts from a demo and attempted to modify it to do the reverse of what it did originally...Run some really long task as a background thread and let the controls be free for use in the main thread. This experiment is attempting to do the opposite...Run the task in the main thread, and work the form's button via a background task. It doesn't toggle the Pause/Continue state just yet...At this point, I was just trying to get the button to respond to a simple click.

Also, the Run method at the very bottom does the Invoke, but it causes the program launch to hang during form load. I figured I might run into problems there, since I may be trying something impossible with a delegate, or I've just got everything set up wrong.

VB
  Imports System.Threading
  'Imports System


Public Class Form1

  '--------------------------------------------------------------------------------------
  'Background thread setup for the Pause/Continue button:

  'Define a delegate type for the form's Pause_Continue_cmd button:
  Private Delegate Sub Pause_Continue_cmd_ClickDelegateType(_
                ByVal sender As System.Object, _
                ByVal e As System.EventArgs) 

  'Declare a delegate button to point to the form's Pause_Continue_cmd button:
  Private Pause_Continue_cmd_ClickDelegate As _
          Pause_Continue_cmd_ClickDelegateType

  Private BG_Thread As Thread

  'The main thread as "owner" of Form1 (and its controls will provide the actual
  'button clicks for the Pause_Continue_cmd button, because the background thread
  'cannot access the form's controls directly.
  '--------------------------------------------------------------------------------------




  Private Sub Form1_Load(ByVal sender As System.Object, _
                         ByVal e As System.EventArgs) _
                         Handles MyBase.Load

    FormIsLoadingFlag = 1
        Threading.Thread.CurrentThread.Name = "UI Thread"

        ProgramLaunchFlag = 0
        'Any other initializtion code for the main thread and form goes here.
    FormIsLoadingFlag = 0

    '--------------------------------------------------------------------------------------
    'Background thread setup for the Pause/Continue button.

    'Make a new Pause_Continue_cmd button object:
    Dim New_DelegatePauseContinueButton As New _
            Delegate_Pause_Continue_Button(Me, BGThreadNumber:=1)

    'Make the thread to run the object's Run method:
    BG_Thread = New Thread(AddressOf New_DelegatePauseContinueButton.Run)

    'Make this a background thread so it automatically
    'ends when the form's thread ends:
    BG_Thread.IsBackground = True
    BG_Thread.Start()

  End Sub

    Private Sub StartLoop_cmd_Click(ByVal sender As System.Object, _
                                    ByVal e As System.EventArgs) _
                                    Handles StartLoop_cmd.Click
        Call StartLoop()
    End Sub

    Private Sub StartLoop()
        Dim loopo As Long
        Dim loopo2 As Long

        For loopo = 1 To 100000000
            For loopo2 = 1 To 100000000    'Time Delay for each pass.
                loopo = loopo
                'Do nothing.
            Next loopo2

            Call Pause_Continue()

            Counto = Counto + 1

            Me.CounterVal_echo.Text = Str(Counto)
            Me.CounterVal_echo.Refresh()
        Next loopo

    End Sub

    Public Sub Pause_Continue()
        'Pause the above loops by getting caught in this endless loop until
        'the value of CountStopFlag = 0:

EndlessLoop:
        If CountStopFlag = 0 Then Exit Sub
        GoTo EndlessLoop

    End Sub

  Public Sub Pause_Continue_cmd_Click(ByVal sender As System.Object, _
                                      ByVal e As System.EventArgs) _
                                      Handles Pause_Continue_cmd.Click
      CountStopFlag = 1
  End Sub


End Class





Public Class Delegate_Pause_Continue_Button
    'The form that owns the Pause_Continue_cmd button:
    Private OwnerForm As Form1

    'Define a delegate type for the form's DisplayValue method:
    Private Delegate Sub Pause_Continue_cmd_ClickDelegateType( _
              ByVal sender As System.Object, _
              ByVal e As System.EventArgs) 'Handles Pause_Continue_cmd.Click

    'Declare a delegate variable to point to the form's DisplayValue method:
    Private Pause_Continue_cmd_ClickDelegate As Pause_Continue_cmd_ClickDelegateType

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



    Public Sub New(ByVal my_form As Form1, _
                   ByVal BGThreadNumber As Integer)
        OwnerForm = my_form
        PrivateBGThreadNumber = BGThreadNumber    'The thread number.

        'Initialize the delegate variable to point to the form's DisplayValue method:
        Pause_Continue_cmd_ClickDelegate = AddressOf OwnerForm.Pause_Continue_cmd_Click

    End Sub

    Public Sub Run()
        Try
            Do
                'Wait 1 second:
                'Thread.Sleep(1000)

                '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 OwnerForm
                    If OwnerForm.InvokeRequired() Then

                        'Make an array containing the parameters to pass to the method:
'NOT SURE THIS ARRAY IS THE CORRECT WAY TO DO THIS SINCE THAT WOULD MEAN THAT THE BUTTON CLICK EVENT
'ARGUMENTS WOULD NEED TO BE PARSED OUT OF IT, WHICH EVENTS CANNOT DO:
                        Dim args() As Object = {Nothing, Nothing}

                        'Invoke the delegate:
                        'OwnerForm.Invoke(Pause_Continue_cmd_ClickDelegate, args)
'NOT SURE ABOUT THE FOLLOWING LINE.  THE CLICK EVENT HAS PASSED ARGUMENTS, SO THIS SHOULD TOO, CORRECT?
'AT ANY RATE, THIS LINE LOCKS UP THE FORM_LOAD PROCEDURE WHEN THE PROGRAM IS LAUNCHED:
                        OwnerForm.Invoke(Pause_Continue_cmd_ClickDelegate)
                    End If
                End SyncLock
            Loop
        Catch ex As Exception
            Debug.WriteLine("Unexpected error in thread " & PrivateBGThreadNumber & vbCrLf & ex.Message)
        End Try
    End Sub


End Class

AnswerRe: Click a main thread button from a background thread Pin
GuyThiebaut24-Jun-13 22:43
professionalGuyThiebaut24-Jun-13 22:43 
GeneralRe: Click a main thread button from a background thread Pin
treddie24-Jun-13 23:47
treddie24-Jun-13 23:47 
GeneralRe: Click a main thread button from a background thread Pin
treddie25-Jun-13 19:40
treddie25-Jun-13 19:40 
QuestionVB6 DataGrid Bookmark Error Pin
Member 981947024-Jun-13 10:27
Member 981947024-Jun-13 10:27 
AnswerRe: VB6 DataGrid Bookmark Error Pin
Eddy Vluggen25-Jun-13 9:59
professionalEddy Vluggen25-Jun-13 9:59 
AnswerHow to detect UTF-8-based encoded strings Pin
diegosendra24-Jun-13 9:33
diegosendra24-Jun-13 9:33 
SuggestionRe: How to detect UTF-8-based encoded strings Pin
Richard Deeming24-Jun-13 9:51
mveRichard Deeming24-Jun-13 9:51 
GeneralRe: How to detect UTF-8-based encoded strings Pin
diegosendra24-Jun-13 10:30
diegosendra24-Jun-13 10:30 
GeneralRe: How to detect UTF-8-based encoded strings Pin
Roy Heil24-Jun-13 11:08
professionalRoy Heil24-Jun-13 11:08 
GeneralRe: How to detect UTF-8-based encoded strings Pin
diegosendra24-Jun-13 11:15
diegosendra24-Jun-13 11:15 
GeneralRe: How to detect UTF-8-based encoded strings Pin
Richard MacCutchan24-Jun-13 20:33
mveRichard MacCutchan24-Jun-13 20:33 
QuestionCombine Printjobs Pin
tolarion24-Jun-13 2:23
tolarion24-Jun-13 2:23 
AnswerRe: Combine Printjobs Pin
Dave Kreskowiak24-Jun-13 4:05
mveDave Kreskowiak24-Jun-13 4:05 
GeneralRe: Combine Printjobs Pin
tolarion24-Jun-13 4:46
tolarion24-Jun-13 4:46 
GeneralRe: Combine Printjobs Pin
Dave Kreskowiak24-Jun-13 7:16
mveDave Kreskowiak24-Jun-13 7:16 
GeneralRe: Combine Printjobs Pin
tolarion24-Jun-13 10:02
tolarion24-Jun-13 10:02 
GeneralRe: Combine Printjobs Pin
Dave Kreskowiak24-Jun-13 11:58
mveDave Kreskowiak24-Jun-13 11:58 

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.