C# Download Manager (Thread, ThreadPool, BeginInvoke, BackgroundWorker) - winforms

So here it is. I want to make some test download manager (winforms), and want to use 4 different ways (managers):
1. BackGroundWorker manager
2. Thread manager
3. ThreadPool manager
4. Delegate.BeginInvoke manager
Download from ftp. Of course make it async for multidownloading, and when i want to add new download, previous download might still work. So, my problem is architecture. I imagine this smth like:
1) create interface IDownloadManager. Here - enum Status(Idle, download, downloaded, canceled, errorDownloaded), some events (DownloadStarted, DownloadComleted, DownloadCancelled, DownloadStopped), method for Percent, method for Start.
2) ManagerFactory class, that creates selected Manager.
3) IManagerFactory interface, that handle with creating Manager
4) ManagerHost class, where stores all information of current managers.
Can anyone tell me, where is my architecture mistakes? give me some advises, suggestions etc..Thanks for reading and helping!

It seems good. Hope the architecture you designed will work well.

Keep in mind that support for cancellation or progress for all those '4 different ways' will end up with the implementation of the BackgroundWorker. The backgroundworker combines the uses of the threadpool and synchronizationcontext for UI purposes.

Related

VB.Net WPF Single instance application, with new instance opening window of first

I want to be able to write a WPF application that only allows one instance of it, but if the user opens another instance, the first instance opens a window. So, I need
Single instance only WPF application
Inter-process communication between new instances and the original, which allows the new instances to tell the first one to open a window.
I would like to know if this is possible, and how to do it, because so far, Google hasn't been of much help. I have seen other questions ask this problem, but none have actually been of help (and a few have not been in vb.net, but have still not answered this problem). I would really like a proper example of how to do this, because even though I have been using vb.net for a while, this sort of stuff I have never encountered.
Single instance only WPF application
You can use a named Mutex to ensure only one instance is running, see “make single instance application” what does this do? (example given in VB.Net)
Inter-process communication between new instances and the original
There are a number of options here, including WCF, .Net Remoting and Named Pipes, see What is the best choice for .NET inter-process communication?

Implementing startup tasks for SL5 OOB application

I am putting together a Silverlight 5 application that will run out-of-browser and has a bit of everything on start-up. Specifically, here are the steps I need to follow:
Check that the app is running out of browser (if not, display a screen instructing the user to install it locally).
Display a "splash screen" (it would be nice if this would play animations while the remaining steps execute).
Configure MEF
Pre-load context information and 'static' data from the server (for example, settings). This data is required before any of the application logic can run.
Dynamically load additional XAP files including an external theme library.
Replace the "splash screen" with the shell which contains a navigation frame.
Navigate to the application's start page.
I also need to support Application Extension Services (IApplicationService, IApplicationLifetimeAware) so any process I implement must respect these services. Most of these services will require MEF to be configured, so they should not execute before MEF has been configured and imports satisfied.
Another consideration is that some imports may be satisfied only after the dynamic XAP files have been pulled in and MEF recomposed.
One of the hurdles I'm running into is the fact that I cannot do step 5 until the previous steps are complete. Loading the XAP files or calling the server for data asynchronously allows the code to proceed. I need a way to "stall" the UI until all of the composition is complete and all required context data has been loaded.
So, I'm looking for recommended approaches that satisfy all of these requirements and am happy to provide more details if that helps get to a working solution.
UPDATE
The best explanation I can give for my difficulty is that I must 'release' the UI thread to display a 'splash screen' but also suspend the normal life cycle of the application while each step executes. I can't (and don't want to) do everything in the Application.Start event handler because application services will have already started.
Plus, releasing the UI thread means I do work in the background and let the original method (Application.Start, for instance) return and the runtime will move forward in the startup process. For example, starting a background process in the Starting method of an application service then returning allows the runtime to raise the Start event on the Application object. But if I need the background process to complete before I can do the next thing, I have to suspend the current thread which blocks the UI.
So I'm not sure how to divide up the work or where to put it (App, application service, bootstrapper, workflow, etc.).
You can use ManualResetEvent class to force the service calls to be Sync (and guarantee the order they complete). You could also use this to synchronize the background thread to the main UI thread.
http://mohundro.com/blog/2006/06/27/a-little-bit-about-manualresetevent/

Auto update dialog / Notification for WPF Application

