1. Create a queue)
#region New Queue public void NewQueue(string name) { if (CloudStorageAccount == null) return; try { ClearStatus(); CloudQueueClient client = CloudStorageAccount.CreateCloudQueueClient(); CloudQueue queue = client.GetQueueReference(name); queue.Create(); Refresh(); ReportSuccess("Queue " + name + " created"); } catch (Exception ex) { ReportException(ex); } } #endregion
2. Copy queue)
#region Copy Queue public void CopyQueue(string name, string destName) { try { ListSpinnerVisible = Visibility.Visible; ClearStatus(); CloudQueueClient client = CloudStorageAccount.CreateCloudQueueClient(); CloudQueue queue = client.GetQueueReference(name); CloudQueue destQueue = client.GetQueueReference(destName); destQueue.CreateIfNotExist(); CloudQueueMessage message; while ((message = queue.GetMessage(TimeSpan.FromSeconds(5))) != null) { destQueue.AddMessage(new CloudQueueMessage(message.AsBytes)); } ReportSuccess("Queue " + name + " copied to " + destName); } catch (Exception ex) { ReportException(ex); } Refresh(); } #endregion
3. Rename the queue)
#region Rename Queue public void RenameQueue(string name, string destName) { try { ListSpinnerVisible = Visibility.Visible; ClearStatus(); CloudQueueClient client = CloudStorageAccount.CreateCloudQueueClient(); CloudQueue queue = client.GetQueueReference(name); CloudQueue destQueue = client.GetQueueReference(destName); if (destQueue.Exists()) { ReportError("Cannot rename queue - the destination queue '" + destName + "' already exists"); ListSpinnerVisible = Visibility.Collapsed; return; } destQueue.Create(); CloudQueueMessage message; while ((message = queue.GetMessage()) != null) { destQueue.AddMessage(new CloudQueueMessage(message.AsBytes)); queue.DeleteMessage(message); } queue.Delete(); ReportSuccess("Queue " + name + " renamed to " + destName); } catch (Exception ex) { ReportException(ex); } Refresh(); } #endregion
4. delete a queue)
#region Delete Queue public void DeleteQueue(string name) { try { ListSpinnerVisible = Visibility.Visible; ClearStatus(); CloudQueueClient client = CloudStorageAccount.CreateCloudQueueClient(); CloudQueue queue = client.GetQueueReference(name); queue.Delete(); ReportSuccess("Queue " + name + " deleted"); } catch (Exception ex) { ReportException(ex); } Refresh(); } #endregion
5. Create a message)
public void NewMessage(string queueName, string textContent) { if (CloudStorageAccount == null) return; try { ClearStatus(); CloudQueueClient client = CloudStorageAccount.CreateCloudQueueClient(); CloudQueue queue = client.GetQueueReference(queueName); queue.CreateIfNotExist(); queue.AddMessage(new CloudQueueMessage(textContent)); RefreshDetail(queueName); ReportSuccess("New message queued"); } catch (Exception ex) { ReportException(ex); } }
6. delete a message (message at the top of the queue)
public void DeleteMessage(string queueName) { try { DetailSpinnerVisible = Visibility.Visible; ClearStatus(); CloudQueueClient client = CloudStorageAccount.CreateCloudQueueClient(); CloudQueue queue = client.GetQueueReference(queueName); CloudQueueMessage message = queue.GetMessage(); if (message != null) { queue.DeleteMessage(message); } ReportSuccess("Message removed from queue"); } catch (Exception ex) { ReportException(ex); } RefreshDetail(queueName); }
7. Delete all messages in the queue)
public void DeleteAllMessages(string queueName) { CloudQueueMessage message; try { DetailSpinnerVisible = Visibility.Visible; ClearStatus(); CloudQueueClient client = CloudStorageAccount.CreateCloudQueueClient(); CloudQueue queue = client.GetQueueReference(queueName); while ((message = queue.GetMessage()) != null) { queue.DeleteMessage(message); } ReportSuccess("Queue " + queueName + " cleared"); } catch (Exception ex) { ReportException(ex); } RefreshDetail(queueName); }
8. Upload messages to the queue
#region Upload Message private string UploadQueueName; private CloudQueue UploadQueue; public void UploadMessages(string queueName, string[] filenames) { if (CloudStorageAccount == null || filenames == null || filenames.Length == 0 || UploadInProgress) return; UploadInProgress = true; UploadFileList = filenames; DetailSpinnerVisible = Visibility.Visible; ReportActive("Uploading Messages.."); UploadQueueName = queueName; CloudQueueClient client = CloudStorageAccount.CreateCloudQueueClient(); UploadQueue = client.GetQueueReference(queueName); BackgroundWorker background = new BackgroundWorker(); background.DoWork += new DoWorkEventHandler(Background_UploadMessages); background.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Background_UploadMessagesCompleted); background.RunWorkerAsync(); } void Background_UploadMessages(object sender, DoWorkEventArgs e) { try { int count = 0; int errors = 0; if (UploadQueue != null) { foreach (string filename in UploadFileList) { DetailSpinnerVisible = Visibility.Visible; try { UploadQueue.AddMessage(new CloudQueueMessage(File.ReadAllBytes(filename))); count++; } catch (Exception ex) { if (errors == 0) { ReportException(ex); } errors++; } RefreshDetail(UploadQueueName); } } if (errors > 0) { if (count > 0) { ReportWarning("Upload complete, " + numberof(count, "message", "messages") + " added, " + numberof(errors, "error", "errors")); } else { ReportError("Upload failed, " + numberof(count, "message", "messages") + " added, " + numberof(errors, "error", "errors")); } } else { ReportSuccess("Upload complete, " + numberof(count, "message", "messages") + " added"); } } catch (Exception ex) { ReportException(ex); } } void Background_UploadMessagesCompleted(object sender, RunWorkerCompletedEventArgs e) { UploadInProgress = false; DetailSpinnerVisible = Visibility.Collapsed; } #endregion
9. Download messages from the queue
#region Download Messages private CloudQueueMessage[] DownloadMessageList; public void DownloadMessages(string containerName, CloudQueueMessage[] messages, string[] filenames) { if (CloudStorageAccount == null || messages == null) return; DownloadMessageList = messages; DownloadFileList = filenames; DetailSpinnerVisible = Visibility.Visible; ReportActive("Downloading Messages..."); DownloadInProgress = true; BackgroundWorker background = new BackgroundWorker(); background.DoWork += new DoWorkEventHandler(Background_DownloadMessages); background.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Background_DownloadMessagesCompleted); background.RunWorkerAsync(); } void Background_DownloadMessages(object sender, DoWorkEventArgs e) { try { CloudQueueMessage message; string filename; int count = DownloadMessageList.Length; for (int i = 0; i < count; i++) { message = DownloadMessageList[i]; filename = DownloadFileList[i]; File.WriteAllBytes(filename, message.AsBytes); } ReportSuccess("Download Complete"); } catch (Exception ex) { ReportException(ex); } } void Background_DownloadMessagesCompleted(object sender, RunWorkerCompletedEventArgs e) { DownloadInProgress = false; DetailSpinnerVisible = Visibility.Collapsed; } #endregion