|
So do: you can call the same event handler for all your boxes, and use the Tag property to hold the "I'm editing" flag. You get the appropriate TextBox instance passed in as the sender parameter of the event handler.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
i am newbee for c#, can please explain how to do.
|
|
|
|
|
Which bit? This is all basic stuff ...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
thats ok. but i don't know, which active control is enabled when i select textbox.
|
|
|
|
|
Sorry?
Do you mean you don't know which control it was when you enter the event handler? Because as I said, that is given to you as the sender parameter.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
|
Hi,
I want to send push notifications to mobile phones that I will register. And I want to use Azure Notifications Hub for this purpose. If this possible?
Best regards.
|
|
|
|
|
That's a question that is specific to Azure Notifications Hub, not to C#. According to their website, it is possible.
Push notifications for consumer and enterprise apps—from any backend to any device platform
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I have an app that transfers files to a server via FTP. This class doesn't actually do the Ftp'ing, it just maintains a queue of files to be sent.
Some requirements:
1) No more than 3 files can be FTP'd at one time.
2) The appearance of a file in the target folder should start the process.
3) When a file is done being FTP'd, the next file should automatically start.
So I came up with this FTPQueue class[^]. I would appreciate any feedback.
Thanks
public static class FTPQueue
{
private const int MAX_FILE_UPLOADS = 3;
private static int _filesBeingUploaded = 0;
private static Queue _fileQueue = new Queue();
private static List<string> _filesInProgress = new List<string>();
public static void FileReceived(string fileName)
{
Console.WriteLine("Received file {0}", fileName);
_fileQueue.Enqueue(fileName);
UploadNextFile();
}
private static async void UploadNextFile()
{
if (_fileQueue.Count == 0)
{
return;
}
string fileName = (string)_fileQueue.Peek();
if (!string.IsNullOrEmpty(fileName))
{
if (_filesBeingUploaded >= MAX_FILE_UPLOADS)
{
return;
}
if (_filesInProgress.Any(x => x == fileName))
{
return;
}
_filesBeingUploaded++;
Console.WriteLine("Files being uploaded {0}", _filesBeingUploaded);
Task<bool> upload = TransferFile(fileName);
bool result = await upload;
Console.WriteLine("Files {0} upload complete", _filesBeingUploaded);
FileUploadComplete(fileName, result);
}
}
private static async Task<bool> TransferFile(string fileName)
{
Console.WriteLine("Tranfering file {0}", fileName);
await Task.Delay(10000);
bool result = true;
return result;
}
private static void FileUploadComplete(string fileName, bool result)
{
if (result)
{
Console.WriteLine(string.Format("File {0} upload complete", fileName));
if (_fileQueue.Count > 0)
{
Console.WriteLine("Removing file {0} from the queue", fileName);
_fileQueue.Dequeue();
_filesInProgress.Remove(fileName);
}
_filesBeingUploaded--;
Console.WriteLine("Files being uploaded {0}", _filesBeingUploaded);
}
else
{
Console.WriteLine(string.Format("File {0} upload failed", fileName));
}
UploadNextFile();
}
}
For testing I ran it from a simple Console app:
class Program
{
private static Timer _timer;
static void Main(string[] args)
{
_timer = new Timer();
_timer.Interval = 3000;
_timer.Elapsed += _timer_Elapsed;
_timer.Enabled = true;
Console.ReadLine();
}
private static void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
var fileName = Path.GetRandomFileName();
Console.WriteLine("Created new file {0}", fileName);
FTPQueue.FileReceived(fileName);
}
}
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Why even bother trying to FTP more than 1 file at a time; is this supposed to increase throughput?
|
|
|
|
|
Why not?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Well, does it increase throughput? If not, it's a make-work project.
|
|
|
|
|
It's possible for many files to be dropped into the folder at once, so I want to send more than one at a time to decrease the total time it takes to get them to the server.
If there's a lot of files and they go one at a time it could take forever for them to finish.
I set it to 3 for starters but if it works well I could up it to 5 or even 10. Each one will be in it's own process so they should all go concurrently.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Are you sending to different destinations? If not, have you determined that transmitting more than one at a time reduces the overall elapsed time?
It's hard to optimize a solution when there is no apparent benefit.
|
|
|
|
|
I can't see why FTP'ing more than file at a time wouldn't work. It would certainly be quicker than sending them one at a time.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Everything is going through the same "hose".
In addition, you now have to consider the potential for multiple failed FTP's in progress.
|
|
|
|
|
Not necessarily.
Sending 3 files at the same time is not any faster than sending the same 3 files one at a time. The network pipe can still only send one bit at a time, not 3. The benefit you get from sending multiple files at the same time is limited to the amount of time it takes to send and receive FTP protocol data for each file, not the file data you're sending. This including starting the FTP session, authentication, navigation, file transfer commands, file framing data, acknowledgments, ...
Since, in both test cases, you're sending the exact same file data, the timings of sending the actual file data cancel each other out. It may indeed be faster, but you're not going to see a result of sending 3 files at the same time takes 33% of the time of sending one at a time.
|
|
|
|
|
So are you saying that there's no benefit in sending multiple files at once? If so, what's the right way to do it?
Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
No, I'm saying that you're not going to get the boost in speed you think you're going to get.
|
|
|
|
|
So it sounds like it's best to just send one file at a time?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
(A cry in the wilderness)
|
|
|
|
|
What?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
|
It's the easiest to implement.
If you're trying to get someone to make the decision for you, it's not going to happen. It's up to you to evaluate how much work this is going to be for you and whether or not it's going to be worth it.
|
|
|
|
|
I'd be inclined to simplify that.
Start with a general-purpose async throttle, inspired by code from Stephen Toub's blog[^]:
public sealed class Throttle
{
private readonly Queue<TaskCompletionSource<IDisposable>> _waiters = new Queue<TaskCompletionSource<IDisposable>>();
private readonly WaitCallback _releaseCoreCallback;
private readonly Task<IDisposable> _releaserTask;
private readonly Releaser _releaser;
private readonly int _maxCount;
private int _currentCount;
public Throttle(int initialCount)
{
if (initialCount <= 0) throw new ArgumentOutOfRangeException();
_maxCount = _currentCount = initialCount;
_releaser = new Releaser(this);
_releaserTask = Task.FromResult((IDisposable)_releaser);
_releaseCoreCallback = ReleaseCore;
}
public Task<IDisposable> WaitAsync()
{
lock (_waiters)
{
if (_currentCount > 0)
{
_currentCount--;
return _releaserTask;
}
var waiter = new TaskCompletionSource<IDisposable>();
_waiters.Enqueue(waiter);
return waiter.Task;
}
}
private void Release()
{
TaskCompletionSource<IDisposable> toRelease = null;
lock (_waiters)
{
if (_waiters.Count > 0)
{
toRelease = _waiters.Dequeue();
}
else if (_currentCount < _maxCount)
{
_currentCount++;
}
else
{
throw new SemaphoreFullException();
}
}
if (toRelease != null)
{
ThreadPool.QueueUserWorkItem(_releaseCoreCallback, toRelease);
}
}
private void ReleaseCore(object state)
{
((TaskCompletionSource<IDisposable>)state).SetResult(_releaser);
}
private sealed class Releaser : IDisposable
{
private readonly Throttle _throttle;
public Releaser(Throttle throttle)
{
_throttle = throttle;
}
public void Dispose()
{
_throttle.Release();
}
}
}
That gives you a simple way to restrict the number of async methods which can access a resource simultaneously.
Your FTPQueue class then becomes:
public static class FTPQueue
{
private const int MAX_FILE_UPLOADS = 3;
private static readonly Throttle UploadThrottle = new Throttle(MAX_FILE_UPLOADS);
public static async Task FileReceived(string fileName)
{
Console.WriteLine("Received file {0}", fileName);
using (await UploadThrottle.WaitAsync())
{
Console.WriteLine("Uploading file {0}", fileName);
bool result = await TransferFile(fileName);
if (result)
{
Console.WriteLine("Finished uploading {0}", fileName);
}
else
{
Console.WriteLine("Failed to upload {0}", fileName);
}
}
}
private static async Task<bool> TransferFile(string fileName)
{
await Task.Delay(10000);
return true;
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|