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

  • Thread starter Sergey Poberezovskiy
  • Start date
S

Sergey Poberezovskiy

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.
 
S

Steve

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
 
K

kelphis

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.
 
S

Sergey Poberezovskiy

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)
 
S

Sergey Poberezovskiy

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.
 
S

Steve

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
 
S

Sergey Poberezovskiy

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...
 

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