Communicating and Controlling web page from C# - c#

I have already opened a webpage in Firefox which is actually running through the apache web server on the same machine. And I have C# application running on the same machine.
Now I want to refresh/inform my already open web page that C# has finished processing.
I have seen the threads all over the web and also some similar threads on Stackoverflow but did not find solution.
Any idea how to solve this?
In details: C# Application is transferring images from external storage. Web page (http://127.0.0.1/mypage.php) is looking for the images periodically, but never know that all images are transferred or not so that it can process these images to do some more work.
In this scenario, we want to tell our web page (which is already running in firefox) all data transferred and now you can refresh to process data.

Two steps: Let the server communicate that C# is done, and have the web page react to it
You somehow need to expose the fact that the C# program has finished to the web. If you also have an IIS running, this could be done via a URL served by IIS directly that returns a value indicating whether C# is still running or not. If you don't, you can write to a file, database or whatever and have a script on your Apache server that checks for this value. Whatever you choose, you want something like www.myserver.com/are_you_finished.[php, aspx, whatever] that returns 1 or 0
Then, you can build JavaScript script in your client page that periodically checks this URL and reacts as soon as the value is 1. This would be a typical AJAX call, ie you need to play with XmlHttpRequest. This could be elaborated much further, but maybe you first say if that's what you have in mind, and I also think there's a lot of good documentation on how to do this on here.

You can include a Internet Explorer control in ouyr appliacation. This way you can control which page is displayed and you can call refresh and whatever you want.
Another way is to include a meta refresh tag in the html page so it periodically looks for updates and refreshes ifself.
I see no way to remote control firefox. The only thing i could think of is remember the process id when you start firefox, and the you kill the process and start a new window, but I consider this bad style.

Related

How to get any User Computer's Win32_PhysicalMedia data via a Web Site?

When I use ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia") in a webpage, it provides server informations. But I need computers information of any user that is using the website. I prepared an website including Win32_PhysicalMedia query. However it returns only the server computers information. Is it possible to get personal computers information from a web site?
Thanks.
Well of course any computer code running on the web server can get information about the web server that running that code.
However, the client side?
that is a browser, and what happens if you running a iPad, or Andriod phone? Such information about the client side is not possbile.
Why?
Well, when you come to my web site to view a cute cat picture? I can't have code run on YOUR computer that mucks around, looks for a document called "my passwords", or looks for files called banking. Or how about I mess around and steal all your family pictures?
If you could do that, the internet would be the worlds worst system and it would repsrent no security at all.
So, information about the client side computer that you can get?
Well, you can get information about what kind of browser they are using. and some Javascript on that web page can get say the current screen resolution (or better said the size of the browser window).
but, things like what OS, the hardware, and all your banking information and files and things about that computer? Nope, that is 100% hands off, and browser are VERY much securied, and are what we called sandboxed VERY tight. For example, while you can drop say a file-Upload control into a web page?
and that control will let the USER click on that button, and choose a file? You can't EVEN in JavaScript (browser side, client side script running) SET or CHOOSE or PICK the file name!!! - in other words, even that browser control is secured by NOT allowing you the develop to pick a local file. Again, this is for reasons of secuirty, since again as noted, while you look at my web site cute cat pictures, you can't have browser code running around and grabbing your files from YOUR computer while you are looking at MY WEB site!!!!
So, browser code can't even select a file to up-load. The user MUST select the file, and then when you up-load that file, ONLY the file name is passed along with the file data (not even the file path name to the file on YOUR computer is sent to the web server. Again, this is all locked down for reasons of security.
If you need information about the client side computer, then you would have to provide a user with a program to download. They would run the desktop program, it would then do your type of hardware select query, gather the information and THEN send the data up to your web site (I guess you would create a web method, or so called web API for that desktop program to call and send the data to). Of course, you would need a program written for Android phones, apple phones, windows desktop, Apple desktops and even more choices.
So, web based software is VERY much locked down, and has ZERO abiliity to get information about the client side computer. As noted, you can in JavaScript get information about what browser the user is using. (but then again, say chrome runs on Android and desktop). And you can as noted get the current window size, which in most cases will tell you what screen resolution. And a good many browsers will also allow you to get the users current zoom level (I been wanting to track this information for a very long time - since after 125% zoom, some of my web pages look like garbage).
So, you have to grasp how the web and this supposed fad called the internet works. The client side, or client desktop computers (or phones) that interact with your web site are using a browser, and as noted, those browsers are VERY much locked down, and this is of course for good reason - that reason being security, and thus I can sleep well at night, that when I go to visit YOUR web site with a cute cat picture, your not then running code on my computer that is pocking and peeking around with my personal computer that is MY computer, and not YOUR computer.

Auto restart IIS Website from console application

