I just played around with delegates and threads. I have a simple method which counts up an integer and writes it to a textfield. If I run this the classic way (in the run event of the thread) all runs nice, the GUI is responding and the counter is displayed. But if I make this with a delegate I have to add a app.doEvents line to the loop otherwise the app doesn’t respond anymore.

You've overridden the Thread.Run method and are running the delegate from within that method. In order for the thread class to operate cooperatively, threaded code must be called from within the Run event.

The built-in version of the Thread.Run method is what causes the Thread.Run event to be executed. Since you've overridden this method you need to make sure to call it from within the override method. You can call an overridden method by using the Super keyword.

Add a property to you custom Thread class (e.g. MethToRun As ThreadMethod). Then re-write your overridden Run method like this:

Because of your note that is prohibited to manipulate the GUI from within a thread I tried applying what they showed in the example of the thread class in the LR. I put the counter in the module and made a timer which writes it to the textfield. But surprisingly the GUI is still affected for example when you move over the buttons the cursor.

Because of your note that is prohibited to manipulate the GUI from within a thread I tried applying what they showed in the example of the thread class in the LR. I put the counter in the module and made a timer which writes it to the textfield. But surprisingly the GUI is still affected for example when you move over the buttons the cursor.

Is that still the wrong way?

My guess is that you're never actually running on a thread. As was pointed out above, subclassing Thread and adding a method with the same name as the Run event means your code is not actually executing in the thread, i.e. from the Run event.

Quote:

My intention was to have a thread class to which I can pass a method instead of making a subclass everytime. If I remember it right I could do this in VB.net

Here's a snippet of code from a Window in one of my projects. Task is a Window property of type Thread, and ImportRun is a method of said Window with the following signature: Protected Sub ImportRun(objCaller As Thread)

Self.Task = New Thread 'Note that I'm using the generic Thread class.AddHandler Task.Run, AddressOf ImportRun 'ImportRun will be called when the thread starts.

Notes:* The handler method must have the correct parameters for whatever event is being handled.* Inside the handler method, Self refers to the instance which owns the method (if any). The caller, in this case a Thread, is passed as a parameter. In the example above, my handler can access any properties or methods of the Window, though it still can't do something that updates the GUI.

Well, looks like going through the whole RB curriculum would be a good idea.

It’s actually pretty simple. Except the parameter that is needed doesn’t look logical to me by now. What I discovered is that even running in a thread it seems to slow down the gui. If you run the method and hover over the buttons the disappear while adding the blue background. Is that because the method runs to fast?