Unofficial Programming Thread

Post » Thu Sep 23, 2010 5:02 am

So I couldn't find the last version of this and the one I did find was locked so I figured I would start a new one. Apologies if one existed but I searched repeatedly and could not find it.

I am going to be working on a small toolkit project in C# and I have run into a couple problems. First, do I use WinForms or WPF. Now I do not know WPF at all but the book I do have for C# works with it in the later chapters. I won't be doing anything too fancy with the interface anyways so it might be overkill. Second, I am unsure how to set Windows services to Manual or Disabled startup time. I tried using Google but found many different ways all of which were only small code segments I did not understand. I can start and stop them ok but they will just restart later.

Some things I want to implement in this program:
- running chkdsk
- running Windows defrag
- emptying recycle bin (I know how to do this)
- disable startup items
- kill procresses
- disable Windows services
- install some programs

I know how to launch programs and send command line stuff for chkdsk so thats not too bad. The startup items I am looking into same with processes but I could just run Systernals tools. I won't be redistributign this program just for personal use. But if anyone here wants to help that would be great as well.
User avatar
Sheila Reyes
 
Posts: 3386
Joined: Thu Dec 28, 2006 7:40 am

Post » Thu Sep 23, 2010 7:36 am

For new work, I'd learn and use WPF. I'd fall back to WinForms if I had to have it done in a hurry without climbing the WPF learning curve, or if I had reusable or open-source components in WinForms that I could use.

