Need little help converting c#->vb syntax

L

Leon

Hi there,

I am trying to write a mutli threaded program in VB.Net 2005. As I
am pretty new to writing multithreaded apps I am experimenting with
several ways of implementing things. On a web site I found the following
code which starts a new thread in its own appdommain which sounds very
promising for my needs. Now I've just not managed to translate the C#
code to work properly in VB.Net. But it should be easy for people who are
already experienced with using delegates. Now heres the line of code I
need in VB syntax:

AppDomain ad = AppDomain.CreateDomain ("worker");
Thread t = new Thread (delegate() { ad.DoCallBack (Work); });

best regards
-Leon
 
E

Eternal Snow

It's a pity that VB.Net cannot support to start a new thread with
parameters.
If you sure need to do that, please create a Queue first to send all
parameters in, and read them out while new thread starting.
Dim MyThreading As New Threading.Thread(AddressOf MethodName)
MyThreading.Start
 
L

Leon

Thanks for your reply. The way you described creating new threads is how
it's done if you don't want the new thread to run within a separate
AppDomain. But that's exactly what I'd like to try and hat's what the C#
example does. Passing parameters to the thread isn't required.
 
T

Tom Shelton

It's a pity that VB.Net cannot support to start a new thread with
parameters.
If you sure need to do that, please create a Queue first to send all
parameters in, and read them out while new thread starting.
Dim MyThreading As New Threading.Thread(AddressOf MethodName)
MyThreading.Start

As of 2005, it most certainly can. That is funciton of the
System.Threading.Thread class, and the .NET 2.0 version includes a
ParamertizedThreadStart delegate, which means your threadstart mehtod
can look like:

sub ThreadProc (byval params as object)
' do cool stuff
end sub
 
T

Tom Shelton

Hi there,

I am trying to write a mutli threaded program in VB.Net 2005. As I
am pretty new to writing multithreaded apps I am experimenting with
several ways of implementing things. On a web site I found the following
code which starts a new thread in its own appdommain which sounds very
promising for my needs. Now I've just not managed to translate the C#
code to work properly in VB.Net. But it should be easy for people who are
already experienced with using delegates. Now heres the line of code I
need in VB syntax:

AppDomain ad = AppDomain.CreateDomain ("worker");
Thread t = new Thread (delegate() { ad.DoCallBack (Work); });

best regards
-Leon

Leon,

That is essentially creating a new app domain, and then executing the
Work callback delegate on that domain for a new thread.... It is
using a feature of C# called anonymous methods to do this.

The only way to replecate this in VB.NET 2005, is to do something
like:

' code in some function or event
dim ad as appdomain = appdomain.createdomain("worker")
dim t as new thread (addressof(threadproc))
t.start(ad)

private sub threadproc (byval param as object)
dim ad as domain = directcast(param, appdomain)
ad.docallback(Worker)
end sub
 
G

goodvbprogrammer.freed

Leon,

The best way to multithread in VB.NET is to use a Delegate. If the
"Work" routine does anything to affect any other part of your program
in any way, which probably does b/c otherwise you wouldn't run it,
then you can't use a separate AppDomain b/c it is the same as running
2 separate programs. Instead, use a Delegate. Delegates must have
the same signature as the routine you wish to call, meaning that if
the routine you are calling takes an Integer variable as a parameter,
the Delegate must also take an Integer variable.
Example code to call the MessageBox.Show function asynchronously:

Delegate Function MessageBoxShow(ByVal text As String, ByVal caption
As String, ByVal buttons As _ MessageBoxButtons, ByVal icon As
MessageBoxIcon, ByVal defaultButton As _ MessageBoxDefaultButton) As
DialogResult

Private Sub MessageBoxShow(text As String, caption As String, buttons
As MessageBoxButtons, _
icon As MessageBoxIcon, defaultButton As MessageBoxDefaultButton)
Dim delegateMessageBoxShow As New MessageBoxShow(AddressOf
MessageBox.Show)
delegateMessageBoxShow.BeginInvoke(text, caption, buttons, icon,
defaultButton, AddressOf _ MessageBoxShowCompleted,
delegateMessageBoxShow)
End Sub

Private Sub MessageBoxShowCompleted(result As IAsyncResult)
Dim delegateMessageBoxShow As MessageBoxShow =
CType(result.AsyncState, MessageBoxShow)
Dim diagResult As DialogResult
diagResult = delegateMessageBoxShow.EndInvoke(result)
Select Case diagResult
Case DialogResult.OK
'do something
Case DialogResult.Cancel
'do something
End Select
End Sub

End Example

If the routine you are calling does not return a value, you can
replace "Delegate Function" with "Delegate Sub", delete the As clause
at the end of the line, replace "AddressOf MessageBoxShowCompleted,
delegateMessageBoxShow" with "Nothing, Nothing", and delete the entire
MessageBoxShowCompleted routine. If the routine you are calling
doesn't accept parameters you can delete the code in the parentheses
in the Delegate line and delete the code before "AddressOf" keyword in
the BeginInvoke line.

I hope this helps. If it doesn't, email me and I will clarify.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top