Nasenbaer
Nasenbaer

Reputation: 4900

How to get the Thread to Invoke in DotNet

If I have minimum 2 classes. One class creates the Bitmap and the other draws the bitmap on UI form. I want to ask you if there is any variable I can transport from UIClass to the GeneratorClass except the whole Form or any control. I prefer to transport the "Thread" as property from UIClass to GeneratorClass and in GeneratorClass I can create the image there by invoking in UIThread.

I know:

Control.Invoke(Delegate, Parameters)

Or in WPF

Control.Dispatcher(Delegate, Parameters)

I also know

System.Threading.Thread(ThreadStart)

I prefer to only have a "Thread-Variable" to work with to start Invoking or to use Dispatcher to stay on WPF and WinForms and GeneratorClass with same Thread.

Thanks for your ideas (preferred in VB.Net)

* My working answer *

use the shared Threading.SynchronizationContext.Current to receive the current UI Thread. Then use GuiThread.Send(AddressOf MyMethode, MyParameters) to work in UI thread.

 Private Sub CreateTestImage()
        'This methode is needed to work in Ui Thread
        Dim SyncContext As Threading.SynchronizationContext = Threading.SynchronizationContext.Current 'Read current UI Thread and store it to variable
        If Me._UseMultiThreading = True Then
            'call methode WITH multthreading
            Dim ThS As New Threading.ParameterizedThreadStart(AddressOf CreateTestImageAsync)
            Dim Th As New Threading.Thread(ThS)
            Th.SetApartmentState(Threading.ApartmentState.STA)
            Th.Start(SyncContext)
        Else
            'call methode WITHOUT multthreading
            Call CreateTestImageAsync(SyncContext)
        End If
    End Sub

Methode in thread:

Private Sub CreateTestImageAsync(ByVal obj As Object)
    'Callback is only supporting As Object. 
    'Cast it back the the SynchronizationContext 
    Dim GuiThread As Threading.SynchronizationContext = CType(obj, Threading.SynchronizationContext)

    'Do some stuff

    GuiThread.Send(AddressOf ImageCreated, ImgInfo) 'Call methode in UI thread
End Sub

Upvotes: 1

Views: 543

Answers (1)

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174457

You would pass the current SynchronizationContext to the thread. in your thread it would look something like this:

void ThreadMethod(object parameter)
{
    var context = (SynchronizationContext)parameter;

    context.Send((s) => TheMethodYouWantToRunOnTheUiThread(), null);
}

You would start your thread like this:

var newThread = new Thread(ThreadMethod);
newThread.Start(SynchronizationContext.Current);

It is in C#, but I think you can translate it.

BTW: This is the very same mechanism used by the BackgroundWorker class to marshal the events ProgressChanged and Completed to the UI thread. See here for more info on this topic

Upvotes: 2

Related Questions