JumpyNET Posted March 25, 2011 Posted March 25, 2011 (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 March 25, 2011 by JumpyNET Quote
Administrators PlausiblyDamp Posted March 25, 2011 Administrators Posted March 25, 2011 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). Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
JumpyNET Posted March 25, 2011 Author Posted March 25, 2011 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? Quote
Leaders snarfblam Posted March 25, 2011 Leaders Posted March 25, 2011 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] Quote [sIGPIC]e[/sIGPIC]
JumpyNET Posted March 26, 2011 Author Posted March 26, 2011 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.