I want a automatic update notification in my application. A message box should appear which tells that an update is available, if user wants then it can download the latest version in downloads folder of windows. Nothing else (user will install it manually) not application.
-I'm using Installshield so no Click once solution.
Thanks
If you want an out-of-the-box solution to this problem you're likely to be disappointed. I haven't found anything that works except ClickOnce, and I dislike it. I did find this:
http://windowsclient.net/articles/appupdater.aspx
My solution was to roll my own. It's actually not that difficult. I wrote a small bootstrapper application that first checks for updates, downloads them if necessary, and then launches my application in a new AppDomain. Pretty easy.
If you want to check for updates while your app is running, you need to write and add a component/class to your project that performs that task, and informs the user (MessageBox or whatever) that an update is available. If they choose to perform the update then you need to launch your bootstrapper (so it can fetch the updates) and kill your current process.
All of this is very possible with a little time and some custom code. It's not as difficult as it sounds. The biggest thing is determining how configurable you want your custom solution to be because that can affect when/where your bootstrapper goes to look for updates (I built mine to look for updates on a network share).
http://autoupdatewpf.codeplex.com/
i found one. This one is quite simple and solve the purpose.

Can Silverlight (SLOOB) start a process even with full trust?

I have been tasked with writing an installer with a silverlight out of browser application. I need to.
get the version off a local EXE
check a web service to see that it is the most recent version
download a zip if not
unpack the zip
overwrite the old EXE
start the EXE
This installer app is written in .NET WinForms now but the .NET framework is an obstacle for people to download.
The recommended solution is to use a SLOOB however i am not sure how to assign full trust. If i assign full trust can I start a process.
Thanks
Looking into this, I suspect you're going to have to create the process using WMI through the COM interface. At the end of the day, that makes this a very difficult option and very subject to failure due to a host of reasons (WMI being disabled or secured, user won't give full trust, etc.) I suspect you would be much better off creating a .msi deployment package or something similar that was able to go out and download the framework, if necessary. There are a lot of deployment models available, almost all of which feel superior to this one.
That said, if you're going to do this:
To get the COM object, you're going to want to use the AutomationFactory.CreateObject(...) API. Tim Heuer provides a sample here.
To actually do the WMI scripting, you're going to want to create the WbemScripting.SWbemLocator object as the root. From there, use the ConnectServer method to get a wmi service on the named machine. You can then interrogate the Win32_Process module to create new processes.
Edit: I spent a little time working on this and, even on my local machine as Admin I'm running into security problems. The correct code would be something similar to:
dynamic locatorService = AutomationFactory.CreateObject("WbemScripting.SWbemLocator");
dynamic wmiService = locatorService.ConnectServer("winmgmts:{impersonationLevel=impersonate,authentationLevel=Pkt}//./root/cimv2");
dynamic process = wmiService.Get("Win32_Process");
dynamic createParameters = process.Methods_["Create"].InParameters.SpawnInstance_;
createParameters.CommandLine = "cmd.exe";
wmiService.ExecMethod("Win32_Process", "Create", createParameters);
Silverlight 4 will have support for something like this: http://timheuer.com/blog/archive/2010/03/15/whats-new-in-silverlight-4-rc-mix10.aspx#sllauncher

Custom Publish/Subscribe eventing and the build it .Net eventing in Winforms

I had to design and implement my own publish subscribe eventing to satisfy the requirements of a dynamic UI Winform design. Is there any .Net library that supports pub/sub out there that I can compare mine and improve?
Why not start with this list-based publish/subscribe pattern (using WCF) on MSDN?
Actually I went further than that. For example you can create a SystemEvent and publish it on a subject with a name of 'Application.UI.TreeInstance.[InstanceName].Node.Clicked' and if you register for anything like 'Application.' or 'Aplication.UI.' or 'Application.UI.TreeInstance.[InstanceName].Node.*' or the full subject you will get called on your delegate through multicast and receive the event. The SystemEvent you can define as you want; it has a ValueNamePairCollection and a public interface to get the data out as you feel like. You can register for synchonous and asynchronous processing and offcourse Unregister as well. My UI elements now process events that they care without having to know who publishes them.
If the community has any interest in stuff like this I could package up the code and make it available so we can all share and improve. I have tested it thoroully and works 100%.Also I wrote some monitoring GUI that you can see in real time what is happening with publications and subscriptions as well as registrations and unregistrations.

Resources