Pass data to background agent, C# windows phone - c#

Did quite a bit of reading on how to get data from my main app to the background agent. Microsoft suggestion seems to be to use isolated storage with mutex.
It is suggested in a few places that you can create a static class in a third project (referenced by main and agent) and pass the data that way (but no samples). I could not get that to work. The background agent did not seem to have access to the static class created by the main app.
Has anyone got that to work? Or is isolated storage the best way?

What you have read in a few places is completely impossible by design.
Background agents live in a separate process. If you'll define static variable in a shared library, you'll have 2 completely independent copies of that variable, one in the GUI process, another one in the background agent process.

Related

ProcessCpuUsage class - Resources Monitor

I want create Resources monitor as UWP application. I read a lot about performanceCounter class from System.Diagnostics but... this class is not contain to UWP. Then i read about some native classes but my skill is too low to implement this yet ;/.
Then i found ProcessCpuUsage class Windows.SystemDiagnostics; and i tried implements this class but i cant find info about constructors and i dont know how implements it? What info can i get from this class ?
In my app i need string with cpu/ram usage, and info about free disk space, and i want to show it as widget. Please Help.
ProcessCpuUsage class has no constructor, it provides access to data about the CPU usage of a process. And this class only has a GetReport method, which gets the ProcessCpuUsageReport for the process. With ProcessCpuUsageReport class, we can get KernelTime and UserTime consumed by the process.
To get a ProcessCpuUsage object, we need use ProcessDiagnosticInfo.CpuUsage property. This is one of the properties in ProcessDiagnosticInfo class. This class provides diagnostic information about a process, such as CPU usage, disk usage, memory usage and so on. And ProcessDiagnosticInfo class has two static methods: GetForCurrentProcess and GetForProcesses that help us to get the ProcessDiagnosticInfo.
However, please note that these two methods can only get the ProcessDiagnosticInfo related to your own app. GetForProcesses method can return a list of ProcessDiagnosticInfo objects for all running processes. But here the "all running processes" means all running processes in the same App Container. For example, for a UWP with out-of-process background tasks, GetForProcesses method may return several ProcessDiagnosticInfos including running background tasks. But for a simple UWP app, it may always return one ProcessDiagnosticInfo. This method can't return diagnostic informations for all running processes in the computer/device as UWP apps are running in app containers and isolated from each other.
So if you are going to create a resources monitor application, UWP may be not a good choice. Classic desktop apps might be better for your scenario.

C# Windows Service with GUI

I am creating a task scheduler that needs to be running even when the user is logged off. From what I've read, a windows service is best for this.
Obviously it also needs a GUI for the user to actually schedule tasks.
I don't think windows services can have a GUI, what is the best way to accomplish this?
You need to write two programs.
The first is the UI for managing the data - adding & removing schedules etc.
The second is the Windows service that just reads the data and acts on it.
You'll need a database or configuration file that both programs can read and write to, so you'll need to have some form of data locking to prevent issues if both the service and GUI are trying to update the data at the same time.
The "database" doesn't have to be a full blown relational database like SQL or oracle. It would probably be enough to be a configuration file that the GUI writes and the service reads. The storage method you choose depends on the volume and complexity of the data you need to store.
You can create scheduled task using "Task Scheduler" utility in Windows. You can specify the frequency, executable etc details in the "Task Scheduler".

how to call specific function from outside an application

Ok so I have a C# program which handles some scheduled tasks. However I need to be able to call a specific function within this program from windows task scheduler.
I have no code or no starting points because frankly I don't even know the proper terminology to put into a google search... its like I am trying to search for "ferrari" by typing in "things with disk shaped objects mounted on the sides". I just have no idea where to even start. I need someone to just give me a quick brief on where I should be looking to accomplish this task. Once I know where to look I can figure it out.
Heres what I need to do.
I have a c# winforms application that has several functions inside of it (lets say method_1, method_2, and method_3). I want to create a windows scheduled task that runs just method_2. I was imagining some sort of launch parameter like you can put in the "target" line of shortcuts like "chrome.exe /d" or whatever.
Thoughts? I am probably over thinking things honestly, but some simple suggestions would be nice.
It's a little unusual to have a windows scheduled task executing a winforms app... scheduled tasks are more commonly used to execute background tasks/services. It's a little hard to give you further advice without knowing more about what you're trying to do.
Usually, you'd move the common logic to its own assembly and compile it as a Class Library.
Then you'd have your WinForms project reference the shared class library, a third project for your scheduled task that also uses the class library.
But if want to send parameters to your WinForms app, you could change your Program.Main signature from this:
static void Main()
to this:
static void Main(string[] args)
The args array will contain the arguments passed to your application. You can interpret these and call the appropriate method.
In addition to dcastro I like to add that you could also create a library and create several dedicated executables to call specific functions inside this library
Then also use this library from your winforms app.
Projects in your solution:
library.dll with all the shared methods
Task1.exe: Exe to be called from the scheduled Task. (References library.dll)
WinFormApp.exe: Your gui (References library.dll)

best way to access a windows service via GUI

I'm curious about the best way for a C# gui to access the functions of a Windows Service, be it WCF or ServiceController or some other option.
I'll explain what I'm doing: on a regulated time interval the service will be zipping one hours worth of datafiles from location A and sending the zipped file to location B, this will be done in the background 24/7 or until the service is stopped by the user and runs even when no one is logged in (hence the need for service) I would like the user to be able to pull up a GUI program that will allow them several options:
1) change the location to zip from
2) change the location to zip to
3) manually start the zipping process for a DateTime range specified
Now most of the functions for zipping and timers is all stored within the service. SO im wondering if a ServiceController in the GUI program would allow me to send variables to/from the service (ie folderpath names as strings, various other data) or if I'll need to spend the time making a WCF and treat the GUI as the Client and the Windows service as a source.
It should be noted the GUI will likely recieve data from the service, but only to signify if it is currently busy zipping.
One option is to tave a WCF service embedded on your windows service. With this WCF you can control the behaviour without restarting the service.
But the best option IMO is to have this in a config file. You can add some keys, but you would have to restart the service when you update the config.
In this case you can try a workaround, as in this thread.
The config is a good place for this kind of detail, because it can be easily modified and, unlike a database, it will be always avaiable.
I don't fully understand what you're trying to say, but you define what the interface to your service is when you make it. If the operations you define take in variables, then you can pass data from your application to the service.
From what you described, just make opertaions in the service to do those 3 things you listed then call those from a button click in your UI code.
WCF would be fine for this here's a basic introduction to it http://msdn.microsoft.com/en-us/library/bb332338.aspx.

How to run a process in a multiuser environment

I running a process for doing some task. Suppose 10 person are requesting to run that process then what will happen?
Whether it will maintain a queue or ?
I am writing the program in C#.
Any answers will be appreciated :)
If you have a simple Application, windows will always create a new process and load the executable within this place.
To have a single application which will get notified if the same user starts the same application a second time you can use the SingleInstance approach.
If you want that several users on the same machine will use the same instance you need to break down another fence. This can be achieved by using a windows service. In this case every user start it's own GUI (maybe using the SingleInstance behaviour) and this GUI will sync the showing up task list with a running service in the background.
Further infos about writing services you find an stackoverflow, google or at msdn.

Categories