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(By Val msg As String)
txtChat.AppendT ext(msg)
End Sub
I've never used a delegate or invoke before and I'm rather confused on the
topic. Hope someone can help.
Thanks. 9 1970
Hi,
"Terry Olsen" <to******@hotma il.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. .. 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(By Val msg As String) txtChat.AppendT ext(msg) End Sub
I've never used a delegate or invoke before and I'm rather confused on the topic. Hope someone can help.
1) You first define a delegate, to understand delegates better, you have to
know the compiler creates a class that can wrap a sub or function with the
same signature for each delegate you define. Like other classes a delegate
can be used as a variable and can be instantiated, the constructor takes a
sub or function that matches the delegate signature. BUT, in VB.NET
AddressOf returns an instantiated delegate, so you don't need to explicitly
use the delegate's constructor.
2) When ToChatWindow is called, we check if InvokeRequired is needed (eg.
when you call from an other thread) and if it is needed, then we call
txtChat.Invoke which will invoke ToChatWindow again (through a delegate
instance), but this time it runs on the UI thread and InvokeRequired will be
false.
Public Class SomeForm
Inherits Form
' define delegate ( signature matches ToChatWindow )
Private Delegate Sub ToChatWindowHan dler( msg As String )
' delegate instance
Private ToChatWindowHan dlerInst As ToChatWindowHan dler
Public Sub New()
' create delegate instance
ToChatWindowHan dlerInst = AddressOf ToChatWindow
End Sub
Public Sub ToChatWindow( msg As String )
If ( txtChat.InvokeR equired ) Then
txtChat.Invoke( ToChatWindowHan dlerInst, _
New Object() { msg } )
Return ' dont forget
End If
' at this point you are in UI thread
txtChat.AppendT ext( msg )
End Sub
End Class
Don't want to confuse you, but notice that you can also call
SomeDelegateIns tance.Invoke(<p arameters>), but this will just invoke the
wrapped function without marshalling it to the UI thread unlike
Control.Invoke( SomeDelegateIns tance, <parameters>) which does marshall to
the UI thread(which is the thread that created the Control).
And one more thing, there is Control.Invoke and Control.InvokeL ater, the
former will wait until the function has run on the UI thread, the latter
will not wait until it is run and return immediately.
HTH,
Greetings
Thanks.
So the following code goes in Form1's NEW method? Public Sub New() ' create delegate instance ToChatWindowHan dlerInst = AddressOf ToChatWindow End Sub
Hi,
"Terry Olsen" <to******@hotma il.com> wrote in message
news:u9******** ******@TK2MSFTN GP09.phx.gbl... So the following code goes in Form1's NEW method?
Yes, that's the idea, you need an instantiated delegate so why not do it in
the form's constructor (Sub New), you could also use Form_Load, if you have
any problems with it or with the code explain where you think it goes wrong.
Usually a Form already has a New method, so it should look like:
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeCompo nent()
' Add any initialization after the InitializeCompo nent() call.
ToChatWindowHan dlerInst = AddressOf ToChatWindow
End Sub
HTH,
Greetings Public Sub New() ' create delegate instance ToChatWindowHan dlerInst = AddressOf ToChatWindow End Sub
Thanks for the help. I was able to simplify it and I came up with this,
which works great!
Delegate Sub ToChatWindowDel egate(ByVal msg As String)
Private Sub ToChatWindow(By Val msg As String)
If txtChat.InvokeR equired Then
txtChat.Invoke( New ToChatWindowDel egate(AddressOf ToChatWindow), New
Object() {msg})
Else
txtChat.AppendT ext(msg)
End If
End Sub
Hi,
"Terry Olsen" <to******@hotma il.com> wrote in message
news:OD******** *****@TK2MSFTNG P15.phx.gbl... Thanks for the help. I was able to simplify it and I came up with this, which works great!
Delegate Sub ToChatWindowDel egate(ByVal msg As String)
Private Sub ToChatWindow(By Val msg As String) If txtChat.InvokeR equired Then txtChat.Invoke( New ToChatWindowDel egate(AddressOf ToChatWindow), New Object() {msg}) Else txtChat.AppendT ext(msg) End If End Sub
That's ok too and i almost posted that, but then changed it, because
ToChatWindow could be called alot and each AddressOf creates a new
instantiated delegate, so to avoid re-creating the delegate each time, i
used a delegate field and created the delegate inside the ctor.
Greetings
Thank-you for the excellent code..I learned a lot from it.
--
Dennis in Houston
"Bart Mermuys" wrote: Hi,
"Terry Olsen" <to******@hotma il.com> wrote in message news:OD******** *****@TK2MSFTNG P15.phx.gbl... Thanks for the help. I was able to simplify it and I came up with this, which works great!
Delegate Sub ToChatWindowDel egate(ByVal msg As String)
Private Sub ToChatWindow(By Val msg As String) If txtChat.InvokeR equired Then txtChat.Invoke( New ToChatWindowDel egate(AddressOf ToChatWindow), New Object() {msg}) Else txtChat.AppendT ext(msg) End If End Sub
That's ok too and i almost posted that, but then changed it, because ToChatWindow could be called alot and each AddressOf creates a new instantiated delegate, so to avoid re-creating the delegate each time, i used a delegate field and created the delegate inside the ctor.
Greetings
Hi,
"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:1E******** *************** ***********@mic rosoft.com... Thank-you for the excellent code..I learned a lot from it.
You're welcome, it is a commonly used pattern that i explained a bit.
Greetings
-- Dennis in Houston
"Bart Mermuys" wrote:
Hi,
"Terry Olsen" <to******@hotma il.com> wrote in message news:OD******** *****@TK2MSFTNG P15.phx.gbl... > Thanks for the help. I was able to simplify it and I came up with > this, > which works great! > > Delegate Sub ToChatWindowDel egate(ByVal msg As String) > > Private Sub ToChatWindow(By Val msg As String) > If txtChat.InvokeR equired Then > txtChat.Invoke( New ToChatWindowDel egate(AddressOf ToChatWindow), New > Object() {msg}) > Else > txtChat.AppendT ext(msg) > End If > End Sub >
That's ok too and i almost posted that, but then changed it, because ToChatWindow could be called alot and each AddressOf creates a new instantiated delegate, so to avoid re-creating the delegate each time, i used a delegate field and created the delegate inside the ctor.
Greetings
>
I see your point, but doesn't the delegate go away when the method it calls
exits?
"Bart Mermuys" <bm************ *@hotmail.com> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. .. Hi,
"Terry Olsen" <to******@hotma il.com> wrote in message news:OD******** *****@TK2MSFTNG P15.phx.gbl... Thanks for the help. I was able to simplify it and I came up with this, which works great!
Delegate Sub ToChatWindowDel egate(ByVal msg As String)
Private Sub ToChatWindow(By Val msg As String) If txtChat.InvokeR equired Then txtChat.Invoke( New ToChatWindowDel egate(AddressOf ToChatWindow), New Object() {msg}) Else txtChat.AppendT ext(msg) End If End Sub
That's ok too and i almost posted that, but then changed it, because ToChatWindow could be called alot and each AddressOf creates a new instantiated delegate, so to avoid re-creating the delegate each time, i used a delegate field and created the delegate inside the ctor.
Greetings
Hi,
"Terry Olsen" <to******@hotma il.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. .. I see your point, but doesn't the delegate go away when the method it calls exits?
After Invoke there is no reference anymore, so it/they will be cleaned up
the next time GC runs, but that wasn't really the point.
It may be a personal thing, but i don't like the idea to create a new
delegate instance each time the function is called expecially when there is
an easy way to avoid it and the function is called repeatedly.
Performance-wise it may not differ that much.
Greetings "Bart Mermuys" <bm************ *@hotmail.com> wrote in message news:%2******** ********@TK2MSF TNGP15.phx.gbl. .. Hi,
"Terry Olsen" <to******@hotma il.com> wrote in message news:OD******** *****@TK2MSFTNG P15.phx.gbl... Thanks for the help. I was able to simplify it and I came up with this, which works great!
Delegate Sub ToChatWindowDel egate(ByVal msg As String)
Private Sub ToChatWindow(By Val msg As String) If txtChat.InvokeR equired Then txtChat.Invoke( New ToChatWindowDel egate(AddressOf ToChatWindow), New Object() {msg}) Else txtChat.AppendT ext(msg) End If End Sub
That's ok too and i almost posted that, but then changed it, because ToChatWindow could be called alot and each AddressOf creates a new instantiated delegate, so to avoid re-creating the delegate each time, i used a delegate field and created the delegate inside the ctor.
Greetings
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Sandor Heese |
last post by:
Question,
When using BeginInvoke (on a From to marshal to the UI thread) with the
EventHandler delegate I see some strange behaviour. The problem is that I
call the UpdateTextBox method with a class derived from EventArgs ( i.e.
MyEventArgs) and when the BeginInvoke is called and the UpdateTextBox
methode is call on the UI thread the parameter e (EventArgs) does not
contain the derived MyEventArgs object but a EventArgs object.
The...
|
by: RickDee |
last post by:
Please help, anybody.
I am trying to write a program so that it can launch an exe file ( which is
also genereated in C# ) and then simulate the button clicking and invoke the
methods inside the exe. The button clicking part is working fine, but I just
cannot get the method call.
Here is my program snippet.
**************************************************************
|
by: Paul Tomlinson |
last post by:
Morning all. Let me describe my problem.
Main thread creates child objects and threads and starts them
Timer on the main thread (which only gets activated after the child threads
have started) periodically calls a function Refresh().
This refresh function runs on its own thread and queries our database for
new jobs.
For each job found this timer invokes a delegate on one of the child thread
objects.
I then get the error "Cannot call...
|
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 checks once every millisecond to see if there is any data avaialable to read at theserial port. If so then reads it. The serial port is openedwithout OVERLAPPING.
TRYING TO GET TO: I don't think polling serial port every millisecond is a good approach....
|
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 happen in function calls
from invoicePrintingLongRunningCodeThread. I need just an example in
| |
by: Daylor |
last post by:
few questions about Delegate in vb.net :
do i need to create NEW delegate each time i use invoke ?
or i can use the same delegate object when using specific invoke ?
and if can use the same one, in case its multithread application, i need to
put it in synclock ?
--------------
|
by: Amir Shitrit |
last post by:
Hi to all.
Whats the difference between DynamicInvoke and Invoke methods of the
Delegate class?
Thanks in advance.
|
by: Christian Kaiser |
last post by:
We have a component that has no window. Well, no window in managed code - it
uses a DLL which itself uses a window, and this is our problem!
When the garbage collector runs and removes our component (created
dynamically by, say, a button click, and then not referenced any more), the
GC runs in a different thread, which prohibits the DLL to destroy its
window, resulting in a GPF when the WndProc of that window is called - the
code is gone...
|
by: Sergey Poberezovskiy |
last post by:
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();
}
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |