PDA

View Full Version : MapPoint 2011 in C# Windows Application Not Responding when address to locate on the map is more.



prasadvp
10-03-2011, 01:57 PM
Hello all,
I have developed a Windows Forms application in C# with MapPoint 2011 and it was working fine to locate and populate pushpins for address locations retrieved from an SQL Server database based on certain conditions. But now when the data set (address locations from database) got larger and the MapPoint is opening and running and not responding after a while. Then I have to close the program from Windows End Program. If address locations are less its still working fine.
Please help with any solutions/or suggestions to this performance issue.

Regards,
Prasad

Eric Frost
10-03-2011, 02:02 PM
I would check your code for basic memory/object cleanup to make sure you're not doing bad... if you go into the Task manager and look at processes, and sort by memory descending while your program is running, does something keep getting bigger and bigger? If so, I would first check to see if there's something in your code that is causing memory usage to increase. If you wish, you may paste your code in here if you want us to look at it.

Eric

prasadvp
10-03-2011, 04:17 PM
Hello,

Posting my Button click Code which is supposed to load the MapPoint 2011 application and populate the locations with pushpins. If amount of records retreived by the query is large, MapPoint is not responding. Please take a look and help in resolving this issue. Thank you very much.

Regards,
Prasad.

privatevoid button3_Click(object sender, EventArgs e)
{
string refChildAddress;
ArrayList allChildLocations = newArrayList();
try
{
if (MApp == null)
{
MApp = new MapPoint.Application();
bIsRunning = true;
MApp.BeforeClose += new MapPoint._ApplicationEvents_BeforeCloseEventHandle r(MapPoint_BeforeClose);
MMap = MApp.ActiveMap;
MApp.Visible = true;
MApp.UserControl = true;
MApp.WindowState = MapPoint.GeoWindowState.geoWindowStateNormal;
MDataSet = MMap.DataSets.AddPushpinSet("Child Locations");
}
if (cboChildName.Text != String.Empty)
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
if (dgView1.RowCount > 1)
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
SqlCommand command2 = newSqlCommand("spGetServiceType_Child_Therapist", conn);
command2.CommandType = CommandType.StoredProcedure;
command2.Parameters.Add("@ServiceTypeCodes", SqlDbType.VarChar).Value = strServiceTypeCodes;
command2.Parameters.Add("@LanguageCodes", SqlDbType.VarChar).Value = strLangCodes;
command2.Parameters.Add("@CountyCodes", SqlDbType.VarChar).Value = strCountyCodes;
command2.Parameters.Add("@CityCodes", SqlDbType.VarChar).Value = strCityCodes;
command2.Parameters.Add("@ZipCodes", SqlDbType.VarChar).Value = strZipCodes;
command2.Parameters.Add("@PaySourceProgCodes", SqlDbType.VarChar).Value = strPaySourceProgCodes;
command2.Parameters.Add("@PaySourceCodes", SqlDbType.VarChar).Value = strPaySourceCodes;
SqlDataReader sqlReader2 = command2.ExecuteReader(System.Data.CommandBehavior .CloseConnection);
if (sqlReader2 != null)
{
while (sqlReader2.Read())
{
String qryChildAddress = sqlReader2["Address"] + ", " + sqlReader2["City"] + ", " + sqlReader2["State"] + ", " + sqlReader2["Zip"];
try
{
MapPoint.Location childLocation = null;
MapPoint.FindResults findChildResults = MMap.FindResults(qryChildAddress);
if (findChildResults == null || findChildResults.Count <= 0)
thrownewException(qryChildAddress);
string rq = Convert.ToString(findChildResults.ResultsQuality);
if (rq == "geoFirstResultGood" && findChildResults.Count == 1)
{
childLocation = findChildResults.get_Item(1) as MapPoint.Location;
string strNL = System.Environment.NewLine;
if (childLocation != null)
{
pushpin = MMap.AddPushpin(childLocation);
pushpin.Note = "Name-" + sqlReader2["Child Name"] + strNL + "Service Type-" + sqlReader2["Service Type"] + strNL + "Therapist-" + sqlReader2["Therapist Name"] + strNL + "Payment Method-" + sqlReader2["Payment Method"] + strNL + "Employee Type-" + sqlReader2["Therapist Employee Type"] + strNL + "Child Dom. Lang.-" + sqlReader2["Child Dominant Language"] + strNL + "Child County-" + sqlReader2["Child County"] + strNL + "Child Address-" + sqlReader2["Address"] + strNL + "Child City-" + sqlReader2["City"] + strNL + "Child State-" + sqlReader2["State"] + strNL + "Child Zip-" + sqlReader2["Zip"] + strNL + "Pay Source Prog. Code-" + sqlReader2["Pay Source Program Code"] + strNL + "Pay Source Code-" + sqlReader2["Pay Source Description"];
pushpin.MoveTo(MDataSet);
//pushpin.BalloonState = MapPoint.GeoBalloonState.geoDisplayName;
PushPinColors(sqlReader2["Service Type"].ToString());
//pushpin.Symbol = 270;
//childLocation.GoTo();
//MMap.Altitude = 25;
allChildLocations.Add(childLocation);
}
}
}
catch
{
if (sqlReader2.IsClosed == false)
sqlReader2.Close();
goto NoRecords;
}
}
sqlReader2.Close();
if (cboChildName.Text == String.Empty)
{
if (allChildLocations.Count >= 1)
{
MapPoint.Location center = MMap.Union(allChildLocations.ToArray());
center.GoTo();
MMap.Altitude = 25;
}
}
}
}
else
{
conn.Close();
}
}
catch
{
}
finally
{
allChildLocations = null;
MMap = null;
}
NoRecords:;
}

