By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,680 Members | 1,744 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,680 IT Pros & Developers. It's quick & easy.

need help convert c# to VB (passing delegate into Thread constructor)

P: n/a
I have the following code in C# that I have trouble converting to VB(2.0):

private delegate void openDialog();
private void openWindowsDialog(openDialog open)
{
Thread thread = new Thread(new ThreadStart(open));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}

I tried the following code, but it would not compile:

Private Delegate Sub openDialog()
Private Sub openWindowsDialog(ByVal open As openDialog)
Dim thread As Thread = New Thread(New ThreadStart(open)) ' exception
here: Error 2 'System.Threading.ThreadStart' is a delegate type and requires
a single 'addressof' expression as the only argument to the constructor.
thread.SetApartmentState(ApartmentState.STA)
thread.Start()
End Sub

Thanks in advance for any help.
Aug 27 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Excerpt from MSDN:

"Visual Basic users can omit the ThreadStart constructor when creating a
thread. Use the AddressOf operator when passing your method for example
Dim t As New Thread(AddressOf ThreadProc). Visual Basic automatically
calls the ThreadStart constructor."

Steve C.
MCSD,MCAD,MCSE,MCP+I,CNE,CNA,CCNA
Sergey Poberezovskiy wrote:
I have the following code in C# that I have trouble converting to VB(2.0):

private delegate void openDialog();
private void openWindowsDialog(openDialog open)
{
Thread thread = new Thread(new ThreadStart(open));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}

I tried the following code, but it would not compile:

Private Delegate Sub openDialog()
Private Sub openWindowsDialog(ByVal open As openDialog)
Dim thread As Thread = New Thread(New ThreadStart(open)) ' exception
here: Error 2 'System.Threading.ThreadStart' is a delegate type and requires
a single 'addressof' expression as the only argument to the constructor.
thread.SetApartmentState(ApartmentState.STA)
thread.Start()
End Sub

Thanks in advance for any help.

Aug 27 '07 #2

P: n/a
On Aug 27, 12:34 pm, "Sergey Poberezovskiy" <se...@compros.com.au>
wrote:
I have the following code in C# that I have trouble converting to VB(2.0):

private delegate void openDialog();
private void openWindowsDialog(openDialog open)
{
Thread thread = new Thread(new ThreadStart(open));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();

}

I tried the following code, but it would not compile:

Private Delegate Sub openDialog()
Private Sub openWindowsDialog(ByVal open As openDialog)
Dim thread As Thread = New Thread(New ThreadStart(open)) ' exception
here: Error 2 'System.Threading.ThreadStart' is a delegate type and requires
a single 'addressof' expression as the only argument to the constructor.
thread.SetApartmentState(ApartmentState.STA)
thread.Start()
End Sub

Thanks in advance for any help.
Private Sub openWindowsDialog()
Dim Thread as Thread = New Thread(addressOf openDialog)
thread.SetApartmentState(ApartmentState.STA)
thread.Start()
End Sub

i didnt compile this so if it doesnt work im sorry.

Aug 27 '07 #3

P: n/a
Sure it does not work - you are trying to invoke the delegate declaration -
whereas I need to invoke the specific delegate that was passed into the
procedure.

Anyhow, I have worked out a solution - I needed to pass AddressOf
open.Invoke as a parameter:
Dim Thread as Thread = New Thread(addressOf open.Invoke)

"kelphis" <am******@hotmail.comwrote in message
news:11**********************@o80g2000hse.googlegr oups.com...
On Aug 27, 12:34 pm, "Sergey Poberezovskiy" <se...@compros.com.au>
wrote:
>I have the following code in C# that I have trouble converting to
VB(2.0):

private delegate void openDialog();
private void openWindowsDialog(openDialog open)
{
Thread thread = new Thread(new ThreadStart(open));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();

}

I tried the following code, but it would not compile:

Private Delegate Sub openDialog()
Private Sub openWindowsDialog(ByVal open As openDialog)
Dim thread As Thread = New Thread(New ThreadStart(open)) '
exception
here: Error 2 'System.Threading.ThreadStart' is a delegate type and
requires
a single 'addressof' expression as the only argument to the constructor.
thread.SetApartmentState(ApartmentState.STA)
thread.Start()
End Sub

Thanks in advance for any help.

Private Sub openWindowsDialog()
Dim Thread as Thread = New Thread(addressOf openDialog)
thread.SetApartmentState(ApartmentState.STA)
thread.Start()
End Sub

i didnt compile this so if it doesnt work im sorry.

Aug 27 '07 #4

P: n/a
Steve,

I can read the MSDN - and NEVER ask question without checking the doco
first. Unfortunately, the question I asked is not clearly covered in MSDN,
and your reply is totally irrelevant to the question - try to create paste
the code into a VB class and compile by following YOUR advice.

"Steve" <ln********************@gmail.comwrote in message
news:OM*************@TK2MSFTNGP02.phx.gbl...
Excerpt from MSDN:

