Hit URL using VB.NET (Get and Forget) - vb.net

I want to create get and forget Url using vb.net. Basically I will push an url(just get response from url, not showing in the browser/run in background) and forget. I want push 10 url at the same time when I the button clicked. Here's my code, but it still processing the url the url one by one.
I have tried using Task, but I got an error when including "Imports System.Threading.Tasks" because my .net framework version still 3.5.
It is impossible to upgrade my .Net version, so is anyone have an advice?
Please let me know what should I do, thanks before :)

You can use regular threads for this sort of thing. My VB.Net is very rusty. Here's approximately what I would do in C#. It should translate directly to VB.Net
List<Thread> threads = new List<Thread>();
foreach (string url in MyUrls)
{
Thread t = new Thread(() => MyUrlPushMethod(url));
t.Start();
threads.Add(t);
}
foreach (var thread in threads)
{
thread.Join();
}

Related

How to add customized typing indicator using c# in Azure bot

I am building a chat application using botframework v4 with .net core. I want to implement customized typing indicator. Currently I am using below code and it is showing typing indicator like below image
public async override Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default)
{
ITypingActivity replyActivity = Activity.CreateTypingActivity();
await turnContext.SendActivityAsync((Activity)replyActivity);
await Task.Delay(5000);
await base.OnTurnAsync(turnContext, cancellationToken);
}
output:
But instead of showing this typing symbol(...), I need to show some customized message (like bot is typing) and after getting bot response indicator need to remove automatically.
Using WebChat, there's not a built-in way to easily customize the typing indicator. But you should be able to modify the css. I would start with some investigation here.
If you are wanting to do this for more than just WebChat, that might not be possible. Instead of using typing activities, you might be able to use proactive messages to accomplish the goal.

Xamarin Xaml force update interface elements

The post has been rewritten to better fit the current problem.
I have a button x:Name="selectVesselButton". On button click, it tries to establish a connection to a server, which takes a sec or two for to do. Originally, I wanted the button to be grayed out while it was downloading and deserializing the json file from the connection.
My old code (before async, and trying to update the button):
// disabling the button to prevent spam clicking.
string buttonText = selectVesselButton.Text;
selectVesselButton.IsEnabled = false;
selectVesselButton.Text = "loading...";
// retrieve data for speed page.
RetrieveData();
// redirect to next info block if build was successfull.
FocusSpeedblock();
// enabling the button again.
selectVesselButton.Text = buttonText;
selectVesselButton.IsEnabled = true;
The issue with this code was that the button visuals did not update until the RetrieveData() was finished, defeating the purpose of doing that at all. This was because the code for updating the interface and the code for downloading and deserializing the object were both on the same thread.
However, following Ivan's advice, I made the downloading and deserializing Async, which fixed this issue (more like moved it).
This works fairly well, but I am still having some trouble updating the interface automatically. I have some labels that need to be updated based on the json file output. The value of the labels update on the background, but only update visually once I interact with the labels (I.E. scrolling the scrollview they are on). Check edit 3 for more detail on that.
EDIT 3:
When the second thread is finished, it should call the UpdateSpeedLabels() and update some labels. However, they update in codebehind, without instantly updating the interface. They only update if I interact with those labels.
The preferred way of doing this on Xamarin is with data binding. As you opted out of this it is still possible.
What you need is to ensure that your long task is not running in the UI thread as it blocks it and prevent its updates. You do this by using Task.Run(() => { your task code }); . However you can't update your user interface inside the Task.Run as it is not running on the UI thread and it would crash the app, so you need to use Device.BeginInvokeOnMainThread(() => { your UI code }); inside Task.Run for that part.

"Invalid cross-thread access" when changing a WP8 textBlock

I'm coming from VB 6 and am semi-new to VB.NET.
I'm writing a Windows Phone 8 application. There's a grid in which I have several textBlocks that I want to dynamically display data from a file stream (that contains scanned data).
When the WP8 page opens, it automatically loads the data into the textBlocks. This works. Before I load the data from the file, I want to "reset" all textBlocks and hide them. For this I wanted to use a procedure which essentially does the following for every textBlock:
tbl1.Text = ""
tbl1.Visibility = System.Windows.Visibility.Collapsed
This works exactly one time: when the page loads. The procedure does not produce an error.
Now when I call that method again later, when I want to refresh the data shown on the page, I get the following error on the first line of code shown above:
An exception of type 'System.UnauthorizedAccessException' occurred in System.Windows.ni.dll but was not handled in user code
Additional information: Invalid cross-thread access.
I'm a bit lost now. In VB6 I was able to do whatever I wanted with my UI elements. I presume I'm making some kind of newbie mistake here?
I read somewhere about some Dispatcher thing. But that seems overly complicated to simply change a value in a textBlock to me. Is there no simple way?
The solution is indeed to use the Dispatcher.BeginInvoke method.
Dispatcher.BeginInvoke(() =>
{
tbl1.Text = ""
tbl1.Visibility = System.Windows.Visibility.Collapsed
});
The reason why you need this is because you try to access an object that depends on the UI thread from a non-UI thread.
It is probably because you are not calling the method from UI thread. Try using Dispatcher.BeginInvoke:
Dispatcher.BeginInvoke(()=>
{
tbl1.Text = "";
tbl1.Visibility = System.Windows.Visibility.Collapsed;
});

