This is much easier when you use the BackgroundWorker class. It has thread marshalling built in, and makes it easy to get progress feedback and to allow mid-work cancelling. http://msdn2.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx[^]
scott_hackett
Posts
-
How to interrupt a method in c#? -
char x = 0;I bet the chars got stored sequentially in memory, like this... abcd????? s then points to the 'a' memory location and cout prints "abcd" and whatever memory follows it until it hits the first 0 (what it thinks is the null terminator). s is then moved to point to the 'b' memory location and prints "bcd" and whatever memory follows it and so on and so forth. Interesting!
-
Wondering if such a book exists...You might like The Pragmatic Programmer[^]. At almost 8 years old, it's getting a little dated, but it's a great book filled information that's more timeless than how to use this-or-that API. 122 reviews @ 4.5 stars has to mean something good. :-D
-
Better efficiency?Is that a serious post... for real? I now return to The daily WTF for all of my coding freak out needs.
-
Better efficiency?It wasn't that he was or wasn't using a DataSet... he was using the same DataSet member variable for all of the queries run in the class. It's like declaring a class member variable i for all of the
for
loops that you're going to do in your class. -
Better efficiency?I once worked with a guy who was writing a lot of the DAL code. When it was time to code review some of his stuff, we saw a lot of functions that made a query string, got a DataSet back, extracted the data from the DataSet and returned the extracted data. Basic stuff. However, there were no DataSets declared locally in any of those functions. He had declared a private DataSet class member and used that in all of those functions. The reason... it was too inefficient to declare them locally when you could do it once at the top of the class! Forehead slaps broke out around the room.
-
MIDI composition softwareI use FL Studio with an M-Audio MIDI controller to make loops, then I pull the loops into ACID music studio to make the whole song. It's a great combination. FL studio definitely has a steep learning curve, but it can do amazing things and the price just can't be beat ($99 for the fruity loops edition, which is fantastic). I love to make dnb music and I'm not a musically trained person at all. I tend to do what sounds good to me and I love the stuff I've made with those two pieces of software.
-
New York ShoppingTell her to beware of any non-chain electronic stores in New York. Non-chain stores are the ones that are not part of a larger retail company. Many, many electronic stores in New York offer great deals that seem too good to be true and they are all over the place. Please tell her not to shop at them, because they mostly sell refurbished junk that's likely to break, and some just flat out sell stolen stuff. Like the previous people said, Best Buy and Circuit City are great picks. EB Games is a pretty good game store that's everywhere, as well. Just let her know to stay away from any store that looks at all suspicious.
-
How to Convert Process to ServiceI'll assume that you're talking about managed code here. Regular applications have an entry point that gets called when they are run. Services, on the other hand, may not be started by double clicking them because they typically don't have that standard application entry point. Instead, they have an entry point that the service control manager (SCM) understands. The SCM is what starts and stops service processes. It's fairly easy to convert one to the other. However, does your app have a user interface (this even includes message boxes)? If so, it's not a good candidate to run as a service. If you want to see how to make a service, there's a good template service project in VS. There's also a good article here[[^](http://a good article here)] on how to make a version of a service that can be debugged.
-
How can I download video files programmatically in C# [modified]Similar to something I posted a few messages up... you'll want to modify this to be a bit more asynchronous, especially if you're downloading large files like videos. This function downloads the contents of the web file and stores it in a temp file and returns the temp file name.
public string GetWebFile(string url)
{
HttpWebRequest webRequest = null;
HttpWebResponse webResponse = null;
Stream responseStream = null;
FileStream fileStream = null;
string filename = "";try { // Create the web request webRequest = (HttpWebRequest)HttpWebRequest.Create(url); // Get the response webResponse = (HttpWebResponse)webRequest.GetResponse(); // Get the response stream responseStream = webResponse.GetResponseStream(); // write it to a temporary file in 1000 byte chunks filename = Path.GetTempFileName(); fileStream = new FileStream(filename, FileMode.Append); int bytesRead = 0; byte\[\] data = new byte\[1000\]; // keep looping until we read nothing do { // read the next 1000 bytes of data bytesRead = responseStream.Read(data, 0, 1000); // write the data to the file fileStream.Write(data, 0, bytesRead); } while (bytesRead > 0); } catch (Exception e) { MessageBox.Show("Error : " + e.Message); } finally { // free any resources if (fileStream != null) { fileStream.Close(); fileStream.Dispose(); } if (responseStream != null) { responseStream.Close(); responseStream.Dispose(); } if (webResponse != null) { webResponse.Close(); } } // return the contents return filename;
}
-
How to grab a web page using .Net 2.0 WebBrowser ControlYou don't need the web browser to do that. Just do the following:
public string GetWebPage(string url)
{
HttpWebRequest webRequest = null;
HttpWebResponse webResponse = null;
Stream responseStream = null;
Encoding encode = null;
StreamReader webPageStream = null;
string webPageText = "";try { // Create the web request webRequest = (HttpWebRequest)HttpWebRequest.Create(url); // Get the response webResponse = (HttpWebResponse)webRequest.GetResponse(); // Get the response stream responseStream = webResponse.GetResponseStream(); // Get the encoding encode = Encoding.GetEncoding("utf-8"); // Read the response and using a StreamReader webPageStream = new StreamReader(responseStream, encode); webPageText = webPageStream.ReadToEnd(); } catch (Exception e) { MessageBox.Show("Error : " + e.Message); } finally { // free any resources if (webPageStream != null) { webPageStream.Close(); webPageStream.Dispose(); } if (responseStream != null) { responseStream.Close(); responseStream.Dispose(); } if (webResponse != null) { webResponse.Close(); } } // return the page contents return webPageText;
}
-
Build Comment Web Page in VS 2005That was actually removed from the product and doesn't exist in 2005. It's amazing to me really, I'd think that Microsoft would be all over competing with javadoc. You can still create the XML file by checking an option to do so in the build configuration, but there's no way to generate the HTML. There are a few options, though. I work for SlickEdit and one of the features we offer does just what you're looking for, check out the product showcase link for more info. There's also NDoc which is open source and very popular. There's another product, too, called VS Docman. Each one does things a little differently, so I suggest looking at each one to see what suits you best. I don't know if you had seen what VS 2003 spit out when you actually created the web pages, but it was pretty ugly. That's probably the main reason they dropped it.
-
2nd request for book recommendationsFor COM development, the essential book is "Inside COM" by Rogerson. Unfortunately, it will leave you with very little that you can put to serious practical use, but it will ease you into the concepts of what it means to be COM component. Many developers fail at trying to write COM components that don't understand the fundamentals. I personally use ATL, which I highly recommend. For ATL development, "ATL Internals" by Sells and Rector, and "ATL COM Programming" by Richard Grimes are both masterpieces. Good luck!
-
Windows serviceI pasted up a tweaked version of the service template that I use for my own use. It's hooked up for running it debug (which VS won't let you do with services), just use the NO_SERVICE compile flag. This is similar to something someone posted in an article recently. Anyway, enjoy. Windows service template (8k)