An installable service contains an Installer (if you select Add Installer, it'll create an Installer subclass for you). Set the installer's Startup Type property as desired. The installutil program that actually installs the service will use that property. That gives you control over the startup type of a service you write.

To change the startup type of another service, you have to use WMI. Microsoft intentionally refused to provide control over the startup type through ServiceController. The Win32_Service class in WMI has the ChangeStartMode method, which will do what you want.
User avatar
carla
 
Posts: 3345
Joined: Wed Aug 23, 2006 8:36 am

Post » Thu Sep 23, 2010 3:13 am

"- disable startup items" Might just be a registry setting you have to edit, google might have more answers. I remember seeing some code for registry in c++, it was very, very, messy.

"- kill procresses" Not too hard I dont think, I think i've read process.Kill() will do it, but im not sure if you need to have created the processes or no. I'm gonna look this one up, kinda interested.
User avatar
Ria dell
 
Posts: 3430
Joined: Sun Jun 25, 2006 4:03 pm

Post » Thu Sep 23, 2010 5:58 am

For new work, I'd learn and use WPF. I'd fall back to WinForms if I had to have it done in a hurry without climbing the WPF learning curve, or if I had reusable or open-source components in WinForms that I could use.

An installable service contains an Installer (if you select Add Installer, it'll create an Installer subclass for you). Set the installer's StartType property as desired. The installutil program that actually installs the service will use that property.

I am looking to edit existing services. And there is no dedline for this project since its for personal use but I want it to work on as many machines as possible, that is why I am considering WinForms since I can use .NET 2.0 for maximum compatability.
User avatar
suniti
 
Posts: 3176
Joined: Mon Sep 25, 2006 4:22 pm

Post » Thu Sep 23, 2010 4:55 am

System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcessesByName("notepad");myProcesses[0].Kill();


Thats a working example, just tested it. Use GetProcesses(); for a full list of running processes, then you could close whichever one.
User avatar
Beulah Bell
 
Posts: 3372
Joined: Thu Nov 23, 2006 7:08 pm

Post » Thu Sep 23, 2010 2:07 pm

I am looking to edit existing services. And there is no dedline for this project since its for personal use but I want it to work on as many machines as possible, that is why I am considering WinForms since I can use .NET 2.0 for maximum compatability.


WPF is best with at least .NET 3.5 SP1, so that makes WinForms the better approach for compatibility.

For editing settings of existing services, WMI is the way to go.
User avatar
Catherine N
 
Posts: 3407
Joined: Sat Jan 27, 2007 9:58 pm

Post » Thu Sep 23, 2010 5:58 am

This is more of a math question but still relevant. I am creating bouding spheres for some 3D models and I decided to go with Ritters algorithm to calculate them. I've gotten as far as to expand the sphere if any points in the is outside the sphere. Finding the new radius is easy but the part I am to stupid to figure out is moving the center point. So how would one do that?
User avatar
maya papps
 
Posts: 3468
Joined: Mon Aug 07, 2006 3:44 pm

Post » Thu Sep 23, 2010 11:12 am

Anyone interested in making the toolkit I am working on into a community project? I am still trying to figure out which way I want to take everything. I was playign around with Expression Blend 3, which lets you create WPF interfaces, and come up with a concept I like.
User avatar
Adriana Lenzo
 
Posts: 3446
Joined: Tue Apr 03, 2007 1:32 am

Post » Thu Sep 23, 2010 2:12 am

This is more of a math question but still relevant. I am creating bouding spheres for some 3D models and I decided to go with Ritters algorithm to calculate them. I've gotten as far as to expand the sphere if any points in the is outside the sphere. Finding the new radius is easy but the part I am to stupid to figure out is moving the center point. So how would one do that?


I'm guessing you mean "bounding spheres" rather than "bouding spheres", in which case you don't actually need to calculate a sphere. Just do a distance check between the centre of the sphere and whatever point you want to check against, if it's less than the radius it's inside the sphere, otherwise it isn't. It's as simple as that.
User avatar
vanuza
 
Posts: 3522
Joined: Fri Sep 22, 2006 11:14 pm

Post » Thu Sep 23, 2010 9:42 am

I'm guessing you mean "bounding spheres" rather than "bouding spheres", in which case you don't actually need to calculate a sphere. Just do a distance check between the centre of the sphere and whatever point you want to check against, if it's less than the radius it's inside the sphere, otherwise it isn't. It's as simple as that.

Of course I meant bounding spheres. :P

I could do it your way, but I want the sphere as tight as possible which is why I choose to use Ritters algorithm in the first place.
User avatar
sexy zara
 
Posts: 3268
Joined: Wed Nov 01, 2006 7:53 am

Post » Thu Sep 23, 2010 6:06 am

Of course I meant bounding spheres. :P

I could do it your way, but I want the sphere as tight as possible which is why I choose to use Ritters algorithm in the first place.


What is it that you are actually using the sphere for anyway? It seems like a lot of effort using an algorithm to generate a sphere when you could just store a radius for a model.
User avatar
neen
 
Posts: 3517
Joined: Sun Nov 26, 2006 1:19 pm

Post » Thu Sep 23, 2010 5:28 am

What is it that you are actually using the sphere for anyway? It seems like a lot of effort using an algorithm to generate a sphere when you could just store a radius for a model.

It's a part of this assignmet I am working on for school.
User avatar
Charleigh Anderson
 
Posts: 3398
Joined: Fri Feb 02, 2007 5:17 am

Post » Thu Sep 23, 2010 1:31 pm

@kerm I haven't been able to find a free copy of Ritters Algorithm anywhere so I don't know. If we are talking about the same algorithm, Ritters algorithm also only gives an approximation. It will not give the best possible bounding sphere.

Regarding moving the centre point I would *assume* that if you increase the radius by half the distance from the current radius to the point outside the radius, you can then you should be able move the centre of the sphere by that distance towards that point

In other works given the centre C a sphere of radius R, and a point, P outside the sphere.
Set R to R plus the distance between C and P all divided by 2:
R = [R + Dist(C,P)]/2

Then translate C along CP towards P by the distance between C and P, minus R, all divided by 2
TranslatedDistance = [Dist(C,P) - R]/2
User avatar
Luna Lovegood
 
Posts: 3325
Joined: Thu Sep 14, 2006 6:45 pm

Post » Thu Sep 23, 2010 11:00 am

@kerm I haven't been able to find a free copy of Ritters Algorithm anywhere so I don't know. If we are talking about the same algorithm, Ritters algorithm also only gives an approximation. It will not give the best possible bounding sphere.

Regarding moving the centre point I would *assume* that if you increase the radius by half the distance from the current radius to the point outside the radius, you can then you should be able move the centre of the sphere by that distance towards that point

In other works given the centre C a sphere of radius R, and a point, P outside the sphere.
Set R to R plus the distance between C and P all divided by 2:
R = [R + Dist(C,P)]/2

Then move C towards P by the distance between C and P, minus R, all divided by 2
C += [Dist(C,P) - R]/2


Here's a description of Ritter's Algorithm, including C code for adjusting the center: http://tog.acm.org/resources/RTNews/html/rtnews7b.html#art4

I'm not sure either Ritter's (in the link) or your solution fully expresses the actual solution. Translating the center C along the line CP is a vector operation.

As to whether Ritter's is a good algorithm, much depends on the applicable definition of goodness. Ritter's is extremely fast. Exact solutions are not fast. Here's a review that compares some exact, near-exact, and fast algorithms: http://www.ep.liu.se/ecp/034/009/ecp083409.pdf
User avatar
Robert Bindley
 
Posts: 3474
Joined: Fri Aug 03, 2007 5:31 pm

Post » Thu Sep 23, 2010 3:37 am

I'm not sure either Ritter's (in the link) or your solution fully expresses the actual solution. Translating the center C along the line CP is a vector operation.

Yeah, I was sort of relying on the explanation to get the point across :)

Thanks for the link.
User avatar
Donatus Uwasomba
 
Posts: 3361
Joined: Sun May 27, 2007 7:22 pm

Post » Thu Sep 23, 2010 5:28 am

Here's a description of Ritter's Algorithm, including C code for adjusting the center: http://tog.acm.org/resources/RTNews/html/rtnews7b.html#art4

I'm not sure either Ritter's (in the link) or your solution fully expresses the actual solution. Translating the center C along the line CP is a vector operation.

As to whether Ritter's is a good algorithm, much depends on the applicable definition of goodness. Ritter's is extremely fast. Exact solutions are not fast. Here's a review that compares some exact, near-exact, and fast algorithms: http://www.ep.liu.se/ecp/034/009/ecp083409.pdf

To my understanding Ritter's algorithm is fast and very accurate most of the time. My teacher, who is the one who wrote that paper you linked, recommends to use either Ritter's or EPOS algorithm if one wants fast and tight spheres. :)

