473,396 Members | 2,147 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,396 software developers and data experts.

Delegates and Event to replace with Interface?

Hi there,

I just read Chris Sells's article at
http://www.codeproject.com/csharp/de...269#xx922269xx

I wonder i can do this:

1)

I want to built in a class library that had multithreading enabled.

My initial overview of the library:

interface IParent
{
void funcA();
}

class Parent : IParent
{
// used this to be extended by other ppl in future
}

interface IChildA
{
int funcB();
}

interface IChildB
{
int funcC();
}

class ChildA : Parent, IChildA
{

}

class ChildB : Parent, IChildB
{

}

After look into your article, is it possible to replace interfaces into
delegates (correct me if i misunderstood).

to be like this?

interface IParent
{
void funcA();
}

class Parent : IParent
{
// used this to be extended by other ppl in future
}

delegate int funcB();
delagate int funcC();

class ChildA
{
public event funcB evtFuncB;

....
}

class ChildB
{
public event funcC evtFuncC;

....
}

So when, my Windows Form i call it,

I can trigger ChildA (maybe in buttonA) and ChildB (maybe in buttonB) ???

2)

At the of your article, does this code need to be there or be removed?

statirc int WorkerCompletedWork()
{
System.Threading.Thread.Sleep(4000); // is this a must?
}

Coz i am doing a program, that had 2 buttons, if i click buttonA, i hope i
can click on buttonB while buttonA backend is still processing.

Hope someone can clear my doubts! Thanks.
--
Regards,
Chua Wen Ching :)
Nov 16 '05 #1
3 2380
Chris?s sleep was simply there to slow that method down so he could demonstrate ?asynchronous delegate invocation. No you don't need to sleep in your method. Also the fact that the method is static was purely to demonstrate that you can call static methods using delegates as well as instance methods. I only know this because this used to be the template of the Delegate module in out Essential.NET course - and in fact some of the original slides are still in there.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/
Hi there,

I just read Chris Sells's article at
http://www.codeproject.com/csharp/de...269#xx922269xx

I wonder i can do this:

1)

I want to built in a class library that had multithreading enabled.

My initial overview of the library:

interface IParent
{
void funcA();
}

class Parent : IParent
{
// used this to be extended by other ppl in future }

interface IChildA
{
int funcB();
}

interface IChildB
{
int funcC();
}

class ChildA : Parent, IChildA
{

}

class ChildB : Parent, IChildB
{

}

After look into your article, is it possible to replace interfaces into delegates (correct me if i misunderstood).

to be like this?

interface IParent
{
void funcA();
}

class Parent : IParent
{
// used this to be extended by other ppl in future }

delegate int funcB();
delagate int funcC();

class ChildA
{
public event funcB evtFuncB;

...
}

class ChildB
{
public event funcC evtFuncC;

...
}

So when, my Windows Form i call it,

I can trigger ChildA (maybe in buttonA) and ChildB (maybe in buttonB) ???

2)

At the of your article, does this code need to be there or be removed?

statirc int WorkerCompletedWork()
{
System.Threading.Thread.Sleep(4000); // is this a must?
}

Coz i am doing a program, that had 2 buttons, if i click buttonA, i hope i can click on buttonB while buttonA backend is still processing.

Hope someone can clear my doubts! Thanks.
--
Regards,
Chua Wen Ching :)

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.760 / Virus Database: 509 - Release Date: 10/09/2004

[microsoft.public.dotnet.languages.csharp]

Nov 16 '05 #2
Hi Richard,

Thanks a lot. So does it mean, i can replace the interface with delegates
and events as shown on my previous example.

I am trying to look for articles to implement delegates/events/threading
within the dll itself rather than windows form. Means the dll takes care
everything.

What do you think? Thanks again.

"Richard Blewett [DevelopMentor]" wrote:
Chris?s sleep was simply there to slow that method down so he could demonstrate ?asynchronous delegate invocation. No you don't need to sleep in your method. Also the fact that the method is static was purely to demonstrate that you can call static methods using delegates as well as instance methods. I only know this because this used to be the template of the Delegate module in out Essential.NET course - and in fact some of the original slides are still in there.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/
Hi there,

I just read Chris Sells's article at
http://www.codeproject.com/csharp/de...269#xx922269xx

I wonder i can do this:

1)

I want to built in a class library that had multithreading enabled.

My initial overview of the library:

interface IParent
{
void funcA();
}

class Parent : IParent
{
// used this to be extended by other ppl in future }

interface IChildA
{
int funcB();
}

interface IChildB
{
int funcC();
}

class ChildA : Parent, IChildA
{

}

class ChildB : Parent, IChildB
{

}

After look into your article, is it possible to replace interfaces into delegates (correct me if i misunderstood).

to be like this?

interface IParent
{
void funcA();
}

class Parent : IParent
{
// used this to be extended by other ppl in future }

delegate int funcB();
delagate int funcC();

class ChildA
{
public event funcB evtFuncB;

...
}

class ChildB
{
public event funcC evtFuncC;

...
}

So when, my Windows Form i call it,

I can trigger ChildA (maybe in buttonA) and ChildB (maybe in buttonB) ???

2)

At the of your article, does this code need to be there or be removed?

statirc int WorkerCompletedWork()
{
System.Threading.Thread.Sleep(4000); // is this a must?
}

Coz i am doing a program, that had 2 buttons, if i click buttonA, i hope i can click on buttonB while buttonA backend is still processing.

Hope someone can clear my doubts! Thanks.
--
Regards,
Chua Wen Ching :)

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.760 / Virus Database: 509 - Release Date: 10/09/2004