Eric Frost
10-03-2011, 06:07 PM
Can you post it as indented code? That would probably be easier to read than this colored code.

Thanks,
Eric

prasadvp
10-03-2011, 07:10 PM
Hello Eric,
When I post my code here it is coming like this format only. Can't do indentation and other foramtting.

Regards,
Prasad.

Eric Frost
10-03-2011, 08:51 PM
Use the code tags you see in the formatting bar.

Eric

prasadvp
10-04-2011, 08:48 AM
Hello Eric,
Posting the code in code tags. When I checked with Task Manager-Processes the MapPoint.Exe is getting bigger and bigger and continuing like that. Then we have to end the process. This is happening if the number of addresses to locate in the map is more(ie; above 300 addresses to locate). Please help.

With Warm Regards,
Prasad.



privatevoid button3_Click(object sender, EventArgs e)
{
string refChildAddress;
ArrayList allChildLocations = newArrayList();
try
{
if (MApp == null)
{
MApp = new MapPoint.Application();
bIsRunning = true;
MApp.BeforeClose += new MapPoint._ApplicationEvents_BeforeCloseEventHandle r(MapPoint_BeforeClose);
MMap = MApp.ActiveMap;
MApp.Visible = true;
MApp.UserControl = true;
MApp.WindowState = MapPoint.GeoWindowState.geoWindowStateNormal;
MDataSet = MMap.DataSets.AddPushpinSet("Child Locations");
}
if (cboChildName.Text != String.Empty)
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
if (dgView1.RowCount > 1)
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
SqlCommand command2 = newSqlCommand("spGetServiceType_Child_Therapist", conn);
command2.CommandType = CommandType.StoredProcedure;
command2.Parameters.Add("@ServiceTypeCodes", SqlDbType.VarChar).Value = strServiceTypeCodes;
command2.Parameters.Add("@LanguageCodes", SqlDbType.VarChar).Value = strLangCodes;
command2.Parameters.Add("@CountyCodes", SqlDbType.VarChar).Value = strCountyCodes;
command2.Parameters.Add("@CityCodes", SqlDbType.VarChar).Value = strCityCodes;
command2.Parameters.Add("@ZipCodes", SqlDbType.VarChar).Value = strZipCodes;
command2.Parameters.Add("@PaySourceProgCodes", SqlDbType.VarChar).Value = strPaySourceProgCodes;
command2.Parameters.Add("@PaySourceCodes", SqlDbType.VarChar).Value = strPaySourceCodes;
SqlDataReader sqlReader2 = command2.ExecuteReader(System.Data.CommandBehavior .CloseConnection);
if (sqlReader2 != null)
{
while (sqlReader2.Read())
{
String qryChildAddress = sqlReader2["Address"] + ", " + sqlReader2["City"] + ", " + sqlReader2["State"] + ", " + sqlReader2["Zip"];
try
{
MapPoint.Location childLocation = null;
MapPoint.FindResults findChildResults = MMap.FindResults(qryChildAddress);
if (findChildResults == null || findChildResults.Count <= 0)
thrownewException(qryChildAddress);
string rq = Convert.ToString(findChildResults.ResultsQuality);
if (rq == "geoFirstResultGood" && findChildResults.Count == 1)
{
childLocation = findChildResults.get_Item(1) as MapPoint.Location;
string strNL = System.Environment.NewLine;
if (childLocation != null)
{
pushpin = MMap.AddPushpin(childLocation);
pushpin.Note = "Name-" + sqlReader2["Child Name"] + strNL + "Service Type-" + sqlReader2["Service Type"] + strNL + "Therapist-" + sqlReader2["Therapist Name"] + strNL + "Payment Method-" + sqlReader2["Payment Method"] + strNL + "Employee Type-" + sqlReader2["Therapist Employee Type"] + strNL + "Child Dom. Lang.-" + sqlReader2["Child Dominant Language"] + strNL + "Child County-" + sqlReader2["Child County"] + strNL + "Child Address-" + sqlReader2["Address"] + strNL + "Child City-" + sqlReader2["City"] + strNL + "Child State-" + sqlReader2["State"] + strNL + "Child Zip-" + sqlReader2["Zip"] + strNL + "Pay Source Prog. Code-" + sqlReader2["Pay Source Program Code"] + strNL + "Pay Source Code-" + sqlReader2["Pay Source Description"];
pushpin.MoveTo(MDataSet);
//pushpin.BalloonState = MapPoint.GeoBalloonState.geoDisplayName;
PushPinColors(sqlReader2["Service Type"].ToString());
//pushpin.Symbol = 270;
//childLocation.GoTo();
//MMap.Altitude = 25;
allChildLocations.Add(childLocation);
}
}
}
catch
{
if (sqlReader2.IsClosed == false)
sqlReader2.Close();
goto NoRecords;
}
}
sqlReader2.Close();
if (cboChildName.Text == String.Empty)
{
if (allChildLocations.Count >= 1)
{
MapPoint.Location center = MMap.Union(allChildLocations.ToArray());
center.GoTo();
MMap.Altitude = 25;
}
}
}
}
else
{
conn.Close();
}
}
catch
{
}
finally
{
allChildLocations = null;
MMap = null;
}
NoRecords:;
}