Anyway I've have kinda figured out how to solve this problem. Going to implement it now, might drop my code here if anyone is interested later.
User avatar
Racheal Robertson
 
Posts: 3370
Joined: Thu Aug 16, 2007 6:03 pm

Post » Thu Sep 23, 2010 5:35 am

To my understanding Ritter's algorithm is fast and very accurate most of the time. My teacher, who is the one who wrote that paper you linked, recommends to use either Ritter's or EPOS algorithm if one wants fast and tight spheres. :)

It depends on the shapes. I think on average the bounding sphere was 5% bigger than the best case. So as Dogsbody said it depends on your definition of accurate. :shrug:
User avatar
Cassie Boyle
 
Posts: 3468
Joined: Sun Nov 05, 2006 9:33 am

Post » Thu Sep 23, 2010 1:21 am

It depends on the shapes. I think on average the bounding sphere was 5% bigger than the best case. So as Dogsbody said it depends on your definition of accurate. :shrug:

5% is quite good for such fast algorithm, me thinks. :shrug:
User avatar
Dean Brown
 
Posts: 3472
Joined: Fri Aug 31, 2007 10:17 pm

Post » Thu Sep 23, 2010 3:13 pm

So I was trying to work with WPF, but I have a listbox in a tab control on my main form. But I want to edit its contents from a class. Should I return a string array to fill i or make the control public? When I was researchign it looks like public isn't the best way to go, but when I do return a string array it doesn't seem to work.
User avatar
Gemma Woods Illustration
 
Posts: 3356
Joined: Sun Jun 18, 2006 8:48 pm

Post » Thu Sep 23, 2010 11:12 am

I fail at anything but basic SQL and my google foo is letting me down, so here goes

I have a table that contains two columns (A and B ), which can contain duplicate values.

I need to select rows where value of A is one of a set of values, but the result should contain only unique values of A. When there are several values of A that are the same it should only take the value of A with the maximum value of B.

Doing it for a single value of a isn't an issue:
SELECT CFROM TableWHERE A = xORDER B DESCLIMIT 1 


But I get stuck when I need to do it for multiple possible values of A. Is anyone able to point me in the right direction?
User avatar
Juliet
 
Posts: 3440
Joined: Fri Jun 23, 2006 12:49 pm

Post » Thu Sep 23, 2010 11:42 am

If you just want unique values from a set of rows, you can use either DISTINCT or UNIQUE keyword. It might depend on which database you have - Oracle, MS, etc. Or, actually if you want to find max values:

Maybe something like this, using a subquery. I don't have Oracle or any other database here to test this:

SELECT a FROM Table TWHERE b = (SELECT max(B) FROM Table WHERE a=T.a);

User avatar
CArla HOlbert
 
Posts: 3342
Joined: Wed Feb 21, 2007 11:35 pm

Post » Thu Sep 23, 2010 2:10 pm

@Chaul

The subquery works great, many thanks. I wasn't awear you could do things like that :blush:
User avatar
OnlyDumazzapplyhere
 
Posts: 3445
Joined: Wed Jan 24, 2007 12:43 am

Post » Thu Sep 23, 2010 7:38 am