[microsoft.public.dotnet.languages.csharp]

Nov 16 '05 #3
There is no need to use an interface for callbacks in .NET ? delegates are a much more flexible approach as there is no concept of type compatibility required neither does the name of the method to receive the event ? as long as a method has the correct signature then it can be hooked up to an event.

The DLL can take care of everything but there is a problem. If you are going to use the delegates asynchronously then the completion callback is going to happen on a threapool thread. YOU CANNOT UPDATE THE UI FROM THIS THREAD. So you need to call Control.BeginInvoke on either the form or control in question - which means the DLL must have visibility of a UI element

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

?
nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/
Hi Richard,

Thanks a lot. So does it mean, i can replace the interface with delegates and events as shown on my previous example.

I am trying to look for articles to implement delegates/events/threading within the dll itself rather than windows form. Means the dll takes care everything.

What do you think? Thanks again.

"Richard Blewett [DevelopMentor]" wrote:
Chris?s sleep was simply there to slow that method down so he could demonstrate ?asynchronous delegate invocation. No you don't need to sleep in your method. Also the fact that the method is static was purely to demonstrate that you can call static methods using delegates as well as instance methods. I only know this because this used to be the template of the Delegate module in out Essential.NET course - and in fact some of the original slides are still in there.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/
Hi there,

I just read Chris Sells's article at
http://www.codeproject.com/csharp/de...f=100&forumid=
2983&select=922269#xx922269xx

I wonder i can do this:

1)

I want to built in a class library that had multithreading enabled.

My initial overview of the library:

interface IParent
{
void funcA();
}

class Parent : IParent
{
// used this to be extended by other ppl in future }

interface IChildA
{
int funcB();
}

interface IChildB
{
int funcC();
}

class ChildA : Parent, IChildA
{

}

class ChildB : Parent, IChildB
{

}

After look into your article, is it possible to replace interfaces into delegates (correct me if i misunderstood).

to be like this?

interface IParent
{
void funcA();
}

class Parent : IParent
{
// used this to be extended by other ppl in future }

delegate int funcB();
delagate int funcC();

class ChildA
{
public event funcB evtFuncB;

...
}

class ChildB
{
public event funcC evtFuncC;

...
}

So when, my Windows Form i call it,

I can trigger ChildA (maybe in buttonA) and ChildB (maybe in buttonB) ???

2)

At the of your article, does this code need to be there or be removed?

statirc int WorkerCompletedWork()
{
System.Threading.Thread.Sleep(4000); // is this a must?
}

Coz i am doing a program, that had 2 buttons, if i click buttonA, i hope i can click on buttonB while buttonA backend is still processing.

Hope someone can clear my doubts! Thanks.
--
Regards,
Chua Wen Ching :)

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.760 / Virus Database: 509 - Release Date: 10/09/2004

[microsoft.public.dotnet.languages.csharp]


[microsoft.public.dotnet.languages.csharp]

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Natalia DeBow | last post by:
Hi, I am working on a Windows-based client-server application. I am involved in the development of the remote client modules. I am using asynchronous delegates to obtain information from...
4
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on...
2
by: OpticTygre | last post by:
Ok, so I've been trying and trying to learn about delegates, but I still can't grasp the concept of the advantages. Why use delegate functions instead of calling the function directly? In other...
30
by: Burkhard | last post by:
Hi, I am new to C# (with long year experience in C++) and I am a bit confused by the language construct of events. What is it I can do with events that I cannot do with delegates? At the moment...
13
by: heddy | last post by:
Using Jesse Liberty's excellent book on C#, I am looking at chapter 12 - Delegates. Now, I was under the impression that a delegate is essentially a pointer to a member function. However, when I...
2
by: kristian.freed | last post by:
Hi, I currently work in a project written fully in C# where we make extensive use of delegates and events. We have a model where a "state", an object holding data but not much code but which...
5
by: raylopez99 | last post by:
I understand delegates (static and non-static) and I agree they are very useful, and that the "Forms" used in the Windows .NET API could not work without them. That said, I'm curious as to how...
6
by: =?Utf-8?B?T2xkQ2FEb2c=?= | last post by:
My question is regarding the use of delegates in C#. I see how .Net uses delegates to wire event handlers to events. It’s an object created by a single line of code by the system and that makes...
6
by: Tem | last post by:
I've read every example i could find on the subject and still couldn't figure out its proper usage. What's the point of delegates, why can't I just invoke the method directly??? Can someone...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
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...
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.