473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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
6 6832
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jim | last post by:
I have a windows application that is accessing unmanaged code and providing a callback function that will create events at unknown time spans. When the EventHandler (callback function) fires it...
1
by: Srinivasa Ra via .NET 247 | last post by:
(Type your message here) I am writing an application that does lot of read/write's withcomputer's serial port. The application as a whole is workingfine. Current Approach: I have a Timer that...
2
by: mb | last post by:
got the following code snippet that i've modified off the internet to work - but damn if i know how and i dont like that. im having trouble with the following thread declaration that creates the...
19
by: trint | last post by:
Ok, I start my thread job: Thread t = new Thread(new ThreadStart(invoicePrintingLongRunningCodeThread)); t.IsBackground = true; t.Start(); There are lots of calls to controls and many...
3
by: Stewart | last post by:
Hey Group, Hoping someone can help me out. I have some code which starts up some asynchronous code using a delegate. The code is below. Basically my main code (not shown) calls...
4
by: ^MisterJingo^ | last post by:
Hi all, I've been trying to get my head around delegates. The book i'm using had a single example, not much explaination, and didn't show how to set up a delegate and pass variables in and out...
9
by: Terry Olsen | last post by:
I'm running an asynchronous Socket. In the ReceiveCallback method, I need to append what is received to a textbox on the main form. I have this code: Private Sub ToChatWindow(ByVal msg As...
1
by: tccode97 | last post by:
Hi, I need an urgent help. I am developing a socket application in VC++ that uses asynchronous connnection. After doing search on google, I found the following link ...
5
by: Dom | last post by:
Let's say I do this: MyObject o = new MyObject (this, DoWork); .... where DoThis is a function in the calling class. How do I pick this up in the constructor, MyObject? public MyObject...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.