"Visual Basic users can omit the ThreadStart constructor when creating a
thread. Use the AddressOf operator when passing your method for example
Dim t As New Thread(AddressOf ThreadProc). Visual Basic automatically
calls the ThreadStart constructor."

Steve C.
MCSD,MCAD,MCSE,MCP+I,CNE,CNA,CCNA
Sergey Poberezovskiy wrote:
>I have the following code in C# that I have trouble converting to
VB(2.0):

private delegate void openDialog();
private void openWindowsDialog(openDialog open)
{
Thread thread = new Thread(new ThreadStart(open));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}

I tried the following code, but it would not compile:

Private Delegate Sub openDialog()
Private Sub openWindowsDialog(ByVal open As openDialog)
Dim thread As Thread = New Thread(New ThreadStart(open)) '
exception here: Error 2 'System.Threading.ThreadStart' is a delegate type
and requires a single 'addressof' expression as the only argument to the
constructor.
thread.SetApartmentState(ApartmentState.STA)
thread.Start()
End Sub

Thanks in advance for any help.

Aug 27 '07 #5

P: n/a
Hmmm, strange. If you read the MSDN article I quoted, you'd see the
sample VB code right below it showing exactly how to do what you're
asking. I don't think it could get any clearer than that.

Just trying to help.
Steve C.
MCSD,MCAD,MCSE,MCP+I,CNE,CNA,CCNA
Sergey Poberezovskiy wrote:
Steve,

I can read the MSDN - and NEVER ask question without checking the doco
first. Unfortunately, the question I asked is not clearly covered in MSDN,
and your reply is totally irrelevant to the question - try to create paste
the code into a VB class and compile by following YOUR advice.

"Steve" <ln********************@gmail.comwrote in message
news:OM*************@TK2MSFTNGP02.phx.gbl...
>Excerpt from MSDN:

"Visual Basic users can omit the ThreadStart constructor when creating a
thread. Use the AddressOf operator when passing your method for example
Dim t As New Thread(AddressOf ThreadProc). Visual Basic automatically
calls the ThreadStart constructor."

Steve C.
MCSD,MCAD,MCSE,MCP+I,CNE,CNA,CCNA
Sergey Poberezovskiy wrote:
>>I have the following code in C# that I have trouble converting to
VB(2.0):

private delegate void openDialog();
private void openWindowsDialog(openDialog open)
{
Thread thread = new Thread(new ThreadStart(open));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}

I tried the following code, but it would not compile:

Private Delegate Sub openDialog()
Private Sub openWindowsDialog(ByVal open As openDialog)
Dim thread As Thread = New Thread(New ThreadStart(open)) '
exception here: Error 2 'System.Threading.ThreadStart' is a delegate type
and requires a single 'addressof' expression as the only argument to the
constructor.
thread.SetApartmentState(ApartmentState.STA)
thread.Start()
End Sub

Thanks in advance for any help.

Aug 27 '07 #6

P: n/a
Steve,

to make it clear - I needed to start a thread with a delegate passed as a
PARAMETER into a procedure, so I could not use AddressOf the parameter and
omitting the ThreadStart does not help in any way...

"Steve" <ln********************@gmail.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Hmmm, strange. If you read the MSDN article I quoted, you'd see the sample
VB code right below it showing exactly how to do what you're asking. I
don't think it could get any clearer than that.

Just trying to help.
Steve C.
MCSD,MCAD,MCSE,MCP+I,CNE,CNA,CCNA
Sergey Poberezovskiy wrote:
>Steve,

I can read the MSDN - and NEVER ask question without checking the doco
first. Unfortunately, the question I asked is not clearly covered in
MSDN, and your reply is totally irrelevant to the question - try to
create paste the code into a VB class and compile by following YOUR
advice.

"Steve" <ln********************@gmail.comwrote in message
news:OM*************@TK2MSFTNGP02.phx.gbl...
>>Excerpt from MSDN:

"Visual Basic users can omit the ThreadStart constructor when creating a
thread. Use the AddressOf operator when passing your method for example
Dim t As New Thread(AddressOf ThreadProc). Visual Basic automatically
calls the ThreadStart constructor."

Steve C.
MCSD,MCAD,MCSE,MCP+I,CNE,CNA,CCNA
Sergey Poberezovskiy wrote:
I have the following code in C# that I have trouble converting to
VB(2.0):

private delegate void openDialog();
private void openWindowsDialog(openDialog open)
{
Thread thread = new Thread(new ThreadStart(open));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}

I tried the following code, but it would not compile:

Private Delegate Sub openDialog()
Private Sub openWindowsDialog(ByVal open As openDialog)
Dim thread As Thread = New Thread(New ThreadStart(open)) '
exception here: Error 2 'System.Threading.ThreadStart' is a delegate
type and requires a single 'addressof' expression as the only argument
to the constructor.
thread.SetApartmentState(ApartmentState.STA)
thread.Start()
End Sub

Thanks in advance for any help.
Aug 27 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.