|
Return it and pick it up from the task Result.
This space for rent
|
|
|
|
|
Return it from itself?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
If it works similar like a thread, then you can pass an object as a parameter; according to MSDN there is indeed an overload that provides exactly that.
Task.ContinueWith Method (System.Threading.Tasks)[^]
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Either return it from the StartNew lambda and pick it up from the result, as Pete suggested:
Task.Factory.StartNew(() =>
{
service.Start();
return service;
}, service.CancellationTokenSource.Token)
.ContinueWith(task =>
{
var service1 = task.Result;
...
});
Or, since you're using a lambda method, you can reference it using the closure, in the same way as the StartNew delegate:
Task.Factory.StartNew(() =>
{
service.Start();
}, service.CancellationTokenSource.Token)
.ContinueWith(task =>
{
service.DoSomethingElse();
...
});
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
i understand the both factory and factory method pattern. in factory pattern we create instance of my classed by another class function dynamically where i pass some parameter to another class function and based on that parameter another class function return right instance of class.
in factory method pattern we have to proceed one further step. in factory method pattern subclass create instance of my class. i do not find a scenario where people has to go for factory method pattern. so please some one come with a scenario where normal factory pattern will not be used rather people prefer to use factory method pattern.
here i am posting two set of code first one done by factory pattern and second one done by factory design pattern
1st set of code where factory pattern used
public enum Shipper
{
UPS = 1,
FedEx = 2,
Purolator = 3
}
public interface IShip
{
void Ship();
}
public class ShipperPurolator : IShip
{
public void Ship()
{
MessageBox.Show("Purolator ship start");
}
}
public class ShipperUPS : IShip
{
public void Ship()
{
MessageBox.Show("UPS ship start");
}
}
public class ShipperFexEx : IShip
{
public void Ship()
{
MessageBox.Show("FedEx ship start");
}
}
public class ShipperFactory
{
public static IShip CreateInstance(Shipper enumModuleName)
{
IShip objActivity = null;
switch (enumModuleName)
{
case Shipper.UPS:
objActivity = new ShipperUPS();
break;
case Shipper.FedEx:
objActivity = new ShipperFexEx();
break;
case Shipper.Purolator:
objActivity = new ShipperPurolator();
break;
default:
break;
}
return objActivity;
}
}
Calling this way
IShip objActivity = null;
private void btnUPS_Click(object sender, EventArgs e)
{
objActivity = ShipperFactory.CreateInstance(Shipper.UPS);
objActivity.Ship();
}
private void btnFedEx_Click(object sender, EventArgs e)
{
objActivity = ShipperFactory.CreateInstance(Shipper.FedEx);
objActivity.Ship();
}
private void btnPurolator_Click(object sender, EventArgs e)
{
objActivity = ShipperFactory.CreateInstance(Shipper.Purolator);
objActivity.Ship();
}
now same thing done by factory method pattern where i have to write more code to get the job done
public interface IShip
{
void Ship();
}
public class ShipperPurolator : IShip
{
public void Ship()
{
MessageBox.Show("Purolator ship start");
}
}
public class ShipperUPS : IShip
{
public void Ship()
{
MessageBox.Show("UPS ship start");
}
}
public class ShipperFedEx : IShip
{
public void Ship()
{
MessageBox.Show("FedEx ship start");
}
}
public interface IShipFactory
{
IShip GetShipper();
}
public class ShipperFexExFactory : IShipFactory
{
public IShip GetShipper()
{
return new ShipperFedEx();
}
}
public class ShipperUPSFactory : IShipFactory
{
public IShip GetShipper()
{
return new ShipperUPS();
}
}
public class ShipperPurolatorFactory : IShipFactory
{
public IShip GetShipper()
{
return new ShipperPurolator();
}
}
calling like this way
IShipFactory _IShipFactory = null;
private void btnUPS_Click(object sender, EventArgs e)
{
_IShipFactory = new ShipperUPSFactory();
_IShipFactory.GetShipper().Ship();
}
private void btnFedEx_Click(object sender, EventArgs e)
{
_IShipFactory = new ShipperFexExFactory();
_IShipFactory.GetShipper().Ship();
}
private void btnPurolator_Click(object sender, EventArgs e)
{
_IShipFactory = new ShipperPurolatorFactory();
_IShipFactory.GetShipper().Ship();
}
please tell me a solid scenario when people feel right to choose factory method pattern. thanks
tbhattacharjee
|
|
|
|
|
|
He doesn't like reading: it makes it difficult to be a Help Vampire.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
It's like a stake through the heart.
This space for rent
|
|
|
|
|
Quote: He doesn't like reading
Yeah, and that is exactly why a few days ago I failed to understand his thread on hashing/encryption collected 30+ replies.
|
|
|
|
|
It surprised me as well. Granted, I did reply - but that was to warn people that he was a Help Vampire.
This space for rent
|
|
|
|
|
In our collective defence, most of 'em were to Gerry - who was trying to defend the indefensible without knowing anything about what he was saying.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
That is alright then
|
|
|
|
|
Tridip Bhattacharjee wrote: please tell me a solid scenario when people feel right to choose factory method pattern. thanks IDbConnection.CreateCommand();
Doesn't even take parameters
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
after searching google for Factory pattern definition i found it is
Factory pattern
In Factory pattern, we create object without exposing the creation logic. In this pattern,
an interface is used for creating an object, but let subclass decide which class to instantiate.
The creation of object is done when it is required.
The Factory method allows a class later instantiation to subclasses.
see my Factory pattern related code and there is no subclass which instantiate class object rather i have class with a static function which create & return class object.
so my question is that above definition is saying "subclass decide which class to instantiate" but in my code there is no subclass or child class rather a separate class create instance. is the above definition is for factory method pattern ?
here is my code
public enum Shipper
{
UPS = 1,
FedEx = 2,
Purolator = 3
}
public interface IShip
{
void Ship();
}
public class ShipperPurolator : IShip
{
public void Ship()
{
MessageBox.Show("Purolator ship start");
}
}
public class ShipperUPS : IShip
{
public void Ship()
{
MessageBox.Show("UPS ship start");
}
}
public class ShipperFexEx : IShip
{
public void Ship()
{
MessageBox.Show("FedEx ship start");
}
}
public class ShipperFactory
{
public static IShip CreateInstance(Shipper enumModuleName)
{
IShip objActivity = null;
switch (enumModuleName)
{
case Shipper.UPS:
objActivity = new ShipperUPS();
break;
case Shipper.FedEx:
objActivity = new ShipperFexEx();
break;
case Shipper.Purolator:
objActivity = new ShipperPurolator();
break;
default:
break;
}
return objActivity;
}
}
calling this way
IShip objActivity = null;
private void btnUPS_Click(object sender, EventArgs e)
{
objActivity = ShipperFactory.CreateInstance(Shipper.UPS);
objActivity.Ship();
}
private void btnFedEx_Click(object sender, EventArgs e)
{
objActivity = ShipperFactory.CreateInstance(Shipper.FedEx);
objActivity.Ship();
}
private void btnPurolator_Click(object sender, EventArgs e)
{
objActivity = ShipperFactory.CreateInstance(Shipper.Purolator);
objActivity.Ship();
}
please guide me regarding above definition.
thanks
tbhattacharjee
|
|
|
|
|
What you have there is fine. In the GoF (Gang of Four) definition, they are talking about the idea of having something like an abstract base class - the factory method will use this as the return type, but would create concrete versions of the classes that are subclassed off this. As you are using an interface instead (a better choice to my mind), this is your return type.
This space for rent
|
|
|
|
|
Tridip Bhattacharjee wrote: please guide me regarding above definition. Open this page[^], and scroll down to the answer that begins with "Let's understand difference between Factory and FactoryMethod with an example".
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
See this MSDN example
If Task.Wait blocks, why would you ever want to use it?? If I'm in a WPF app and I launch a task from the main thread, it blocks the UI, so why bother using a thread? If I'm going to block the main thread, why not just put the thread's code in the UI to start with?
Someone please give me a real example of when I would really want to use Task.Wait.
Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
You are assuming here that the only time that you would want to use a Task is off the UI thread. This isn't the case. As an example, suppose you wrote some code to index files in a particular folder - and you then want to finish the task only once the indexing has completed, you could very well create a top level task and then fire off multiple tasks inside - with a Wait on them completing.
This space for rent
|
|
|
|
|
So, Task A spawns 3 child tasks, then waits for all 3 to finish.
In this case you would want Task A to run async, correct?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
|
In that example, it would be better to use Task.WhenAll[^] to wait for the child tasks to complete.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
That's true. I was trying more to illustrate the point that waiting on a task or a series of tasks doesn't mean that you created it off a UI thread.
This space for rent
|
|
|
|
|
There are very few situations where it's necessary to block the current thread until a task has completed. The only obvious one that comes to mind is when you're writing a console application, and you want to use async code. And with .NET 4.5 or higher, it's usually better to call task.GetAwaiter().GetResult() rather than task.Wait() , to avoid having any exceptions wrapped in an AggregateException .
static class Program
{
static void Main()
{
MainAsync().GetAwaiter().GetResult();
}
static async Task MainAsync()
{
...
}
}
But in most cases, there's usually a better option than blocking the current thread.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard Deeming wrote: There are very few situations where it's necessary to block the current thread until a task has completed.
That's why I asked. I can't see a reason to block, and the example I posted shows the Main method of a console app creating a thread that blocks.
It's a typical stupid MSDN example, but I thought I'd ask anyhow.
Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
I guess it's difficult to produce a meaningful real-world example that doesn't end up with the concept you're trying to demonstrate buried in unrelated implementation details.
You just need to imagine that the task is an async method, calling an API that's only available via asynchronous task-returning methods. The Main thread is synchronous, so you're going to have to block at some point to wait for the API calls to complete.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|