Please see the answer to the following question: Can't specify the 'async' modifier on the 'Main' method of a console app
I am trying to do this with a VB.NET program. I have added the package using NUGET and I have ensured that the Reference is added. Please see the code below:
Imports Nito.AsyncEx
Public Class ScheduledTasks
Private Shared Async Sub MainAsync(args As String())
Dim bs As New Bootstrapper()
Dim list As VariantType = Await bs.GetList()
End Sub
End Class
The error is: Type BootStrapper is not found. I have used Intellisense to look at the types contained in Nito.AsyncEx and Bootstapper is not there? How do I create an asynchronous main method using VB.NET?
Bootstrapper is not a part of AsyncEx. AsyncContext is:
Imports Nito.AsyncEx
Public Class Program
Private Shared Sub Main(args As String())
AsyncContext.Run(Function() MainAsync(args))
End Sub
Private Shared Function MainAsync(args As String()) As Task
...
End Function
End Class
Related
I've created my own private NuGet server and hosted two packages written in VB, one with a single public class and one with a Module containing some extension methods. When I reference the packages in my application, I am able to create a new instance of the class from the package, but I am unable to use any methods declared in the module. I know that modules need to be contained withing the namespace, so I have a feeling I may need to reference it somewhere to make use of it. Does anyone know what I need to do? Thanks.
I've currently got the following:
Namespace TestHelperNamespace
Public Class TestHelper
Public Sub DoSomething()
End Sub
End Class
Public Module TestModule
Public Sub StringSub(s As String)
End Sub
End Module
End Namespace
Import the Module's namespace in your code
Imports NugetModuleNamespace
Here's a MCVE
Imports Namespace2
Namespace Namespace1
Module Module1
Private Sub foo()
Dim a = 1.23#
Dim b = a.Square() ' doesn't work without Imports
End Sub
End Module
End Namespace
Namespace Namespace2
Module Module2
<System.Runtime.CompilerServices.Extension>
Public Function Square(value As Double) As Double
Return value ^ 2
End Function
End Module
End Namespace
This applies to Modules in separate files as well.
I wrote a module and into which a Public Sub Main method. But, when I run the program. It gives " No accessible 'Main' method with an appropriate signature was found in 'abc'."
Could you please suggest possible solutions to the error.
Public Sub Main(ByVal cmdArgs As String)
Dim returnValue As Integer
If cmdArgs.Length > 0 Then
returnValue = Import_Start(cmdArgs, "f9880")
Console.WriteLine("Import end with an error " & returnValue)
Else
Console.WriteLine("parameter failure")
End If
End Sub
End Module
If you want to start your app from a Sub Main, the correct signature is:
Public Sub Main(args As String())
' or
Public Sub Main()
The command line args will be passed as a string array. Yours just declares it as String resulting in the compiler error. You also need to set the StartUp object to Sub Main in Project Properties, but that already seems to have been done.
If you do not want/need to use a module you can add it to a form (it is not clear if this is even a WinForms app) using:
Public Shared Sub Main(args As String())
' or
Public Shared Sub Main()
I want to make the function "createFolder" faster (or at least not blocking my main thread), by adding a new callback to my threadpool.
I marked the main function with the STAThread() and the exception tells me to mark my main function with the STAThread().
I'm open to any tips!
You have placed the STAThread attribute on the wrong method: it needs to be the method that the form is started from, not the method you are executing.
In many cases, your application will have a Sub Main and this is what needs to be decorated with the STAThread attribute. An example from MSDN:
Public Class MyForm
Inherits Form
Public Sub New()
Me.Text = "Hello World!"
End Sub 'New
<STAThread()> _
Public Shared Sub Main()
Dim aform As New MyForm()
Application.Run(aform)
End Sub
End Class
Hello I'm working on using the interface class in vb.net to make a plugin architecture. So far I've not found a step by step tutorial for a beginner. But I've gotten as far as this:
Main App
Public Class PluginHandler
Interface IApplications
Sub ChangeForms()
End Interface
Public Shared Sub GetDLLFromDir(ByVal TheDir)
For Each dll As String In System.IO.Directory.GetFiles(TheDir, "*.dll")
LoadPlugin(dll)
Next
End Sub
Public Shared Sub LoadPlugin(ByVal ThePlugin)
Dim Asm = Assembly.LoadFile(ThePlugin)
Dim type As Type = Asm.GetType("TestPlugin.Class_TestPlugin")
Dim method As MethodInfo = type.GetMethod("ChangeForms")
method.Invoke(Nothing, Nothing)
End Sub
End Class
TestPlugin.vb
Public Class Class_TestPlugin
Implements Plugin_Application.PluginHandler.IApplications
Sub ChangeForms() Implements Plugin_Application. _
PluginHandler.IApplications.ChangeForms
Dim NewForm As New Form_Test
NewForm.Show()
End Sub
End Class
My issue is it says for the method to invoke --> Non-static method requires a target.
I seen on another forum that the method may not be found. I found it says the methods name and void. But I'm not sure what to do. If somebody can mod my code to work or give me some ideas to make my code work. Thanks :)
Here is a link to my test project folder: Link
You have to create an object the method should be called on:
Dim ctor = type.GetConstructor({}) 'no parameters for constructor
Dim obj = ctor.Invoke({})
method.Invoke(obj, {})
I hope this isn't a stupid question, I can't find a reasonable answer on google.
I'm starting a project which only contains one class file. I will be turning the class file into a dll at the end. I understand that another app normally makes calls to the dll once it's referenced in the project. I need the dll to run a sub inside of it on load like a normal mybase.load sub. This sub needs to execute only once on load to populate some variables. I don't want to have to call the sub from the main app. The rest of the functions/subs in the dll will be called from the main app when needed. Please don't respond with register them globally under the class, I need a sub or function.
If there isn't such a sub how would I go about creating a function/sub that preforms an onload?
Thanks. :)
Hope I'm making sense. Thanks for your response.
Shared Sub New()
on your class.
Another option is to have a private class inside your class and initialise it with a member variable:
Public Class MyLibraryClass
Private mobjSettings As New SettingsClass
Public Function SampleLibraryFunction() As String
Return mobjSettings.SettingsProperty
End Function
Private Class SettingsClass
Friend SettingsProperty As String
Sub New()
'initialise
SettingsProperty = "This is a test"
End Sub
End Class
End Class