Winwaed
10-04-2011, 09:36 AM
It is pretty well known behavior with poor memory management. You can improve thingsd further though. For example, set "center" to null, and call GC.Collect() after a hundred or so points.

Eric Frost
10-04-2011, 09:44 AM
I don't see any glaring problems, but then I'm not the expert C# MapPoint programmer :-) Maybe someone else sees a potential issue causing memory usage to grow.

How many records do you get to before it starts to fail?

I guess one thing you might do is periodically Save a PTM file, close the MapPoint application, wait a few seconds, and then re-open the PTM file and continue adding Pushpins.

Eric

prasadvp
10-04-2011, 01:42 PM
Hello,
When the records to locate on the MapPoint approaches 300 and above its failing. With records below 300 even though the memory usage is increasing while looping through each address location, it works.

The requirement is to locate all the addresses fetched from the database table. The MapPoint Locations are appeneded to an array of locations and finally to show the map to a relatively central point of all the locations.

Regards,
Prasad.

Eric Frost
10-04-2011, 03:20 PM
Are you looking for a developer to help you meet the requirements?

Eric

prasadvp
10-04-2011, 03:50 PM
Hello,
Not really as I am developing this application. Need help in solving this issue related to memory. As Winwaed mentioned about GC.Collect() please shed some light into where to put that in this code and so. Didn't used the GC class before.
Also the code is not reaching the place where the center is assigned variable, before itself the memory usage is increasing and application not responding. I am declaring an ArrayList and adding each Locations located in the Map into it before displaying the entire set of Locations.

Also I am using the MapPoint.Application() class not the MapPoint ActiveX control. Please help.

Regards,
Prasad.

Eric Frost
10-04-2011, 09:35 PM
If other things don't work, I'd still recommend saving the PTM, killing the MapPoint instance, re-opening, and then continue.

Eric

Winwaed
10-05-2011, 07:43 AM
Setting references to null in .NET will clear the references. When all references to an object have been cleared, the garbage collector will deem the object unused and mark it for deletion. This can happen at any time. Calling GC.Collect() forces garbage collection, but it can also slow things down, so you don't want to do it on every iteration. So at the end of an iteration loop after you clear all your references, try calling it, say, every 100 iterations.


Richard