Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

I and also the translator on developerfusion have trouble translating the following lines of code to VB.NET. I hope you guys can help me out.

 

[CS]

operation.Post(new SendOrPostCallback(delegate(object state)

{

EventHandler handler = SomethingHappened;

if(handler != null)

{

handler(this, EventArgs.Empty);

}

}), null);

operation.OperationCompleted();[/CS]

 

And just in case here is the whole code that I am trying to translate:

[CS]

using System;

using System.Threading;

using System.ComponentModel;

 

namespace SynchronizationContextExample

{

public class MySynchronizedClass

{

private Thread workerThread;

private AsyncOperation operation;

public event EventHandler SomethingHappened;

public MySynchronizedClass()

{

operation = AsyncOperationManager.CreateOperation(null);

workerThread = new Thread(new ThreadStart(DoWork));

workerThread.Start();

}

 

private void DoWork()

{

operation.Post(new SendOrPostCallback(delegate(object state)

{

EventHandler handler = SomethingHappened;

if(handler != null)

{

handler(this, EventArgs.Empty);

}

}), null);

operation.OperationCompleted();

}

}

}

[/CS]

Edited by JumpyNET
  • Administrators
Posted

It looks like the converter is being tripped up by some of the differences between c# and vb.net.

 

Try

Imports System.Threading
Imports System.ComponentModel

Namespace SynchronizationContextExample
Public Class MySynchronizedClass
	Private workerThread As Thread
	Private operation As AsyncOperation
	Public Event SomethingHappened As EventHandler

	Public Sub New()
		operation = AsyncOperationManager.CreateOperation(Nothing)
		workerThread = New Thread(New ThreadStart(AddressOf DoWork))
		workerThread.Start()
	End Sub

	Private Sub DoWork()
		operation.Post(New SendOrPostCallback(Sub(state As Object)

												  RaiseEvent SomethingHappened(Me, EventArgs.Empty)
											  End Sub), Nothing)
		operation.OperationCompleted()
	End Sub
End Class
End Namespace

and see if that works (I think it will require .Net 4 though because of the use of a sub rather than a function for a lambda expression).

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

I downloaded VS2010Express and was dissappointed to find out they have made it a 30 trial. :(

 

How could I modify the code to work on VS2008Express using this lambda expression?

  • Leaders
Posted

Jumpy, it looks to me like it's still free. You have 30 days to register, but no purchase required.

Although Microsoft Visual Studio 2010 Express products are provided free of charge, we do require that you register your product within 30 days of installation for continued use. If you are not sure how to obtain your free Visual Studio 2010 Express registration key, follow these instructions.

[/Quote]

[sIGPIC]e[/sIGPIC]
Posted
Jumpy, it looks to me like it's still free. You have 30 days to register, but no purchase required.

Thanks for letting me know. I just registered. :)

 

And thank you PD for the translation help. It worked just perfectly.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...