Delegates and Event to replace with Interface?

G

Guest

Hi there,

I just read Chris Sells's article at
http://www.codeproject.com/csharp/delegate_bedtime.asp?df=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.
 
R

Richard Blewett [DevelopMentor]

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/delegate_bedtime.asp?df=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]
 
G

Guest

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.
 
R

Richard Blewett [DevelopMentor]

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 said:
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/delegate_bedtime.asp?df=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]
 
Top