MobileCache m_mobileCache1;
m_mobileCache1 = new MobileCache(@"C:\user\MobileCache");
m_mobileCache1.Open();
Map mMap = new Map();
mMap.MapLayers.AddRange(m_mobileCache1);
if (m_mobileCache1.CacheExists)
{
m_mobileCache1.Close();
m_mobileCache1.DeleteCache();
}
MobileServiceConnection con = new MobileServiceConnection();
con.Url = @"http://server/arcgis/services/Test_Mobile_Services/Radiation_Data_V1_B11/MapServer/Mobileserver";
con.CreateCache(m_mobileCache1);
m_mobileCache1.Open();
MobileCacheSyncAgent agent = new MobileCacheSyncAgent(m_mobileCache1);
agent.MapDocumentConnection = con;
agent.DownloadExtent(m_mobileCache1.GetExtent(), 0, 0);
mMap.MapLayers.AddRange(m_mobileCache1);
mapControl1.MapLayers.AddRange(m_mobileCache1);
using ESRI.ArcGIS.Mobile.WPF; ESRI.ArcGIS.Mobile.WPF.Map mMap = new ESRI.ArcGIS.Mobile.WPF.Map(); mMap.MapLayers.AddRange(m_mobileCache1);
public static void Download_Data()
{
ViewMapTask viewMapTask = (ViewMapTask)MobileApplication.Current.Project.Tasks.GetFirstExtensionOfType(typeof(ViewMapTask));
ViewMapPage viewMapPage = viewMapTask.ViewMapPage;
MapControl mMap = viewMapTask.ViewMapPage.MapControl;
MobileCache m_mobileCache1;
m_mobileCache1 = new MobileCache(@"C:\Akhil\ArcGIS_Mobile_Projects\P_Radiation_Data_V1_B8\Radiation_Data_V1");
m_mobileCache1.Open();
if (m_mobileCache1.CacheExists)
{
m_mobileCache1.Close();
try
{
m_mobileCache1.DeleteCache();
}
catch (Exception ex)
{
MessageBox.Show("FIRST ERROR " + Convert.ToString(ex));
}
}
else
{
m_mobileCache1.Close();
}
MobileServiceConnection con = new MobileServiceConnection();
con.Url = @"http://emapappdev/arcgis/services/Test_Mobile_Services/Radiation_Data_V1/MapServer/Mobileserver";
con.CreateCache(m_mobileCache1.StoragePath); // Per ESRI sample
m_mobileCache1.Open();
MobileCacheSyncAgent agent = new MobileCacheSyncAgent(m_mobileCache1, con);
agent.MapDocumentConnection = con;
//agent.Synchronize(); // See if Synchronize works instead of line below
agent.DownloadExtent(m_mobileCache1.GetExtent(), 0, 0);
try
{
mMap.Map.MapLayers.AddRange(m_mobileCache1);
}
catch (Exception ex)
{
MessageBox.Show("Second ERROR " + ex.ToString());
}
}
1) The first problem is the first try and catch section, the catch is always triggered because the cache is in use. The error is "cache is in use by another process", fortunately aslong as a messagebox appears the code will continue, and works as expected.
I modified your first try catch. I notice that I don't actually need to open and close a cache to delete it.
2) If I comment out the first catch messagebox, the second try and catch section is triggered. The error states "object reference is invalid". As long as we can solve the first problem then hopefully the second try and catch will work without problems.
See code below, I think you added an extra reference to the Map that I do not see in ESRI samples.public static void Download_Data() { ViewMapTask viewMapTask = (ViewMapTask)MobileApplication.Current.Project.Tasks.GetFirstExtensionOfType(typeof(ViewMapTask)); ViewMapPage viewMapPage = viewMapTask.ViewMapPage; MapControl mMap = viewMapTask.ViewMapPage.MapControl; MobileCache m_mobileCache1; m_mobileCache1 = new MobileCache(@"C:\Akhil\ArcGIS_Mobile_Projects\P_Radiation_Data_V1_B8\Radiation_Data_V1"); if (m_mobileCache1.CacheExists) { try { m_mobileCache1.DeleteCache(); } catch (Exception ex) { MessageBox.Show("FIRST ERROR " + Convert.ToString(ex)); } } MobileServiceConnection con = new MobileServiceConnection(); con.Url = @"http://emapappdev/arcgis/services/Test_Mobile_Services/Radiation_Data_V1/MapServer/Mobileserver"; con.CreateCache(m_mobileCache1.StoragePath); // Per ESRI sample m_mobileCache1.Open(); MobileCacheSyncAgent agent = new MobileCacheSyncAgent(m_mobileCache1, con); agent.MapDocumentConnection = con; //agent.Synchronize(); // See if Synchronize works instead of line below agent.DownloadExtent(m_mobileCache1.GetExtent(), 0, 0); try { // mMap.Map.MapLayers.AddRange(m_mobileCache1); This line should be as follows: mMap.MapLayers.AddRange(m_mobileCache1); } catch (Exception ex) { MessageBox.Show("Second ERROR " + ex.ToString()); } }
public static void Download_DataAkhil()
{
ViewMapTask viewMapTask = (ViewMapTask)MobileApplication.Current.Project.Tasks.GetFirstExtensionOfType(typeof(ViewMapTask));
ViewMapPage viewMapPage = viewMapTask.ViewMapPage;
MapControl mMap = viewMapTask.ViewMapPage.MapControl;
ESRI.ArcGIS.Mobile.WPF.Map MyMap = mMap.Map;
MobileCache m_mobileCache1;
m_mobileCache1 = new MobileCache(@"C:\Akhil\ArcGIS_Mobile_Projects\P_Radiation_Data_V1_B8\Radiation_Data_V1");
if (m_mobileCache1.CacheExists)
{
try
{
m_mobileCache1.DeleteCache();
}
catch (Exception ex)
{
MessageBox.Show(Convert.ToString(ex)); //Still gives error
}
}
else
{
m_mobileCache1.Close();
}
MobileServiceConnection con = new MobileServiceConnection();
con.Url = @"http://emapappdev/arcgis/services/Test_Mobile_Services/Radiation_Data_V1/MapServer/Mobileserver";
con.CreateCache(m_mobileCache1.StoragePath); // Per ESRI sample
m_mobileCache1.Open();
MobileCacheSyncAgent agent = new MobileCacheSyncAgent(m_mobileCache1, con);
agent.MapDocumentConnection = con;
//agent.Synchronize(); // See if Synchronize works instead of line below
agent.DownloadExtent(m_mobileCache1.GetExtent(), 0, 0);
try
{
//mMap.Map.MapLayers.AddRange(m_mobileCache1);
MyMap.MapLayers.AddRange(m_mobileCache1); //Gives error now, stating null reference
}
catch (Exception ex)
{
MessageBox.Show("This is second ERROR " + ex.ToString());
}
}public static void Download_DataAkhil()
{
ESRI.ArcGIS.Mobile.WPF.Map nMap = MobileApplication.Current.Map;
//ViewMapTask viewMapTask = (ViewMapTask)MobileApplication.Current.Project.Tasks.GetFirstExtensionOfType(typeof(ViewMapTask));
//ViewMapPage viewMapPage = viewMapTask.ViewMapPage;
//MapControl mMap = viewMapTask.ViewMapPage.MapControl;
//ESRI.ArcGIS.Mobile.WPF.Map MyMap = mMap.Map;
MobileCache m_mobileCache1;
m_mobileCache1 = new MobileCache(@"C:\Akhil\ArcGIS_Mobile_Projects\P_Radiation_Data_V1_B8\Radiation_Data_V1");
//if (m_mobileCache1.CacheExists) //not needed
// m_mobileCache1.Close(); //not needed
MobileServiceConnection con = new MobileServiceConnection();
con.Url = @"http://emapappdev/arcgis/services/Test_Mobile_Services/Radiation_Data_V1/MapServer/Mobileserver";
con.CreateCache(m_mobileCache1.StoragePath); // Per ESRI sample
m_mobileCache1.Open();
MobileCacheSyncAgent agent = new MobileCacheSyncAgent(m_mobileCache1, con);
agent.MapDocumentConnection = con;
agent.Synchronize(); // See if Synchronize works instead of line below
//agent.DownloadExtent(m_mobileCache1.GetExtent(), 0, 0);
nMap.MapLayers.AddRange(m_mobileCache1);
}public static void Download_DataAkhil() { ESRI.ArcGIS.Mobile.WPF.Map nMap = MobileApplication.Current.Map; //ViewMapTask viewMapTask = (ViewMapTask)MobileApplication.Current.Project.Tasks.GetFirstExtensionOfType(typeof(ViewMapTask)); //ViewMapPage viewMapPage = viewMapTask.ViewMapPage; //MapControl mMap = viewMapTask.ViewMapPage.MapControl; //ESRI.ArcGIS.Mobile.WPF.Map MyMap = mMap.Map; MobileCache m_mobileCache1; m_mobileCache1 = new MobileCache(@"C:\Akhil\ArcGIS_Mobile_Projects\P_Radiation_Data_V1_B8\Radiation_Data_V1"); //if (m_mobileCache1.CacheExists) //not needed // m_mobileCache1.Close(); //not needed MobileServiceConnection con = new MobileServiceConnection(); con.Url = @"http://emapappdev/arcgis/services/Test_Mobile_Services/Radiation_Data_V1/MapServer/Mobileserver"; con.CreateCache(m_mobileCache1.StoragePath); // Per ESRI sample m_mobileCache1.Open(); MobileCacheSyncAgent agent = new MobileCacheSyncAgent(m_mobileCache1, con); agent.MapDocumentConnection = con; agent.Synchronize(); // See if Synchronize works instead of line below //agent.DownloadExtent(m_mobileCache1.GetExtent(), 0, 0); nMap.MapLayers.AddRange(m_mobileCache1); }
Thanks Buddha and Angel for helping me out, now everyone can enjoy this. 🙂
Akhil P.
public static void Download_DataAkhil2()
{
try
{
ESRI.ArcGIS.Mobile.WPF.Map nMap = MobileApplication.Current.Map;
MobileCache m_mobileCache1;
m_mobileCache1 = new MobileCache(@"C:\Akhil\ArcGIS_Mobile_Projects\P_Radiation_Data_V1_B8\Radiation_Data_V1");
MobileServiceConnection con = new MobileServiceConnection();
con.Url = @"http://emapappdev/arcgis/services/Test_Mobile_Services/Radiation_Data_V1/MapServer/Mobileserver";
con.CreateCache(m_mobileCache1.StoragePath); // Per ESRI sample
m_mobileCache1.Open();
MobileCacheSyncAgent agent = new MobileCacheSyncAgent(m_mobileCache1, con);
agent.MapDocumentConnection = con;
agent.Synchronize(); // See if Synchronize works instead of line below
//agent.DownloadExtent(m_mobileCache1.GetExtent(), 0, 0);
nMap.MapLayers.AddRange(m_mobileCache1);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
using System.Net.NetworkInformation;
using System.Text;
Private bool PingGISServer()
{
try
{
Ping pingSender = new Ping();
PingOptions options = new PingOptions();
options.DontFragment = true;
// Create a buffer of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 120;
//First check if Intranetserver is available
PingReply reply = pingSender.Send("severNameHere", timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
return false;
}
if (PingGISServer())
{
If true do something like upload & download data
}
using System.Net.NetworkInformation;
public bool isNetWorkAvailable = NetworkInterface.GetIsNetworkAvailable();
if(isNetWorkAvailable)
{
If true do something
}...
nMap.MapLayers.AddRange(mobileCache); //refreshes the map, should use Map.Refresh or Map.Invalidate instead
public static bool PingGISServer()
{
try
{
Ping pingSender = new Ping();
PingOptions options = new PingOptions();
options.DontFragment = true;
// Create a buffer of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 2000;
//First check if Intranetserver is available
PingReply reply = pingSender.Send("emapappdev", timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
return false;
}
}
public static void Download_DataAkhil2()
{
if (PingGISServer() == true)
{
try
{
ESRI.ArcGIS.Mobile.WPF.Map nMap = MobileApplication.Current.Map; // instance of the Map (does not really work, but does work)
string appName = MobileApplication.Current.ApplicationTitle; //gets the name of the application title to be used in cache storage path
appName = appName.Remove(0, 16); //removes the first 16 characters from the application title
MobileCache mobileCache = new MobileCache(@"C:\NEPRD\Mobile Radiation Applications\P_" + appName + "\\Radiation_Data_V1");
MobileServiceConnection con = new MobileServiceConnection(); //connection to mobile service
con.Url = @"http://emapappdev/arcgis/services/Test_Mobile_Services/Radiation_Data_V1/MapServer/Mobileserver";
con.CreateCache(mobileCache.StoragePath); // creates the cache in the storage path
mobileCache.Open();
MobileCacheSyncAgent agent = new MobileCacheSyncAgent(mobileCache, con); //create an agent to the mobile cache and service
agent.MapDocumentConnection = con; //sets the map document connection
agent.Synchronize(); //agent.DownloadExtent(m_mobileCache1.GetExtent(), 0, 0); - alternative for downloading and synchronizing
nMap.MapLayers.AddRange(mobileCache); //refreshes the map, should use Map.Refresh or Map.Invalidate instead
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
//else
//{
// MessageBox.Show("Ping Failed!");
//}
}