|
What you are referring to is what I believe is called "Garbage Collection".
This used to be very important in C as resources could easily be taken up very quickly if one did not dispose objects once they had been used.
Although with C# you can still dispose object I understand this not to be necessary as the .NET framework takes care of this for you.
With regards to passing the list by reference - yes only 1 copy would persist.
If you were coding in C then this might be necessary, however with C# I think the saving would be minimal.
Also with regards to future development when someone else looks at your code they may be puzzled as to why the list is passed by reference.
I am still learning C#, so I imagine one of the more experienced coders may have more valuable information.
You always pass failure on the way to success.
|
|
|
|
|
Hi,
yes, you should call Close() or Dispose() on those objects that offer them, when
you no longer need the object.
no, passing an reference type (i.e.an instance of a class) to some method does not create
another object; to create an object you must either use the "new" keyword, or call a
creational method (one that has a "new" inside, as in a Factory pattern), which I hope
has "Create" in its name.
no, passing a reference type with the "ref" keyword does not change the object count;
the only thing it does is it allows the called method to not only modify the object,
but also the caller's variable that references that object.
|
|
|
|
|
Hi Luc,
Thank you for clarifying.
Regards,
Gareth.
|
|
|
|
|
By doing the following
BackgroundWorker syncWorker = new BackgroundWorker();<br />
syncWorker.DoWork += new DoWorkEventHandler(SynchronizeDir);<br />
syncWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Synchronized);<br />
syncWorker.RunWorkerAsync(new Object[] { fromDir, toDir, filter, createRemotePath });<br />
Synchronized(syncWorker, new RunWorkerCompletedEventArgs(null, err, true));
A backgroundworker is spawned from the GUI thread into a new thread.
Is there a way to catch any errors that don't get handled by the new thread, in the GUI thread?
Google tells me the RunWorkerCompletedEventhandler (Synchronized in this case) should normally be fired and its RunWorkerCompletedEventArgs should contain the exception in the .Error field.
Rethrowing the error in the spawned thread doesn't get the exception through to the GUI thread.
Even plain ignoring the exception gives me an .Error field with null as value.
What am I doing wrong?
|
|
|
|
|
Not sure what resources you have researched thus far, but this might help.
http://msdn2.microsoft.com/en-us/library/hybbz6ke.aspx[^]
You may already know this information, but reviewing MS's example may reveal what change you need to make.
Just because we can; does not mean we should.
|
|
|
|
|
Yes, that is in the category of examples I found all over the net.
So I figure I'm doing something very wrong...
edit: for some reason the code tags don't interpret the indentation on this post ...
private void SynchronizeDir(object sender, DoWorkEventArgs e)<br />
{<br />
string from = (string)((object[])e.Argument)[0];<br />
string to = (string)((object[])e.Argument)[1];<br />
string filter = (string)((object[])e.Argument)[2];<br />
bool createRemotePath = (bool)((object[])e.Argument)[3];<br />
try<br />
{<br />
SynchronizeDir(from, to, filter, createRemotePath);<br />
}<br />
catch (Exception err)<br />
{<br />
throw err;<br />
} <br />
}
To me it looks like I'm doing the same steps as in the example:
- Extract the argument(s)
- Start the time-consuming operation
- I don't handle the cancel option(yet)
In my code, after extracting the arguments, I call the regular method with those arguments and catch anny errors that normal method would throw. They get caught as they normally are.. But what should I do to make the Synchronized event trigger with the .Error field filled in.
Right now the program just stops on the error throwing...
|
|
|
|
|
From here I would work towards handling the error here in your catch statement. It appears that the catch statement here is running on the main thread.
Just because we can; does not mean we should.
|
|
|
|
|
This is the method that gets called by the bgndWorker.
Like this:
public void BeginSyncingDir(string fromDir, string toDir, string filter, bool createRemotePath)
{
BackgroundWorker syncWorker = new BackgroundWorker();
syncWorker.DoWork += new DoWorkEventHandler(SynchronizeDir);
syncWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Synchronized);
syncWorker.RunWorkerAsync(new Object[] { fromDir, toDir, filter, createRemotePath });
}
As for now I pass the error as a result.
Which is extremely ugly.
|
|
|
|
|
the CODE tag is fine for single-line stuff,
for multi-line code snippets, use the PRE tags, and:
- add a space to empty lines (otherwise they will vanish)
- avoid HTML-like stuff (such as a less-than comparison in an if or for); if necessary
insert a "& l t ;" (without the spaces) instead of the < sign.
|
|
|
|
|
Hmm, Ok.
I'll take it into account for my other posts.
Weird that the code tag is not for code though
Thx for the reply
|
|
|
|
|
Hi,
My combobox is bound to a dataset1.tables[0].defaultview. The values of my combobox to dataset2.tables[0].defaultview. During inserting i set the rowfilter of dataset2.tables[0].defaultview which works fine. The nbr of items in the combobox is limited to the records i filtered.
After choosing a selection and after edit, my combobox.selectedvalue = 147 (the unique key in the dataset.)
But when i set the rowfilter back to string.empty my selectedValue of my combobox becomes <null> but in the combobox there's still the correct last selected text.
Result is that i get an error if i want to edit the same record again, even when everything looks fine to the user.
So it seems that the binding is mixed up or so.
Anyone any idea what goes wrong ?
Thx
Kurt
|
|
|
|
|
combobox.Text = ""; should fix you up.
Just because we can; does not mean we should.
|
|
|
|
|
thx mate and a happy new year.
Kurt
|
|
|
|
|
hi,
how can i remove the this error "Unsafe code may only appear if compiling with /unsafe" ?
I AM WORKING ON "PLOTTER ROBOT"(FYP).
|
|
|
|
|
Does it point you to any code?
Are you using pointers anywhere?
|
|
|
|
|
hi,
by adding /unsafe to the command line when invokind csc.exe,
or by checking the "unsafe" checkbox in Visual's project properties page.
|
|
|
|
|
Hi,
I have some problem using vector in C#.
for example I have vector of double, let say double[] vec1 = new double[] {1, 3, 5, 6, 7, 8, 9 };
And I want to get a view on a part of this vector without copy.
Let say I want to get the vector: double [] vec2=new double[]{5, 6, 7};( which is a part of vec1)
without creating a new vector or without copy the initial vector.
In C++ I can use iterator: data_it_begin = vec1.begin() + 2;data_it_end = vec1.begin() + 5.
I don't how to this in C#.
Does someone can help me.
Thanks!!!
|
|
|
|
|
for(int i=2; i<5; i++)
{
}
|
|
|
|
|
Yes you are right. But I don't want to do that.
I will try to explain again.
As I say.
If I have a fuction Test1( double[] x)
{
/// I can loop on vector element
}
Instead of doing thant I prefer
Test2(std::vector<double>::iterator begin,std::vector<double>::iterator end)
{
//I can loop on element
}
For Test2, there is no need to copy the vector, I just need to pass iterator.
like
std::vector < double > vec1,
Test2(vec1.begin() + 3 , vec1.begin()+ 5), without copy the vector.
For Test1, I have to copy, vec2 = new double[]{vec1[3], vec1[4],vec1[5] }
before Test1(vec2).
I think it more clear!!!
|
|
|
|
|
Hi,
a lot of methods that operate on arrays have an overload taking a start index and a
length. Example:
public static void Sort (
Array array,
int index,
int length
)
That is the easiest way to have them work on a contiguous subset of array elements.
|
|
|
|
|
Ok thanks, I think that is easiest way.
I think I need to pass the vector and the indexBegin and endEnd.
public void Test( double[] vec,int indexBegin,int indexEnd)<br />
{<br />
}<br />
So If one has a big vector It can call the method without copy the vector just doing
Test( vector, 2,3).
So there is no more elegant way to do this in C# as in C++( using STL).
|
|
|
|
|
Hi,
in .NET those simple collections are called arrays, not vectors.
and by convention a subset of most collections is defined by start and length,
not start and end.
yes, all .NET languages have enumerators too, but I pointed you to the easiest solution.
|
|
|
|
|
How to add to it more functions and still use Math.SomeFunction() convention.
Math class is static, that is sealed. Is it possible to use partial or any other mechanisms?
chesnokov
|
|
|
|
|
Chesnokov Yuriy wrote: How to add to it more functions and still use Math.SomeFunction() convention.
Math class is static, that is sealed. Is it possible to use partial or any other mechanisms?
You can't. Create your own utility class for the additional functions.
|
|
|
|
|
You can't add anything to a sealed class.
With C# 3.0 however, which ships with Visual Studio 2008, you can use a technique called "extension methods" to certain datatypes.
modified 12-Sep-18 21:01pm.
|
|
|
|