|
Dear all,
This is my situation:
I try to create text file by using below code:
using (FileStream fs = File.Create(path)){};
It runs ok at debug mode, file is create at [path].
But nothing happens at release mode.
What wrong with this code? Pls. explain for me if you know.
|
|
|
|
|
you should put that in a try-catch and look at the Exception.ToString().
what is the exact content of path?
does the path exist? (if relative, it could be different in release vs debug)
Which Windows edition?
|
|
|
|
|
Thank for your reply.
About your question, I answer as below:
- I also put the try-catch & look for Exception, but there is no exception.
- The path is absolute path: C:\Program Files\MyFolder and it is existed.
- I run in Window XP, VisualStudio 2003.
|
|
|
|
|
If you are trying to write to Program Files it could be a permissions issue. I would try another path that is definately going to accessible such as:
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
To get the path you're using ath the moment I would do it this way:
string folderPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "MyFolder");
I doubt that is related to your problem but may avoid problems in future.
|
|
|
|
|
ndkit wrote: Window XP
That is OK, and will not add unexpected limitations to what you can and can't do.
ndkit wrote: VisualStudio 2003
So you are using .NET 1.1?
I haven't used that for many years now, and I never will target it again. By all means, consider switching to .NET 2.0 (or higher). I'm not saying your problem is directly related, however 2.0 is much better than earlier versions, both in quality and functionality.
If you don't have VS 2005/2008/2010 available, you might try using VS 2010 C# Express Edition, which is free and can be downloaded here[^].
|
|
|
|
|
I have a web service that recieves calls from a number of clients (600 and growing). This calls a 3rd party web service to return some image data, which is then sent to the client.
If i spam the 3rd party service with several calls at once (around 20+), it grinds to a halt, and takes over 2 seconds per call to return, when it normally takes 0.3 seconds on a single call.
I noticed that if i throttle the calls (added a sleep of 500ms between each web request), the 3rd party runs fine.
The question is, whats the best way/how can i implement a throttling system in my code, so if i receive several calls on different threads, i dont send them all at once, but que them up, so the 3rd party server has time to respond.
Regards,
Gareth.
(FKA gareth111)
|
|
|
|
|
A throttle is generally a looping background thread, combined with a queue and a signaling mechanism... Here's one way to do it (Pseudocode):
public SendItem()
{
Add request to queue
Send a new data signal (Perhaps a System.Threading.EventWaitHandle)
}
private Throttle() (Running on background thread)
{
Loop indefinitely
{
If queue is empty
Wait for signal
Else
{
Process item from queue
Wait X milliseconds
}
}
}
|
|
|
|
|
Below is the code i've done from your example.
The only bit i am unsure about is the waithandle. I know what/how to use them, but am unsure how it works in this scenario. Why would i want to hold the thread at a point?, is it just to save cpu cycles so it doesnt keep checking if the queue is empty?
public void AddImageRequestToQueue(string isbn)
{
Contract.Requires(!string.IsNullOrEmpty(isbn));
lock (_queuePadLock)
{
_imageRequests.Enqueue(isbn);
}
}
private void ProcessImageQueue()
{
do
{
if (_imageRequests.Count > 0)
{
string isbn = string.Empty;
lock (_queuePadLock)
{
isbn = _imageRequests.Dequeue();
}
IImageResult imageResult = GetImage(isbn, ImageSize.Small);
OnProcessImageCompleted(new ProcessImageCompletedEventArgs(imageResult.Image, imageResult.ISBN));
Thread.Sleep(SleepTime);
}
}
while (true);
}
Regards,
Gareth.
(FKA gareth111)
|
|
|
|
|
Yep, that's exactly what the wait handle is for. Makes your app more efficient by not having the thread endlessly looping.
|
|
|
|
|
Cool cheers!
Regards,
Gareth.
(FKA gareth111)
|
|
|
|
|
I agree with Ian's approach, which basically is tunneling all 3rd party accesses through a single thread.
However, it may not scale well, as in the end either the single thread tunnel or the web service itself will become a bottleneck. If the 3rd party package is slowing down under extra load, my first impression is it is running out of memory and starts to page to disk all the time.
Depending on what exactly causes the phenomenon, you could limit the number of outstanding requests to N (N to be determined and apparently less than 20). Either use a single thread and some logic to achieve this; or just launch N threads, each trying to find and launch one job at a time, all from a single queue (with appropriate lock while accessing the queue).
OTOH, if the web service itself is basically single-threaded (e.g. because a COM component is involved), then whatever multi-threaded stuff you add to feed it may not achieve anything.
|
|
|
|
|
Cheers for your reply Luc, informative as always
I am going to contact the 3rd party as well, as you say, i might be doing all this work for nothing if their end has limitations.
Regards,
Gareth.
(FKA gareth111)
|
|
|
|
|
Remember that browsers to have their limitations - as in IE 8 (for e.g.) can open only 8 (or was it 6) asynchronous connections at a time. So even if your web services can handle tons of data (you are going to run into problems on the client side).
Firefox allows you to configure this limit.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
My latest tip/trick
Visit the Hindi forum here.
|
|
|
|
|
The client is not a web browser, its a C# windows service. Sorry i did not make that clear.
Regards,
Gareth.
(FKA gareth111)
|
|
|
|
|
hi all.
i have a little Problem that need a help in my program.
I want to search for a field "Name" in the table "Person" of my SQLdatabase and save its ID in another table "Personel".
i do this through this code:
linqDataContext obj = new linqDataContext();<br />
var per = obj.persons.Where(item => item.Name == personCombo.Text).Select(item => item.Id);<br />
obj.PersonelSave(System.Convert.ToInt32(per.ToString()));<br />
I should say that Personel save is a procedure in my linq.dbml file
ALTER PROCEDURE dbo.PersonelSave @PersonId int<br />
AS<br />
insert into personel values (@PersonId)<br />
RETURN
and the error for my code is: "Input string was not in a correct format."
please help me to solve this problom.
|
|
|
|
|
Assuming you're getting the error on PersonelSave()... Set a breakpoint there, and see what the value of "per" is.
Also, what type is "per"? It's not a "Person", or whatever that class is called. If your Id field is numeric, then it's already a number, and you shouldn't be converting it to a string and parsing it back to an integer.
|
|
|
|
|
that's so i don't know how should i do that.
my Id field is numeric and i don't want to convert to string but i can't find any more way.
may i ask a help?
|
|
|
|
|
So don't convert to a string.
Like I said, set a breakpoint... You need to learn how to debug, or you'll be on here every day asking for more help.
And don't use the "var" keyword until you REALLY know what you're doing. It leads to bad habits.
|
|
|
|
|
Hi All,
I'm stuck on this particular issue. I have a custom combobox that is to be used with various generic collections of different types. Inside this control it needs to automatically update a database table with new values and then add and select them to the underlying collection.
If I use the Items.Add() method it blows up with "Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead."
The problem I'm facing is how to cast the ItemsSource so that I can actually add the item and then set is as the selected item.
Any ideas?
TIA,
|
|
|
|
|
I think you have to unbind the ItemsSource property, modify the collection, and then rebind.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
Just cast it as an IList, or an ICollection if necessary. All of the generic lists implement the non-generic interfaces. Just make sure you're adding an item of the correct type.
|
|
|
|
|
Thanks for the replies guys ...
|
|
|
|
|
It's exactly what the error message says... Your ItemsControl is bound to a Collection or View which serves as source for data. Once this (apparently one-way-from source) binding is estabilished, you cannot add items directly, you have to use the ItemsSource...
|
|
|
|
|
Hi
I have one query as
Consider i have ICheck interface which is implemented by class "Data".
So I can access functions in Class Data using
Code as
1)
Data obj;
obj.Fun();
2) ICheck ck = new Data();
ck.Fun()
So what is advantages of using second code.
|
|
|
|
|
You can implement the ICheck interface in any class/struct you like. You can then use the same method an any instances of any of those classes.
|
|
|
|