Last night I was working with getting disk space from all drives on a system and came up with the following:
using System;using System.Collections.Generic;using System.Management;namespace conDiskPercent{    class Program    {        static void Main(string[] args)        {            Disk d = new Disk();            Console.WriteLine("Drive{0}{0}Total Size{0}Free Space", '\t');            d.GetDisks();            Console.ReadLine();        }    }    public class Disk    {        public void GetDisks()        {            string[] Disks = Environment.GetLogicalDrives();            for (int i = 0; i < Disks.Length; i++)            {                double tByteSize;                double fByteSize;                decimal tSize;                decimal fSize;                ManagementObject d = new ManagementObject("Win32_LogicalDisk.DeviceID=\"" + Disks[i].Substring(0, 1) + ":\"");                d.Get();                tByteSize = Convert.ToDouble(d["Size"]);                fByteSize = Convert.ToDouble(d["FreeSpace"]);                if (tByteSize >= 1073741824)                {                    tSize = (decimal)(tByteSize / 1073741824.0);                    if (tSize > 0)                    {                        fSize = (decimal)(fByteSize / 1073741824.0);                        Console.WriteLine("{0}{2}{2}{1}gb{2}{3}gb", Disks[i], tSize.ToString("#.##"), '\t', fSize.ToString("#.##"));                    }                }            }        }    }}


My output is messy I realize so I am looking to clean it up. What I really want to do is try and set up properties for the drive letters, total space, and free space so I don't need to put my output in the method that gets the information, but I am not sure how to do this. I got total space and free space to work (not in this version of the code) but the drive letter I couldn't. On my system it should be C:, D:, and H: but when I tried it would only display H:.

Any ideas? I am using C#, right nwo the code is in .NET 2.0 so maybe there is a method or better way in 3.0/3.5 which I am going to be switching too.
User avatar
Shaylee Shaw
 
Posts: 3457
Joined: Wed Feb 21, 2007 8:55 pm

Post » Thu Sep 23, 2010 6:10 am

Can you debug that code line by line?

I'm not a C# coder, but I don't understand why are you testing if (tByteSize >= 1073741824). If it's less, it's never going to the Console.WriteLine.
User avatar
Jarrett Willis
 
Posts: 3409
Joined: Thu Jul 19, 2007 6:01 pm

Post » Thu Sep 23, 2010 3:24 pm

Can you debug that code line by line?

I'm not a C# coder, but I don't understand why are you testing if (tByteSize >= 1073741824). If it's less, it's never going to the Console.WriteLine.

Realized I hadn't changed my total sizes to floats so I corrected and commented the code:
using System;using System.Collections.Generic; //used for string arrayusing System.Management; //used to get disk information via WMInamespace conDiskPercent{    class Program    {        static void Main(string[] args)        {            Disk d = new Disk(); //create an instance of the disk class            Console.WriteLine("Drive{0}{0}Total Size{0}Free Space", '\t'); //write table heading            d.GetDisks();             Console.ReadLine();        }    }    public class Disk    {        public void GetDisks() //this method gets all the disks in the current system and displays those with a valid size        {            string[] Disks = Environment.GetLogicalDrives(); //array for ALL logical drives in the system            for (int i = 0; i < Disks.Length; i++) //loops to get information for each drive            {                double tByteSize; //total disk size in bytes                double fByteSize; //free disk size in bytes                float tSize; //total space converted                float fSize; //free space converted                //WMI instace for each disk                //drive letter is separated otherwise an invalid parameter exception occurs when the :\ is removed                ManagementObject d = new ManagementObject("Win32_LogicalDisk.DeviceID=\"" + Disks[i].Substring(0, 1) + ":\"");                d.Get(); //get all disk information                //convert each size to the respective variable                tByteSize = Convert.ToDouble(d["Size"]);                fByteSize = Convert.ToDouble(d["FreeSpace"]);                //verify the size can be converted to gigabytes                if (tByteSize >= 1073741824)                {                    tSize = (float)(tByteSize / 1073741824.0); //convert total space from bytes to gigabytes                    if (tSize > 0)                    {                        fSize = (float)(fByteSize / 1073741824.0); //convert free space from bytes to gigabytes                        //output all drive letters and sizes (in gigabytes rounded to 2 decimal places)                        Console.WriteLine("{0}{2}{2}{1}gb{2}{3}gb", Disks[i], tSize.ToString("#.##"), '\t', fSize.ToString("#.##"));                    }                }            }        }    }}

User avatar
Mark Churchman
 
Posts: 3363
Joined: Sun Aug 05, 2007 5:58 am

Next

Return to Othor Games