We have an ASP.Net application used by many customers installed on their own servers. Because it is installed on their end with each having different databases and URL bindings etc I created a console application a while ago that gets a zip file and extracts it to c:\inetpub location to append the latest application changes. This console app is added to scheduled tasks to create an automated update.
Obviously when anyone accesses the site for the first time after it does this they have to wait a little longer whilst the site rebuilds. I changed the console application to include a Process.Start(urlofapp) so that it should hopefully do this as part of the update so the next morning that first user doesn't have to wait for the rebuild.
I have tested it ok running myself but not yet released as my concern is that this url process is kept open. Can anyone enlighten me as to whether this would be the case as I don't want this to happen or can give me any ideas as to how to rebuild the site manually as part of the console app.
IIS has had an auto-start apps feature for quite some time. You just have to enable it. You can find more info from the Gu, and the IIS site.
Safer instead to use a start page which loads everything on the server in the background. If you do this logic in an async method which in turn is called from an MVC controller for example then this can potentially run whilst the main page has finished. Alternatively use a threaded Task and show a start page that is only returned whilst setup is taking place.

updating aspx page from server

Currently I am working on a project where a web page will start a 3 hour data transfer process through the use of a web service. Basically a list of Id's are used to tell the web service to transfer an data object from one database to another.
When I start the process, I would like to allow the user to remain in control. He must be able to see how far the current process is, and be able press a cancel button to stop the data transfer. All this is run from an ASPX web page.
I have discovered that trying to run the process asynchronous does not allow me to update the user interface as I had hoped. Only when the entire process is finished Another problem I might face is that since the process takes a lot of time, the server objects might get refreshed at some point, which could cause me to lose my progress.
I am currently at the point where I am deciding whether or not to use the web server to process the data transfer. If I am, my current solution is to use tasks to run the process asynchronously, and use client side web calls (to the aspx page) to update the client interface. I am also implementing the IRegisteredObject interface for my work object. IRegisteredObject explanation
Any idea on how to best tackle this problem is most welcome. I really want to know if I'm heading in the right direction.
My suggestion would be for you to create a WCF service, which is not hosted within ASP.NET and to make your ASP.NET application call that service to trigger the long running job that performs the data transfer. Meanwhile your ASP.NET app notifies the user that it has triggered the job and you can expose other endpoints on your service which the ASP.NET application can query through a user request in order to extract and report progress.
The web server will only run one page at a time for each user, so if you want to communicate with the browser while the process is running, you can't run the progress from a request. You need to start it as an independent background thread, so that you can finish the request that started the process.
After that you can send requests to the server either to do polling for status of the process, or to control the process.
If possible you should run the process entirely outside of IIS, for example as a console application. That way you only have to keep track of the fact that there is a process running in the web application, for example putting that in a database so that it survives IIS recycling.

Process.Start from user machine

I have a simple asp.net page that knows how to call some commands.
For example, i call mstsc.exe with specific parameters:
Process.Start("mstsc.exe", "/v:" + serverToConnect);
When i use this code on my local machine it works fine.
When i upload this code to an asp.net page on different server and try to click
on the button call this command, nothing happened.
I guess i need to somehow tell the page to call this command from the machine of the user who clicked this button.
Do you know what is the best way doing it?
Thanks.
Not possible.
Stop for a second and imagine the havoc unleashed upon the internet if any website could arbitrarily execute applications on any user's computer.
It works on your local machine because, by coincidence alone, when you're testing it your machine is both the server and the client. The behavior is still the same, the application runs on the server. When you publish it to another server, that behavior continues... The application runs on the server.
Whatever you're trying to accomplish, this is not a feasible approach. It sounds like you want to install a client-side application on users' computers, which is a very different thing than an ASP.NET web application.
Running this code from a web page will result in the process executing on the server. It is not possible to run this code on the local machine from an asp.net page.
All .NET code in an asp page runs on the server.

Control a process using webforms

I am trying here to find a solution to control a process I launch via webforms.
I know it is quite easy to start-stop it using System.Diagnostics.Process class.
What I am trying to achieve is to send data to the process (a terraria server). Basically the server itself when it is launched correctly you can write inside it some commands like kick/ban/save/exit etc.
I'd like to write those commands but remote controlled via a webforms application.
The nearest solution I've thinked would have been to call the server via a daemon I would write that would work as gateway between the webforms commands and the server application. Is that the right way to do things ? Or is there any easier way to do it ?
Are you perhaps looking for the BackgroundWorker class that was introduced in .Net 3.5?
To execute a time-consuming operation in the background, you create a BackgroundWorker, and then listen for events that reportt the progress of your operation and signal when your operation is finished. It executes on a separate thread.
The MSDN site has some examples that might help you figure out if this is for you.
Or you could just use a web service or WCF, couldn't you? You can either host the web service on a web server or as a Windows service.
You can probably start the process on application start and keep a reference to it in a static variable. You'll also need to attach to the OutputDataReceived event so you can record any data the process sends to its StandardOutput stream.
The idea is that the process will be running on the server and the web application will be silently recording any information it sends. When a request comes the buffer will be sent to the user and cleared.
The user can also send commands which the web application will forward to the process through its StandardInput stream.
I haven't tried it but it could possibly work. You will also need to consider what will happen if the application pool recycles. For best results I think you should disable autorecycling.
Yes that is the best way, make windows service that will control and monitor the process and into that windows service build API for a ASP.NET (webforms) application.
ASP.NET application can automatically refresh page (via javascript or meta refresh tag) and get new status from windows service that is controling your process, and event better you can build javascript application and get new status data and send commands with ajax, so there will be no need for whole page refresh.

Categories