|
Why are delegates used instead of just calling the function in the event handler. There are plenty of reasons. Note that delegates are used to bind functions to events, or to call other objects and functions async.
Lets first explain the first, because of delegates you can add more then one listener to the event. This is usefull in multiview application.
The second one is, at least for me, the most important reason. Thanks to the delegate you will be ably to dynamicly invoke a method. This means that a call to the function will be made, but not executed straight away. So the function calling the delegate continues executing its code.
|
|
|
|
|
Gerben I think your second reason is wrong while the calling method whether directly or using its delegate run in the same thread as caller function, it doesn't continue to next statement until the called method(s) return.
---
"Art happens when you least expect it."
|
|
|
|
|
If you invoke a methond dynamicly, even if they operate in the same thread, the running function will not get interrupted. This is because using dynamic invoke you post a message to your application with the request to execute a function. You do not call the function directly.
Also see: MSDN Article on dynamic invokes.
PS: I tried it in a single thread application just to be sure.
|
|
|
|
|
Yes, I see. So it is one other usage of delegates.
Thank you
---
"Art happens when you least expect it."
|
|
|
|
|
Let me add some notes:
1. A Delegate is a type that is used to construct delegate objects that refrence functions with the same signature as delegate.
Just like classes that construct objects.
2. When a delegate object is created with some function name in its constructor, that object will be indirect agent of that function, and this happens RUNTIME. It is important because when you create a compiled class for your users you don't know what functions they will create to bind as their callbacks or event handlers so you call them directly, so there should be some indirect way for them to introduce their functions and that is Delegate!
I hope that is useful
- Mohammad
---
"Art happens when you least expect it."
-- modified at 1:06 Tuesday 13th December, 2005
|
|
|
|
|
Hi buddies,
I have created a remote object in another AppDomain, after a short time of inactivity it seems to be disconnected and removed because next time I call some method on it, an exception says that the object doesn't exists or it is disconnected.
How can I set lifetime of remote objects? May I set it to live till I explicitly remove it ?
Thank you so much,
- Mohammad
---
"Art happens when you least expect it."
|
|
|
|
|
|
Thank you so much leppie, you have helped me lots of times, I hope I can help u back someday buddy
---
"Art happens when you least expect it."
|
|
|
|
|
|
Hi,
I m new to socket programming and c#.I have been trying to write a simple telnet client which will connect to the machine and execute commands and anle to get back the results.I have been so far able to coonect using TcpClient , but I m getting garbage value in return like %??% etc.
Is there any sample code I can follow or please advise me which class I should follow to process the data obtained from the server ? Currently I m using netStream and read and write methods.
Thanks
- P
|
|
|
|
|
The reason you are getting those garbage characters back is that they are formatting codes for telnet. They are attempting to talk to your program and negotiate a common set of features or telling you how to display a certain character.
There's no easy answer here. You'll have to read the Telnet RFC (Request for comments). It details what these codes are and how you should handle them (and also which ones you can ignore).
http://www.faqs.org/rfcs/rfc854.html[^]
Jared Parsons
jaredp@beanseed.org
http://spaces.msn.com/members/jaredp/
|
|
|
|
|
Hi friends:
I wrote a component in C#(derived from System.ComponentModel.Component).
Now, i want use this component in a COM+ Application with pooling objects
but i have samples of componentServiced, and all classes derived from System.EnterpriseServices.ServicedComponent
can you tellme why the serviced component does not inherit of System.ComponentModel.Component or System.ComponentModel.IComponent ????
thanks in advance
|
|
|
|
|
Hi,
I need a help.
I'm creating a datagid .
It has 4 columns.
On the page load event I called the stored procedure which returns 7 fields out of which i should display 4 columns.
First column was has radiobutton, second,third & 4 th are labels.
the value returned by stored procedure is got in a typed dataset.
There is a field called group id , based on this i need to group rows in datagrid.If a particular group id exists for more than 1 the seond column of the datagrid alone has multiple values seperated by lines , 1st , 3rd & 4th columns have single value.
How can I do this.
Thanks in advance
Regards
Priya
|
|
|
|
|
You're really unspecific as to what you want. Is there any way you could present us with a sample returned dataset, and how you would like it displayed(even if its just typed and spaced accordingly). This would give me more information so I could point you in the direction of resources where you could get the most help.
|
|
|
|
|
Hi,
I want to write a program which will modify the inbound HTML in the IE 6.0. It will capture the inbound HTML before it gets displayed, make some modifications in it and display it in the IE. Can anybody tell me how to achieve this?
The typical flow will be - User will enter the URL in the Internet explorer
Address Bar. The incoming HTML will be read by my program before it gets
displayed. My program will modify some strings - say phone numbers in it and
display the modified HTML.
Technology is not a restriction. I am ready to use VC++ or .NET. Do I have
to use shell Extension? If yes, which interface/event will be helpful?
Thanks in advance.
Regards,
Sunil
|
|
|
|
|
Hello!
That's kinda easy to implement:
Just capture the "DownloadComplete"-event, get the Document-property and then the outertext-property of the body... Change the values and write it back to the outertext-property:
private void m_Browser_DownloadComplete(object sender, System.EventArgs e)
{
IHTMLDocument2 loDocument = (IHTMLDocument2)this.m_Browser.Document;
string lsHtml = Document.body.outerHTML;
// make changes
Document.body.outerHTML = lsHtml;
}
In the above shown example, you can see that it just change the body. Another thing is, that you won't see it in the source-code when you open the source-file with the IE context menue. To change the whole file (and document)... you can write an HTML-file back with the write-methode of the browser component. Don't forget to clear the document object with the clear-methode, b4 you load a new one!
I hope I could help you!
Marcel Erz
-- modified at 9:10 Monday 12th December, 2005
|
|
|
|
|
Hi,
I have some more doubts. As I am novice to all these things, please excuse me if I am talking some non sense.
As per my understanding, above solution assumes that the IE control is embedded in the application. In my case, IE will be invoked from Desktop and I want to capture any HTML which is loaded in that the browser loaded from Desktop. The HTML also might not be mine. It can be loaded by giving any URL like www.google.com or www.msn.com etc. So do I have to get the instance of the Internet Explorer which is invoked from Desktop? If yes, how to get the instance of it?
Thanks,
Sunil
|
|
|
|
|
Hi!
Here is an example to catch all running instances:
public void FindIE() {
// Retrieve list of all IE instances
SHDocVw.ShellWindows loIEs = New SHDocVw.ShellWindows();
SHDocVw.InternetExplorer loIE = null;
int llCount = 0;
mshtml.IHTMLDocument2 loDocument = null;
URLList.Items.Clear();
TitleList.Items.Clear();
IECountText.Text = loIEs.Count;
while(llCount <= loIEs.Count) {
loIE = loIEs.Item(llCount);
llCount++;
if (loIE != null) {
URLList.Items.Add(loIE.LocationURL);
loDocument = (mshtml.IHTMLDocument2)loIE.Document;
if (loDocument != null) {
TitleList.Items.Add(loDocument.title);
}
}
}
}
URLList -> List of all URLs open from instaces
TitleList -> List of all title tag data of all opened documents in the instances
IECountText -> Amount of Instances found
The "SHDocVw.ShellWindows" retrieve all instances of the IE. When every instance get created, it sign in to this list automatically, thats why you have an overview of all IE instances.
"SHDocVw.InternetExplorer" is one instance of an IE. With this, you can kinda do everything what you can do with the browser inside your application. Here an example of changing html of all instances:
public void ChangeAllIE() {
// Retrieve list of all IE instances
SHDocVw.ShellWindows loIEs = New SHDocVw.ShellWindows();
SHDocVw.InternetExplorer loIE = null;
int llCount = 0;
mshtml.IHTMLDocument2 loDocument = null;
URLList.Items.Clear();
TitleList.Items.Clear();
IECountText.Text = loIEs.Count;
while(llCount <= loIEs.Count) {
loIE = loIEs.Item(llCount);
llCount++;
if (loIE != null) {
URLList.Items.Add(loIE.LocationURL);
loDocument = (mshtml.IHTMLDocument2)loIE.Document;
if (loDocument != null) {
loDocument.clear();
loDocument.writeln((object)"Changed html!");
loDocument.close();
}
}
}
}
I hope it is clear so far and it works like I said, coz cannot check it at the moment! I will try it tomorrow morning, but I think it will work! If you need more help, just post it here!
Marcel Erz
P.S.: Plz try to understand the code, and not just copy it. The best way to learn is, to read and understand code!
|
|
|
|
|
Hi Marcel,
Thank you for your reply and sample code. I have one more query. I want to create a toolbar for the Internet Explorer which will have controls like drop down and push buttons. I need to write the program in C#. Can you give me some direction in this regard?
Thanks in advance.
Sunil
|
|
|
|
|
|
Hi,
I wrote the program as mentioned above. However I used the event DocumentComplete instead of DownloadComplete. It is working fine if new URL is given in the browser window. But if Refresh button of browser is clicked, then DocumentComplete event is not getting fired. However DownloadComplete event is getting fired. But DownloadComplete event does not give me the WebBrowser object for which the event is fired as it is available in the DocumentComplete event. Is there any way in this function to get the instance of WebBrowser from which this event is fired?
Thanks in advance.
Regards,
Sunil
|
|
|
|
|
Hi there, C# experts!
I have a (in my view actually simple) problem:
In my application I want to wait for a predefined timespan and show this to the user via a progress bar. However, I want to call the waiting method from my business logic class rather than from my GUI class. Therefor I use following simple code:
public void Wait4Something()
{
// Timespan we want to wait...
int Wtime = 60;
// myBar is the progress bar owned by the form.
// "this" is NOT the form but a motor class
// driving the form and driven by form events.
if( this.myBar != null )
{
this.myBar.Value = 0;
this.myBar.Maximum = Wtime;
this.myBar.Step = 1;
}
this.myGUI.Refresh();
for ( int i = 0; i < Wtime; i += 1 )
{
this.SomeOtherObject.Wait4BroadcastReply( (uint) 1000 );
this.myBar.PerformStep(); // Proceed with the progress bar
this.myGUI.Refresh(); // <- this does not do apparently
}
if( this.myBar != null ) this.myBar.Value = 0;
}
However, if I let the code run, everything is fine until I hit [ALT]-[TAB] to swap windows and then hit it again to swap back to my app.
By then, the GUI freezes, shows the hourglass and does not redraw or revalidate and the progress bar does not do steps either. After the specified waiting time, the control returns to normal and reacts to every event as expected.
I could probably dissolve the whole thing by starting a new thread, but i hoped not to get forced to do this for a simple visualization of waiting :/
Does anyone know of a feasible solution??
Thanks in advance for your regard,
Udo
"You can do it that way or that way. I like it that way."
|
|
|
|
|
An Application.DoEvents() call instead (or additional) to the Refresh could do the job. But note that you then probably have to disable the form because it will react to user input.
A seperate thread would be better...
|
|
|
|
|
Hi Robert,
thanks for your instant help, I was afraid of it but in the end it's probably best to do it right in the first place. So I'll be weaving some threads...
Thanks,
Udo
----
Anyway, Application.DoEvents() did help as a hotfix, and as I'm not doing anything mutual it's probably harmless to let the GUI react to user input, if not wanted. So thanks again for your very helpful advice!
|
|
|
|
|
I have come across with a little problem that must be simpler for you guys;
As a matter of fact, i m new in .Net/C# environment, hence i m facing a little bit difficulty in writing code.
I have a form (Non-MDI); on its next button after successfull execution of a method i have to open a new form and for doing this i have written the following code:
DBSecurity.DBSecurity msecure=new DBSecurity.DBSecurity(strServer,strUser,strPwd,strDatabase);
if (msecure.Generate(txtCompany.text,txtLocation.Text)==true)
{
SecurityGUI.frmGUI frm=new frmGUI();
frm.ShowDialog();
this.Close();
}
this code works fine as far as frmGUI form opening is concerned but it do not close the existing form and it still open on back.
Am i writing wrong code? or do you have another solution?
|
|
|
|