I simply want to be able to make a callback on a function in the windows phone code from the server side when something is being updated there. Examples abound on the Internet make use of wsDualHttpBinding, but some wise man has decided to remove support for that from the windows store application API list. I can't find any other way to get the same functionality, does anybody know how to do this?
The reasoning behind not allowing for internet based callbacks is that it puts a big drain on the phone's battery to be constantly listening for them. Instead, they allow you to run PeriodicTask's that will let you occasionally call a server to poll whether there is a change.
Alternatively you could use their notification service:
http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff402558%28v=vs.105%29.aspx
You can try something like this
System.Net.WebClient wc = new System.Net.WebClient();
public void Initialize(object sender, EventArgs e)
{
wc.DownloadStringCompleted += new System.Net.DownloadStringCompletedEventHandler(done);
}
public string version = "1.0.0";
public void done(object sender, System.Net.DownloadStringCompletedEventArgs e)
{
if (version != e.Result)
{
//Do your code here
}
}
You can create a place online that stores the current version then check on start up.
Related
I am beginner in c# with a huge problem.
An application with datagridview in front (Termin plan for one work day) works on many PC's in LAN with MS Windows Server and with MySQL database.
How can I become the changes made on one workstation AUTOMATICALY on all other PC's WITHOUT any action on them (application only started).
I have a procedure for data and datagridview refresh, I must only know WHEN I must start this procedure, that means I must know WHEN any other workstation made any changes.
Thanks for any help!
A simple solution would be to use a timer and when it elapses you refresh you gridview. so on defined period of time it will be refreshed automatically. the problem can be that if you update to often there's a overload of accessing the db. to prevent this, you could make an serverapplication which handles all data
Let's say PC 1 is starting the client application.
First it connects to server application (the server stores the reference of the client e.g. in an list).
After that the user on PC1 makes changes and click on save, the software will send the changes to the server (e.g. a custom object with all needed information).
Server saves the changes to the DB
Serverapplication give a response to the specific client if it worked or not
If it worked, Send an custom object (for example named ChangesDoneEvent) to all clients that indicates that changes have been done.
All connected clients will receive that object and know now that the have to refresh their gridview.
For further information just search for C# Multi threaded Server Socket programming. For sending custom objects over network you will find many resources in the internet too, maybe this will help you Sending and receiving custom objects using Tcpclient class in C#
Declare Delegate on your form
public delegate void autocheck();
private System.Timers.Timer TTTimer = new System.Timers.Timer();
public void autofilldgv()
{
if (this.InvokeRequired)
{
this.Invoke(new autocheck(UpdateControls));
}
else
{
UpdateControls();
}
}
private void UpdateControls()
{
//call your method here
filldgv();
}
void TTTimer_Elapsed(object sender System.Timers.ElapsedEventArgs e)
{
mymethod();
}
public void mymethod()
{
//this method is executed by the background worker
autofilldgv();
}
private void frm_receptionView_Load(object sender, EventArgs e)
{
this.TTTimer.Interval = 1000; //1 sec interval
this.TTTimer.Elapsed += new System.Timers.ElapsedEventHandler(TTTimer_Elapsed);
this.TTTimer.Start();
}
The solution provided above is actually a good way to handle this scenario. Before implementing you might also want to think about the potential fall backs. It is possible that Client PC 's IP could change and since you are using sockets. The object reference added in the list could be faulted state. You might want to think of handling this pitfall.
I develop an application on windowCE 5.0 with opennetcf library.
I want to check WHEN my Device is connected to Cradle. It means I want to handle the event of plugged the device to cradle or other similar.
My purpose is that when Device is connected to Cradle, I disable all forms of my application,
and when it is removed from cradle, all the forms are enabled.
I search much. But the answer is not matched to my expect.
Please help me.
After reading reference of opennetcf, I found out the two events: ACPowerApplied and ACPowerRemoved
Here is my code:
public static event DeviceNotification ACPowerApplied;
public static event DeviceNotification ACPowerRemoved;
void Form1_ACPowerRemoved()
{
MessageBox.Show("Un-cradle");
}
void Form1_ACPowerApplied()
{
MessageBox.Show("Cradle");
}
private void Form1_Load(object sender, EventArgs e)
{
ACPowerApplied += new DeviceNotification(Form1_ACPowerApplied);
ACPowerRemoved += new DeviceNotification(Form1_ACPowerRemoved);
}
But the process did not step into Form1_ACPowerRemoved() and Form1_ACPowerApplied().
Is there any idea for that? Sorry for my poor English. Thank you in advance.
Your code is wrong. You've subscribed to the form's event, and nobody raise it.
Documentation doesn't show how-to-use code, I think. It shows declaraion.
Maybe it will work (not tested):
OpenNETCF.WindowsCE.DeviceManagement.ACPowerApplied += Form1_ACPowerApplied
OpenNETCF.WindowsCE.DeviceManagement.ACPowerRemoved += Form1_ACPowerRemoved
Also you can try to use WinAPI calls: http://blogs.msdn.com/b/davidklinems/archive/2005/02/10/370591.aspx
If you want, I have complete code, but there are a lot of waste and "OnRs232Connect" event.
By the way, what does "craddled" mean for you? Craddle can be disconneted from both AC and PC. I mean, do you want to handle when your device started to get electricity power, or when it started connecting to the PC via Active Sync? If the second, you want to catch "OnRs232Connect" event
I am using this simple way to detect network connect/disconnect events:
NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);
NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);
...
static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
{
RaiseNewtorkChange();
}
static void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
{
RaiseNewtorkChange();
}
static void RaiseNewtorkChange()
{
...
}
The problem is that sometimes NetworkAddressChanged event just stops working, after it was fired several times. Does anyone have an idea why this can happen?
Alternatively, is there another way to handle network connect/disconnect events, using C# or C/C++. Maybe there is such functionality in Windows API or WMI? I need notification on LAN/WiFi network connecting/disconnecting, without polling.
It seems to me that you need to use the Native Wifi API.
Take a look at the function WlanRegisterNotification, in particular this notification:
WLAN_NOTIFICATION_SOURCE_ACM: Registers for notifications generated by the auto configuration module.
Windows XP with SP3 and
Wireless LAN API for Windows XP with SP2: Only the
wlan_notification_acm_connection_complete and
wlan_notification_acm_disconnected notifications are available.
Disclaimer: I have used Native Wifi API in the past, but I have never used this particular feature.
My problem is:
My WebClient uses a function in the Cloud (http://127.0.0.1:81/Service1.svc/Data).
But it is impossible to call several times (for example to make an update).
Here is my code:
private void button_Click(object sender, RoutedEventArgs e)
{
WebClient cnt = new WebClient();
cnt.DownloadStringCompleted += new DownloadStringCompletedEventHandler(cnt_DownloadStringCompleted);
cnt.DownloadStringAsync(new Uri("http://127.0.0.1:81/Service1.svc/Data"));
}
void cnt_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
int num = JsonHelper.FromJson<int>(e.Result);
textbox.Dispatcher.BeginInvoke(() => textbox.Text = Convert.ToString(num));
}
After each click, the application goes well in cnt_DownloadStringCompleted but the result (e.Result) never changes after an update in the server.
At the Azure service, I noticed through a break that the function (Data) is called only once (first time).
How can I do to call my service more than once?
Check your server side to make sure that you aren't allowing caching. If you have the response context in your service, set:
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Alternatively, if you don't have the ability to do this, send in a changing nonsense parameter on the querystring, something like:
http://127.0.0.1:81/Service1.svc/Data?nonsense=190AF142-4341-47DC-9CF5-3BC3ACBD02EE
You can either generate and send in a new guid each time, or even use DateTime.Now.Ticks.ToString(). That being said, if you have control over the server side, eliminate caching, as this way is a serious hack.
I believe web requests are often cached by the emulator (maybe the real phone too).
Here are a few blog posts that mention this, as well as some solutions:
http://www.benday.com/2011/10/06/disable-rest-webrequest-url-caching-on-wp7-mango/
http://www.nickharris.net/2010/10/windows-phone-7-httpwebrequest-returns-same-response-from-cache/
I'm using WebKitDotNet to simulate and automate a web browser. This is really nifty and works in most respects. However, when I try to implement this code, WebKit doesn't trigger a download:
WebKitBrowser _b = null;
private void button1_Click(object sender, EventArgs e)
{
_b = new WebKitBrowser();
_b.DownloadBegin += new FileDownloadBeginEventHandler(b_DownloadBegin);
_b.Error += new WebKitBrowserErrorEventHandler(_b_Error);
_b.AllowDownloads = true;
_b.Navigate("http://sourceforge.net/projects/webkitdotnet/files/WebKit%20.NET%200.x/0.5/WebKit.NET-0.5-bin-cairo.zip/download");
}
void _b_Error(object sender, WebKitBrowserErrorEventArgs e)
{
MessageBox.Show("error!");
}
void b_DownloadBegin(object sender, FileDownloadBeginEventArgs e)
{
MessageBox.Show("hi");
}
Neither the "Error" nor the "DownloadBegin" events fire. I would expect at least one of them to do so - is there a setting that I'm missing?
EDIT: I know this is an old question, but here's the update. When I wrote this question, I was trying to automate a process that required a human being - once per day - to log onto a website, provide credentials, and click a download link. We were hoping to be able to do this programmatically to relieve the monotony for the poor person tasked with doing this job.
Unfortunately, WebKitDotNet did not succeed in this task. Although, in a webkit based browser, you can click on the link and trigger a download, in the embedded WebKitDotNet clicking on the link did nothing. My guess is that something within WebKitDotNet lost the event. If anyone wants to test this, you can use the Sourceforge download link to test.
One of the guys on my team did eventually solve this problem by using an Internet Explorer automation tool called "IMacros". We selected this product because 1) We could guarantee that IE was installed on every computer that would run the program, and 2) IMacros could correctly receive the event from the website and trigger the file download.
On the Issue tracker there is a post date from March 24, 2011 in which is stated that download does not work yet:
https://github.com/webkitdotnet/webkitdotnet/issues/7
Since there are few issues in the tracker, it would have probably been marked as resolved if the feature was added meantime.