How to get text from online .txt file quickly? (c#) - c#

I'm trying to make a very simple updater app that reads current version.txt file contents and checks if it's the same or greater (in which case update starts).
This post helped me make a working app, but it causes a significant pause in my main app. Could it work faster or is there an easier way to load text from online .txt file?
UPD: for some reason the ~15 second lag only happens at first run, all consequent DownloadStrings work as fast as they should until the app is closed.

You won't be able to magically make the download faster. Instead, I recommend you execute the download on a separate thread so the user can use your app without waiting.

You can try
WebClient client = new WebClient();
client.DownloadFile(url, fileName);
And what do you mean the significant pause?
Is the response time of the server you tried to fetch .txt file?

This could help you to make you app faster:
If you are using winforms, try to use console application (it will do things really fast for this case).
If you want to use winforms, try to use workers to do some process in behind and don't block the user to interact with your app. Read this: http://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx

The pause was caused by default proxy settings. Solution is in my other question:
WebClient.DownloadString takes about 15 seconds when first called

Related

CurrentAppSimulator.RequestProductPurchaseAsync Win 8.1 doesn't work?

Calling the new
var result = await CurrentAppSimulator.RequestProductPurchaseAsync("id");
doesn't work in Windows 8.1?
But when I call await CurrentAppSimulator.RequestProductPurchaseAsync("id", false); it does work though this method is depreciated and I need the result for consumable In-Apps.
Also by "doesn't work" I mean nothing happens. It doesn't bring up the testing popup windows for "CurrentAppSimulator" and just fails.
My test product ID was just not set right... silly me.
Couple of suggestions: Run the app once calling any method on CurrentAppSimulator. Close your app and look at the WindowsStoreProxy.XML file that the simulator creates in your app folder C:\Users\\AppData\Local\Packages\\LocalState\Microsoft\Windows Store\ApiData\WindowsStoreProxy.xml.
This file is created in UTF-16 format. If you take this file and edit it you will have better luck than if you try to create your own file.
http://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.store.currentappsimulator has more details.
Last tip is to remember that this file is never written to. you have to edit it to setup each scenario.
And you'll also have better luck if you create and deploy your own copy for testing.
For me, the file would load but no functions worked when I had it save just a plain ascii file or even after changing it to UTF-8. Had to be saved as UTF-16 to work end-to-end. It would have been nice if the simulator had given some feedback on the ReloadAsync call to save me hours of pain and frustration.

There is a way to delete an file that is being executed without kill it?

I have a question that I believe that is complex. I have an application that I execute under my Windows and it takes a long time to finish. I want to keep it running (normally), however I want to kill the file on disk - but obviously it's not possible because it's locked / in-use. I need a way to disassociate it from the running process to kill it and at the same time keep the file running. Any example of code or tool is very welcome.
Well, workarounds are welcome, for example, if there is a way to spawn it from a process, key the master and migrate the child to kill the app, or any other idea that works is welcome - even the ugly ones. :)
Thanks.
A couple of suggestions (completely stolen) from this questions answers:
You could use the MoveFileEx api function to mark the file for deletion upon next reboot.
You can inject a dll to close the handle yourself:
The typical method is as follows. You've said you want to do this in C# so here goes...
If you don't know which process has the file locked, you'll need to examine each process's handle list, and query each handle to determine if it identifies the locked file. Doing this in C# will likely require P/Invoke or an intermediary C++/CLI to call the native APIs you'll need.
Once you've figured out which process(es) have the file locked, you'll need to safely inject a small native DLL into the process (you can also inject a managed DLL, but this is messier, as you then have to start or attach to the .NET runtime).
That bootstrap DLL then closes the handle using CloseHandle etc.
Essentially: the way to unlock a "locked" file is to inject a DLL into the offending process's address space and close it yourself. You can do this using native or managed code. No matter what, you're going to need a small amount of native code or at least P/Invoke into the same.
Helpful links:
http://www.codeproject.com/KB/threads/winspy.aspx
http://damianblog.com/2008/07/02/net-code-injection/
That is a matter the application you want to kill has to handle. It shouldn't keep files open during a long running process. If the application doesn't close the file, killing it will lead to exception in that application.
Not sure if this will work on every Windows version, but here it is:
Rename process executable "foo.exe" to "foo.old"
Put new "foo.exe" to correct place
Send message to process, so it will execute new "foo.exe" image and terminate himself.
On start, remove "foo.old" file in program directory.
Update: oops, looks like you do not want to put new image, just remove old one. Then MoveFileEx is only "legal" option.

Force close a file for copying