Silverlight SaveFileStream Silently Fails

I have a little piece of code that seems to be a textbook example of saving files from Silverlight 4.0, but it doesn't seem to work.
The following snippet comes from a button click handler:
var saveDialog = new SaveFileDialog() { Filter = "All Files(*.*)|*.*" };
if (saveDialog.ShowDialog() == true)
{
using (var stream = saveDialog.OpenFile())
using (var writer = new StreamWriter(stream))
{
writer.WriteLine("Hello, World!");
writer.Flush();
writer.Close();
}
}
I've tried saving a file to many different locations, all with the same behavior:
The SaveFileDialog appears to behave normally.
The SaveFileStream appears (from the debugger) to behave normally.
After the call to writer.Flush(), the BaseStream advances to position 15.
No exception is thrown.
After the block executes, I cannot find the file using Windows Explorer.
It seems to me that the code is too simple to fail under normal circumstances. So that leads to my question: what is amiss with my circumstances? Why is it that the save appears to complete successfully, but the file is nowhere to be found? Security settings? The code itself? I'm at a loss.
Update
I've tried a few more things, and still no luck. I ran the application out-of-browser with the same symptoms, promoted the SaveFileDialog to a class variable. The application behaves like there is no error, but no file appears in the save location (my Documents folder, in Vista).
The Plot Thickens
I was stepping through with the debugger and found additional strange behavior. After the call to saveDialog.OpenFile(), the file appears at the target location. It remains after each statement, but is removed after the call to writer.Close(). Why on earth would the file be automagically deleted when the stream closes?
Thanks in advance for your help!

MEF + SL4 question

I'm working on an app in the Silverlight 4 RC and i'm taking the oppertunity to learn MEF for handling plugin controls. I've got it working in a pretty basic manor, but it's not exactly tidy and I know there is a better way of importing multiple xap's.
Essentially, in the App.xaml of my host app, I've got the following telling MEF to load my xap's:
AggregateCatalog catalog = new AggregateCatalog();
DeploymentCatalog c1 = new DeploymentCatalog(new Uri("TestPlugInA.xap", UriKind.Relative));
DeploymentCatalog c2 = new DeploymentCatalog(new Uri("TestPlugInB.xap", UriKind.Relative));
catalog.Catalogs.Add(c1);
catalog.Catalogs.Add(c2);
CompositionHost.Initialize(catalog);
c1.DownloadAsync();
c2.DownloadAsync();
I'm sure I'm not using the AggregateCatalog fully here and I need to be able to load any xap's that might be in the directory, not just hardcoding Uri's obviously....
Also, in the MainPage.xaml.cs in the host I have the following collection which MEF puts the plugin's into:
[ImportMany(AllowRecomposition = true)]
public ObservableCollection<IPlugInApp> PlugIns { get; set; }
Again, this works, but I'm pretty sure I'm using ImportMany incorrectly....
Finally, the MainPage.xaml.cs file implements IPartImportsSatisfiedNotification and I have the following for handling the plugin's once loaded:
public void OnImportsSatisfied()
{
sp.Children.Clear();
foreach (IPlugInApp plugIn in PlugIns)
{
if (plugIn != null)
sp.Children.Add(plugIn.GetUserControl());
}
}
This works, but it seems filthy that it runs n times (n being the number of xap's to load). I'm having to call sp.Children.Clear() as if I don't, when loading the 2 plugin's, my stack panel is populated as follows:
TestPlugIn A
TestPlugIn A
TestPlugIn B
I'm clearly missing something here. Can anyone point out what I should be doing?
Thanks!
I think most of what you are doing is fine. Although ObservableCollections do support notifications of individual elements being added and removed, MEF doesn't take advantage of this. In your case it will simply clear the collection and then add all the plugins. Since you are using OnImportsSatisfied for the change notification, you don't even need an ObservableCollection. You could just use an IEnumerable for your import.
To add flexibility in downloading different xaps, I would expose a service in your container that can be imported and that provides the functionality to download a xap given a url. Then any component in your container can trigger a download, and the url to download can come from whatever source you deem appropriate.