|
First, improve your readability of variables:
List<string> List1 = new List<string>();
List<string> List2 = new List<string>();
List<string> List3 = new List<string>();
List<string> moviesList = new List<string>();
List<string> savesList = new List<string>();
List<string> tempList = new List<string>();
Also, there are FORs that you could change to foreach.
Please, drop the GOTO. Although it has its uses, the same can be achieved with loop controls and it can avoid confusion.
There are no secrets to success. It is the result of preparation, hard work, and learning from failure. Colin Powell
|
|
|
|
|
Okay, I see many areas that can be improved. Here are a few enhancements that you might want to consider as they should make your life easier (and I'm not talking just about renaming your lists).
First of all, you are mixing a lot of concerns in your code. That's a fancy way of saying that your class is trying to do too much. You should simplify and specialise so that you create classes that are responsible only for their own functionality. That makes testing and fixing a lot easier.
You are relying on goto statements - I'm not going to go into a religious gotos are evil argument here as they sometimes do have their place, but in your case they are obscuring the logic.
You can simplify your GetFiles when you are retrieving multiple extensions using a simple LINQ trick:
IEnumerable<string> files = extensionsForMovies.SelectMany(filter =>
Directory.GetFiles(currentFolder, filter, SearchOption.TopDirectoryOnly));
|
|
|
|
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Cv;
using CxCore;
using OtherLibs;
namespace sampleWebcam
{
public partial class Form1 : Form
{
private CvCapture VideoCapture;
private IplImage frame;
private IplImage imgMain;
public Form1()
{
InitializeComponent();
}
private void btnVideo_Click(object sender, EventArgs e)
{
double vidWidth, vidHeight;
if (btnVideo.Text.Equals("Start Video"))
{
VideoCapture = highgui.CvCreateCameraCapture(0);
if (VideoCapture.ptr == IntPtr.Zero)
{
MessageBox.Show("badtrip ah!!!");
return;
}
btnVideo.Text = "Stop Video";
highgui.CvSetCaptureProperty(ref VideoCapture, highgui.CV_CAP_PROP_FRAME_WIDTH, 640);
highgui.CvSetCaptureProperty(ref VideoCapture, highgui.CV_CAP_PROP_FRAME_HEIGHT, 320);
highgui.CvQueryFrame(ref VideoCapture);
vidWidth = highgui.cvGetCaptureProperty(VideoCapture, highgui.CV_CAP_PROP_FRAME_WIDTH);
vidHeight = highgui.cvGetCaptureProperty(VideoCapture, highgui.CV_CAP_PROP_FRAME_HEIGHT);
picBoxMain.Width = (int)vidWidth;
picBoxMain.Height = (int)vidHeight;
timerGrab.Interval = 42;
timerGrab.Enabled = true;
}
else
{
btnVideo.Text = "Start Video";
timerGrab.Enabled = false;
if (VideoCapture.ptr == IntPtr.Zero)
{
highgui.CvReleaseCapture(ref VideoCapture);
VideoCapture.ptr = IntPtr.Zero;
}
}
}
private void timerGrab_Tick(object sender, EventArgs e)
{
frame = highgui.CvQueryFrame(ref VideoCapture);
if (frame.ptr == IntPtr.Zero)
{
timerGrab.Stop();
MessageBox.Show("Wrong");
return;
}
imgMain = cxcore.CvCreateImage(cxcore.CvGetSize(ref frame), 8, 3);
cxcore.CvCopy(ref frame, ref imgMain);
cxcore.CvFlip(ref imgMain, 0);
picBoxMain.Image = highgui.ToBitmap(imgMain, false);
cxcore.CvReleaseImage(ref imgMain);
}
}
}
i have this code. but when i run this. the screen when running is always black. it does not view any picture but always black screen.
i need your help masters. i dont know what to do. (
|
|
|
|
|
Don't repost the same question just to bump it up. What you should do is step through the code in the debugger and determine where it is not giving the results you expect.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
There are a number of threads on StackOverFlow in which people disagree about the possibility of truly sorting a "standard" .NET Generic Dictionary: i.e., not an instance of the .NET SortedDictionay. Given that a .NET Dictionary is implemented with the use of Hashes for fast look-up, those who believe it cannot be ordered describe that, and the statements by Microsoft about "no guarantee" of a specific order, as the reasons sorting is not possible.
In this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace Nov2_CustomShape
{
public class RandomizedDictionary
{
private Dictionary<int, int> randomDictionary;
public Dictionary<int, int> sortByKeyAsc;
public Dictionary<int, int> sortByKeyDesc;
public Dictionary<int, int> sortByValueAsc;
public Dictionary<int, int> sortByValueDesc;
private Random rand1, rand2;
private int i, j;
public RandomizedDictionary(int nKeyValuePairs)
{
randomDictionary = new Dictionary<int, int>();
rand1 = new Random((int)DateTime.Now.Ticks);
Thread.Sleep(100);
rand2 = new Random((int)DateTime.Now.Ticks);
while (randomDictionary.Count < nKeyValuePairs)
{
i = rand1.Next(0, nKeyValuePairs);
j = rand2.Next(0, nKeyValuePairs);
if(randomDictionary.Keys.Contains(i) || randomDictionary.Values.Contains(j)) continue;
randomDictionary.Add(i,j);
}
sortByKeyAsc = randomDictionary.OrderBy(x => x.Key).ToDictionary(x => x.Key, x => x.Value);
sortByKeyDesc = randomDictionary.OrderByDescending(x => x.Key).ToDictionary(x => x.Key, x => x.Value);
sortByValueAsc = randomDictionary.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
sortByValueDesc = randomDictionary.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
}
}
} A Dictionary with a specified number of Key-Value Pairs is created where each Key is taken at random from the range #0 to the number of Key-ValuePairs minus one, and the Values are also randomly assigned from the same range: no duplicates are allowed for the Values.'
If you test this:
RandomizedDictionary testDictionarySort = new RandomizedDictionary(1000); and examine the four sorted Dictionaries exposed by the Class, they appear (so far, in my tests) to be sorted as expected.
I'm curious to know if you agree with the opinion of those that believe you cannot rely on a Generic Dictionary, sorted via Linq, to be, in fact, sorted.
Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview
|
|
|
|
|
The current implementation of Dictionary keeps the order of insertion as long as nothing is ever removed. That fits this situation: the dictionaries are filled in the correct order, and that's it. No removals.
It's perfectly possible (though perhaps not 100% safe) to rely on it. I guess what it comes down to is, are you willing to take the risk?
|
|
|
|
|
Thanks for your response, Harold; what you say seems absolutely correct to me.
bill
Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview
|
|
|
|
|
I do not see an issue with you using LINQ OrderedBy to get a sorted copy of a Dictionary.
The OrderedBy method returns: Type: System.Linq.IOrderedEnumerable(Of TSource)
An IOrderedEnumerable(Of TElement) whose elements are sorted according to a key. In the case of a Dictionary, TElement is a KeyValuePair. This is more apparent when calling the static Enumerable.OrderBy method.
System.Linq.IOrderedEnumerable<KeyValuePair<Int32, Int32>> ioe = System.Linq.Enumerable.OrderBy<KeyValuePair<Int32,Int32>, Int32>
(randomDictionary, (KeyValuePair<Int32, Int32> x) => x.Key); Bill said their argument is: Given that a .NET Dictionary is implemented with the use of Hashes for fast look-up, those who believe it cannot be ordered describe that, and the statements by Microsoft about "no guarantee" of a specific order, as the reasons sorting is not possible To this I say crawl into Reflector and see how that nasty hash is used in the Enumerator. All it is used for there is as way to tell if the Dictionary's "entries" array at the current index holds a value.
public bool MoveNext()
{
if (this.version != this.dictionary.version)
{
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
}
while (this.index < this.dictionary.count)
{
if (this.dictionary.entries[this.index].hashCode >= 0)
{
this.current = new KeyValuePair<TKey, TValue>(this.dictionary.entries[this.index].key, this.dictionary.entries[this.index].value);
this.index++;
return true;
}
this.index++;
}
this.index = this.dictionary.count + 1;
this.current = new KeyValuePair<TKey, TValue>();
return false;
}
About this statement from the documentation being a concern:For purposes of enumeration, each item in the dictionary is treated as a KeyValuePair(Of TKey, TValue) structure representing a value and its key. The order in which the items are returned is undefined. To this, I say so what, if the order of returned KVP's is undefined. It doesn't matter what the returned order is when copying them to another class that will present them in sorted order.
Well, so much for my analysis. I could be full of it on this , but I don't think so.
|
|
|
|
|
Thanks for that detailed technical analysis, TnTinMan !
I assume that all Linq methods are static methods that require an IEnumerable to work. Perhaps I am not fully educated ?
The distinction between calling the "raw" method, and calling the method "off" IEnumerable, as you show, is not clear to me, but I will study your post carefully.
bill
Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview
|
|
|
|
|
Quote: I assume that all Linq methods are static methods that require an IEnumerable to work. Perhaps I am not fully educated ? As far as I know, you are correct and I do hope that I did not imply otherwise. Quote: The distinction between calling the "raw" method, and calling the method "off" IEnumerable, as you show, is not clear to me, but I will study your post carefully.
All that I was trying to convey with that was that a new IEnumerable<KeyValuepair<Int32, Int32>>is returned by the query. If you run GetType.Fullname against it, you will get something like this:
System.Linq.OrderedEnumerable`2[[System.Collections.Generic.KeyValuePair`2[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] This returned class is created by enumerating the source (the Dictionary).
I apologize if the following is a bit basic and long winded, but I cannot think of another way to make my point. I do not intend to insult your intellegence.
It is easy to forget how a collection is enumerated especially since the "foreach statement" replaces all the grunt work for us.
Borrowing from http://msdn.microsoft.com/en-us/library/system.collections.ienumerator.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2[^].
foreach (Person p in peopleList)
Console.WriteLine(p.firstName + " " + p.lastName);
Doing it the long winded way:
IEnumerator en = peopleList.GetEnumerator();
Person p;
while (en.MoveNext())
{
p = (Person)en.Current;
Console.WriteLine(p.firstName + " " + p.lastName);
}
Each MoveNext call checks if any items remain in the collection and method Current returns the next object in the sequence.
With the classes defined as:
public class People : IEnumerable
{
private Person[] _people;
public People(Person[] pArray)
{
_people = new Person[pArray.Length];
for (int i = 0; i < pArray.Length; i++)
{
_people[i] = pArray[i];
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator)GetEnumerator();
}
public PeopleEnum GetEnumerator()
{
return new PeopleEnum(_people);
}
}
public class PeopleEnum : IEnumerator
{
public Person[] _people;
int position = -1;
public PeopleEnum(Person[] list)
{
_people = list;
}
public bool MoveNext()
{
position++;
return (position < _people.Length);
}
public void Reset()
{
position = -1;
}
object IEnumerator.Current
{
get
{
try
{
return _people[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
} The point of this all was to show that an IEnumerable is nothing more than a class that holds a collection of objects (Array or whatever) that provides a mechanism to retrieve those objects in a sequence order defined by the IEnumerator class. In the case of OrderedEnumerable this return order is supplied by a array that contains the backing array indices sorted as stipulated by the supplied IComparer.
Now in the case of Dictionary, I showed in my previous post the MoveNext method that indicates that the hash value is only used determine where or not particular backing array entry is holding a value. The hash value only figures in when trying to retrieve a value by key; a procedure that play no part in enumerating the dictionary.
I hope this helps clear up my ramblings from last night without making it any worse.
|
|
|
|
|
BillWoodruff wrote: you agree with the opinion of those that believe you cannot rely on a Generic
Dictionary, sorted via Linq, to be, in fact, sorted.
Doesn't matter if it is proven to be sorted because it is documented to be unreliable.
And that means that one would need to re-verify after every single maintenance release of .Net to insure that the underlying implementations did not change. So in the real world it isn't something that should be relied upon.
|
|
|
|
|
Hi Bill, not been here for a while.
I'd turn the argument on it's head: Dictionaries aren't meant to be ordered. Practically, it may be (insertion order/ key order? - I haven't checked), but by using the plain, generic dictionary, you are declaring an object that is unordered. Therefore "relying" on any inherent ordering is semantically inappropriate. There are generic OrderedDictionary and SortedDictionary types that might semantically fulfil what is required and not have the ambiguity "not guaranteeing" a specific order.
|
|
|
|
|
Hello,
I have question about burning open file to CD/DVD/...
My idea:
- some application (no my) create file and this file is open in this application, application sometimes write new data to file (for example: recording video from webcam - file is still open and application still has open file and add new frames to this file)
- My C# application burns this file to CD/DVD in the same time like application which saves data to file.
- File in this progress is still enable to read from another application.
My question is - Is this possible to do it? Which C# framework is good for this?
Thank you very much
|
|
|
|
|
There isn't enough information about the specifics of how this file is handled, so anything anyone says is merely speculation.
But, it doesn't sound like it's going to be possible. If the first application writing the file has it locked exclusive (no shared reads possible), you're already done with this application before you even start.
Also, writing is CD is not the same as writing to other media. You would need a "packet writing" library to do this from your code. I don't know of one as Nero's library was withdrawn years ago.
|
|
|
|
|
Thank you for your answer.
All is about video file. Some camera stores video directly into video file on hard drive in computer. I don't know video format now. I want to burn this video file in the moment when camera still storing new frames to video file.
I am thinking about IMAPI2 but I mean it is not possible with that. Maybe Nero SDK - I will find out.
Or do you have another idea? Or do you mean that it is absolutelly not possible?
|
|
|
|
|
|
Hi
I would like some assistance by the forum colleagues to assist me in providing a solution for a effective and optimised comparing of name inputted by a user which it has to compare against 800,000 names. The solution currently takes around 2 minutes for a single name to compare with 80000 names using Levenshtein function and matching accuracy % required
If there is anybody who has developed or using a solution for similar case, I would appreciate if they could assist me. My developer is not much experienced and has already taken a lot of time.
Best Regards
Jackson</pre>
|
|
|
|
|
I would make a business assumption that the first character is correct, filter you name list on that character and then apply the levenshtien calc on the sub set.
Are you doing the calc on the database or in the BL layer, C# may be faster doing a reiterative process.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hi,
The first letter could be same for 90% of the time. Since it is entered by the application user based on the name provided by the customer, the sequence of different parts in a name ( First, Middle and Last ) could be given differently. It is also possible that it is mispelled by the application user or from First, Middle and Last name the customer has provided only First and Last.
|
|
|
|
|
First step of course is to look at the requirements. Don't implement the code based on what someone thinks the users will do but rather based on what the users actually do.
Second step is to look at the design. Such as the suggestion in the other posting. Which is probably likely given that people don't randomly mix up names - they are often in the same order. There are probably many other possibilities.
Third step profile the solution - actually measure where the time is being spent in the code and then try to determine ways to make those parts faster. However when doing this use REAL test data and not just random stuff.
Note that the first two items in the above are the only way you are going to achieve something that approaches what would be reasonable.
|
|
|
|
|
Threads!
Well, first ensure that you have the best Levenshtein implementation. Your developer could post what he has in the Algorithms Forum[^].
Does the implementation calculate the full distance? Or does it stop once it exceeds the threshold?
And have a look at this old thread: Levenshtein-Distance[^].
|
|
|
|
|
You may helps me to the problems document subject applications a network is not.
Me are much to be an choices helps Prop to the problems of this document:single chat TCP, Console C#
|
|
|
|
|
Wow - I suggest you get someone to help with your english translation, that collection of words makes no sense at all.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
For a moment, I thought it was Yoda asking a question.
Then I thought it was a very drunk Yoda.
|
|
|
|
|
I am loading a panel of dynamic questions in the panel, in the Page_Init method, to avoid situation of losing the values on postback issues. There is a linq statement that loads this dynamic panel.
The same aspx page is used in 3 step registration process.
And this dynamic questionnaire is step 3 of process. I now need to limit the questions that appear on this dynamic queationnaire, by what was entered in step 1 of questionnaire, like a radiobutton choice. How can I pass this to the Page_Init page?
Thanks!!
|
|
|
|
|