I have a web app that writes data to a file every interval, say 10 minutes.
I have another console app that reads the file and does something with data.
The problem is: sometimes the file is staled, in use by the web app while the console app is trying to copy it. Which causes error, lock error.
I have two questions, How can i force the file to closed from another program, not on the same machine?
Other question related to web app, say, there s no request coming to a server for a while, file is never closed. how can i ensure that after an interval, file is closed? background timer thread?
EDIT: OS is window 2003 server.
any suggestions?
If your program "doing something with the data" doesn't actually modify the file then you can open it read-only instead and avoid the whole locking situation. Do you need write access?
You can keep track of your sessions by writing a session count function. When the number of sessions reaches zero you can then close your file. It would be helpful if you gave us more information about what exactly you are trying to do so we know the program's flow.
Use Session_Start and Session_OnEnd to keep track of sessions. This site has a good article on ways to keep track of sessions.

ASP.NET- How to monitor Database Tables Periodically?

I am developing a website using VS 2008 (C#). My current mission is to develop a module that should perform the following tasks:
Every 15 minutes a process need to communicate with the database to find out whether a new user is added to the "User" table in the database through registration
If it finds an new entry, it should add that entry to an xml file (say NewUsers18Jan2009.xml).
In order to achieve this, which of the following one is most appropriate?
Threads
Windows Service
Other
Are there any samples available to demonstrate this?
Separate this task from your website. Everything website does goes through webserver. Put the logic into class library (so you can use it in the future if you will need to ad on-demand checking), and use this class in console application. Use Windows “Scheduled task” feature and set this console app to run every 15 minutes. This is far better solution than running scheduled task via IIS.
It doesn't sound like there's any UI part to your task. If that's the case, use either a windows service, or a scheduled application. I would go with a service, because it's easier to control remotely.I fail to see a connection to a web site here...
Why in the world would an admin need to get pinged every 15 minutes? Poor admin!
I would just put a time stamp on each entry in your users table and create a quick report to allow the admin to query the data whenever they need it.
I think your approach is wrong. You should do one of two things:
Add a user to the XML file as the last step in creating the user.
Generate the XML file on demand when it is requested. This will give you real-time information with very little overhead.
A windows service would give you a good solution - or if you're using SQL Server you could fire this kind of processing from a SQL Agent job.
If you want the code to be 'part' of your web application you could always fire the logic fro a heartbeat page which runs your task(s) whenever the url is called - you can then poll the url from a service or agent job.
The simplest approach is to create a System.Threading.Timer in your app on Application_Start and put it in some static field so it does not get collected. That way you can have your web app poll the database without an external process needed. Of course if your app goes down so does the timer.
For the polling logic just keep a last userId (if you have an increment policy) and check for new added users by filtering WHERE id > lastId.
Create a new table (or text file) that stores the last time you did a "new user export" or additionally just look for the modified/creation date of the last export file. When your script hits your DB do a SQL command to get all users created after the last export time. Spit out new XML for each user.
Set this script to run as a windows scheduled task/cron job/maybe even a database trigger.
Since you seem to be adding all users for a given day to a single XML file (as per your post), why do you need to do this every 15 minutes? Wouldn't it be sufficient to do this once after midnight?
When you do that, preferably in a Windows Service (if it has to run every 15 minutes) or in a command line app that's scheduled to run once at e.g. 0:15 hours, you'll just need to check the "sign up" date for the users and if there's any that signed up the past day, you add them to your list and export that list to the XML file at the end of processing the table.
Marc
While I also voted for creating a Windows Service to perform this function for you, the simplest way I could think to do it would be to put a trigger on your "Users" table that would create the xml file for you when a user is inserted.
I second Chuck's answer - you could pull this data from the database using an XML query and send it directly, no need to much around creating files on the system.

Ensuring Windows Mobile application is not running - From a desktop app

I have a windows mobile 5.0 application (smartphone) that contains a flat data file (CSV), which contains a header row (two doubles), and a list of entries (two doubles, DateTime, and a string).
Occasionally, I need to "sync" the mobile application with a desktop application. The desktop application should read the CSV from the mobile device, and replace it with a new CSV file, based on the contents of the old one.
This seems pretty easy via RAPI (I'm guessing), but I need to ensure that the mobile application is not running. Is there a way to do this?
Mutex? Remote Process Viewer like stuff? File locking?
Thanks for any help you have
Mike
Just use a simple file locking mechanism for the file being read/updated.
Either rename the file before use or create a second 'lock' file which you can check for the existence of.
For whatever reason, the built-in RAPI Functions don't have anything for checking running processes like the ToolHelp API's. With C you could create a set of custom functions in a device library that call the ToolHelp APIs and in turn are called through CeRapiInvoke (which is a generic catch-all entry point for custom RAPI functions). Unfortunately there's no simple mechanism to do this in managed code.
Just thought of an easy way.
Every 3 seconds the program is running, update a registry key with the current datetime.
When I want to sync, check the mobile registry, check it again 5 seconds later. If the values changed, the program is still running.

Categories