@skatamatic, Wow the stupid mistake you found in the code was the actual problem.
Thank you :)
@skatamatic, Wow the stupid mistake you found in the code was the actual problem.
Thank you :)
Okay, you can assume it as +=.
The results I get are like 150907, 220769, 78905 and so on. They are different each time I run the application. I do not add or remove any files from the directory.
What can be the problem other than =+ :D
Hey guys, I am playing with the .NET 4.0's new class Parallel.
I tried to open files in a directory and calculate the total bytes of them. However when I run the code, I get a different result every time.
Can you explain me the problem I have?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Runtime.Remoting.Messaging;
using System.IO;
using System.Drawing;
namespace LearnCSharp
{
class Program
{
static long totalBytes = 0;
private static object token = new object();
static void ProcessImages()
{
string directoryPath = @"C:\files";
string[] fileNames = Directory.GetFiles(directoryPath);
if (fileNames == null)
throw new Exception("Path is wrong");
ParallelLoopResult result = Parallel.ForEach<string>(fileNames, new Action<string>(
delegate(string fileName)
{
FileStream stream = File.OpenRead(fileName);
if (stream == null)
throw new Exception("File not found");
lock (token)
{
totalBytes =+ stream.Length;
}
}
));
}
static void Main(string[] args)
{
TaskFactory taskFactory = new TaskFactory();
Task task = taskFactory.StartNew(new Action(ProcessImages));
Task.WaitAll(task);
Console.WriteLine(totalBytes);
Console.Read();
}
}
}
If you do not modify and read a data from memory or hard disk, then you don't need to worry about thread safety, I think. The race condition or concurrent access/modify is important when you have a shared data between threads, in this function you do not have. You are doing only computation.
If you need to modify and access a shared data concurrently with multiple threads then you need a lock mechanism. This feature comes out of the box with thread safe modules or you can implement it on your own. You can search the internet.
Command line arguments are passed to programs automatically when you use them. You just define and use them if you need them, which is almost never done if you are not using a command line based Linux OS.
What you do to run a program on command line, you call it like "myprogram.exe". If you want to run it with arguments like -debugmode, you can add the parameter "myprogram.exe -debugmode".
Then this first argument is assigned to your char** string array.
I think you can't do that with the current C#. Maybe if you can edit the CLR and modify the compiler to create appropriate IL when you use your "own" type then you can manage it, which is almost impossible :D
You can try C++ though!
Delegates are not very powerful indeed. As you said, it is a master method that calls other methods when a specific condition happens.
What it brings to the table is it has a subscription list. So if you do not know which functions to call while you are writing this master method, well you cannot call them in your method! So delegates enable you to define the master method and let others subscribe to it, then fire them all when appropriate.
You need basic SQL queries if I understood your problem right.
OK, I think I got it.
When setting, I should set to the clone of the value so that any modify to the value will not affect our reference later.
When getting, I should simply return the clone if it is a reference type.
Examples of how do you handle returning references by properties?
Do you just return the reference or return the clone?
If you return the clone, how do you handle assignments to the reference while implementing the class methods without loosing implemented set/get logic in a property?
If you return the reference, how do you make sure that reference will not be modified by external calls?
class Car
{
private Engine _engine;
public Engine Engine
{
get { return _engine.Clone(); }
set
{
if(value.HP > 200)
{
throw new Exception("Too much power for the current car model!");
}
_engine = value.Clone();
}
}
public Car()
{
// Use property to ensure property logic runs
Engine = new Engine(400); // Edit: OK, set to the clone of the value
// Use reference but loose the property logic, loose events in the property too
_engine = new Engine(400); // This one bypasses the 200 HP limit in the property logic
}
}
Best practice is really no different from parameters and return values from functions. If you can get away with a reference, do so. It's generally more efficient for both space and execution time. Not to mention that it's easier to reason about a single object's lifetime than multiple copies of the object.
Hi Narue, nice new avatar.
Can you please give some examples?
Hey,
I am developing a game in C#. I have some question marks in my mind about properties.
-Should I return reference in a get, or should I clone the reference and return a clone?
If I return a reference then in another part of the program, it will be possible to get that reference from get, copy it to another object and modify it, it is bad isn't it?
If I return a clone, then I need to use the private field itself in the class implementing the property, it is bad too isn't it?
What are the best practices about using properties in C#?
Why not use placeholders?
You can save the file to in the file system. When generating URL use HttpRequest.ApplicationPath and add the file name to the add. This will be your URL to the picture.
OK, good luck with that !
You can post here.
Google always wins. iOS will never get above Android again.
Why not use Print button on the browser or keyboard?
I thınk we do not always.
You wrıte java code. Compıler turns these code into java byte code, which is OS independent code. Then an interpreter, which we call JVM executes thıs code like an operating system. This allows java to be OS independent.
Or JVM compiles the code just-in-time to native machine code which the OS can run.
Please take some English course before moving to mobile programming.
If you need help you should write readable sentences.
You need to send the whole code.
You are saying C does not support object oriented and show C# code ?!!
This is like showing tasting a cake and saying that the meat is tasty.
If your application is small you can put your usernames and passwords into a encrypted binary file.
Do not forget to encrypt usernames and passwords with different algorithms or it would be easier to hack your application by knowing a username and its encrypted version.
Your server program will be installed once/ so distributing a one program with server and client capabilities would be redundant.
Design and distribute your client and server applications seperate.
is your web server running?
Yeah maybe %99 terrorists are Muslim but %100 despot occupiers are Christian. So blaming religions is not a way to describe that.
Just draw the buttons as you like. Draw unpressed button, pressed button and mouse over button as an image.
Use events to catch mouse actions and display appropriate image.
From the perspective of a game developer .)
1-Ensure there is room for that and assign it
2-Full is something that you define. If the items in the array is equal to limit, it is full
3-Iterate over each element and check if it equals the name
4-Add index to the array pointer and dereference it
and you may want to avoid using namespaces in header files.
Is he trying to download excel files from clients? If so, is there a way to exclude the time to download the file from php code running time?
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
#define Tellalca GOD;
double ApproachPI()
{
static int call = 0;
call++;
double sum = 0;
for(int k = 0; k < call; k++)
{
sum += 4 * ((pow(-1.0, k)) / (2 * k + 1));
}
return sum;
}
int main(int argc, char** argv)
{
for(int i = 0; i < 100; i++)
{
cout << "PI(" << i << ") = " << ApproachPI() << endl;
}
return 0;
}
Hmm why not test yourself. Write a test application that does the things with standard libraries and primitive constructs.
The problem with Java is not running slow but memory usage I think. You can watch and read what Stroustrup explains about Java, C# and C++.
Go Java and Php
First versions of Linux kernel was developed by a lone man called Linus.
Learning C# is not just learning the syntax. After you get the basics learn more about .NET platform. If you know .NET and C# there is no such distinct thing as Asp.NET at all, it is just server side C# code .)
Well, in order to be a web developer you should know databases, network, web and also desktop development.
I think there is not much specializing for a developer because the technology is evolving so fast, the only thing you should specialize in is learning new tech fast and efficiently.
I am just making request with a console application and read the html response.
Of course Firefox 4 !!!
Just create more else if's.
K... That would be (probably) very difficult, as far as I know, .xls (Excel format) is an XML container. That means that you would have to create an XML reader, then parse it to give you the information you want.
I think that's what you want, your question isn't very clear.
:-) Hope this helps.
There are XML parsers.
You can do it very easily using Virtual C++(.NET).
Develop some oldie console games.
Maybe he is asking how to develop a game using C++.
You need to manage your graphics and sounds using graphic/sound libraries like OpenGL and OpenAl.
The structure of your game may be standard c++ or you can use some game engines.
A good library to start may be SDL or Allegro or SFML(C++). Just google them and follow the tutorials.
Yes it is possible. You have to work with your OS api.
And it is easy, in Linux just observe running processes and conditionally create a new thread and use execv() to run a specific program.
#include <iostream> #include <string> #include <time.h> #define private public using namespace std; void sleep(clock_t wait) { clock_t goal; goal = wait + clock(); while(goal > clock()) ; } void memoryleak() { sleep(2000); long long int i = 0; while(i < 999) { i++; cout << "HAHAHA\n\n\n"; } long long int g = 0; while(g < 9) { g++; } int* d = new int; *d = 0; delete d; *d = 0; cout << *d << "\n"; d = new int; while(true) { cout << "HAHAHA\n\n\n"; } } class someclass { private: string variable; }; int main() { someclass a; a.variable = "I hacked your string!"; cout << "HACKED!! " << "Your changed your variable value to " << a.variable; cout << "\n\n And now random memory leak!!!!"; sleep(2000); memoryleak(); return 0; }
I know, I am evil :p
Hehe, if "#define private public" works it would be very funny answer to that question .)
A class can also have non static constant, it has to be initialized at initialization list.
class A{ const int a; public: A(int a) : a(a){ } };
You can not do
int &var=NULL;
and there is a reason behind it.
any & is reference and once it is initialized, it can not be initialized again, so NULL is not a varaible/memory which & refer to.
You can not even do this:int &var=2;
but you can do
int *p = NULL;
, you can do this because you can reset/intialized a pointer whenever you want.
What is the point of having a non static const in your class, just asking to learn, no offense.
Wow, you are such an inspiration, lolz :D
If we could tell you how to create such websites in a few sentences, everyone would be a web developer, right?
There is no loop. So you randomize the color only once, that is why you see only one color.
C is really a system programming language. So if you want to learn more about C and its usage, learn some UNIX/Linux programming. Linux kernel and GNU interface will help you about this.