0

I am trying to translate the following C# snippet to VB:

public bool ShowHandlerDialog(string message)
        {
            Message = message;
            Visibility = Visibility.Visible;

            _parent.IsEnabled = false;

            _hideRequest = false;
            while (!_hideRequest)
            {
                // HACK: Stop the thread if the application is about to close
                if (this.Dispatcher.HasShutdownStarted ||
                    this.Dispatcher.HasShutdownFinished)
                {
                    break;
                }

                // HACK: Simulate "DoEvents"
                this.Dispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate { }));
                Thread.Sleep(20);
            }

            return _result;
        }

But the translation is giving an error on this line:

this.Dispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate { }));

The translation is:

Me.Dispatcher.Invoke(DispatcherPriority.Background, New ThreadStart(Function() Do End Function))

Which doesnt seem to convert correctly the bit after New ThreadStart. Can somebody please explain what 'delegate {}' does in

new ThreadStart(delegate {}));

and how I might correct the translation error? Thanks for any advice!

1 Answer 1

1

That line simply fires up a new thread and waits for it to finish. The "delegate { }" code is simply an anonymous/inline method (I don't think that is supported in VB.NET); just as if you would point to an empty method basically. For instance, in c# event-handlers can be bound to anonymous (inline) delegate methods as so:

this.OnClick += (EventHandler)delegate(object sender, EventArgs ea) {
    MessageBox.Show("Click!");
};

The comment above says [// HACK: Simulate "DoEvents"]. Just replace the two lines with DoEvents for VB.NET and you should be set. That then allows other threads to do their work before continuing, thus improving responsiveness.

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.