-1

I have done a simple VB application with this code:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim procName As String = Process.GetCurrentProcess().ProcessName
        Dim processes As Process() = Process.GetProcessesByName(procName)

        If processes.Length > 1 Then
            Process.GetProcessesByName("keyinput")(0).Kill()
        End If

    End Sub


    Public Sub type(ByVal int As Double, str As String)
        For Each c As Char In str
            SendKeys.Send(c)
            System.Threading.Thread.Sleep(int * 1000)
        Next
    End Sub

    Sub vai()
        Dim line As String = ""
        If File.Exists("trans.txt") Then
            Using reader As New StreamReader("trans.txt")
                Do While reader.Peek <> -1
                    line = reader.ReadLine()
                    type(0.155, line)
                    'SendKeys.Send(line)
                    SendKeys.Send("{ENTER}")
                Loop
            End Using

            File.Delete("trans.txt")
        End If
    End Sub


    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        vai()
    End Sub

Basically the timer in it check if a file exists, read it and type the content simulating the keyboard. I want this exe to start automatically when user login, it does it, apparently, I can see the form1 pop up but doesn't really works. Everyting is fine only if I run it manually by double-clicking the icon. Why and what can I do? Thanks

ps. i already tried to execute it with windows task manager, or putting a shortcut in the windows startup folder, or calling it from a cmd

EDIT: when app starts automatically , process is running, but windows form is showing like this enter image description here

Instead starting manually is showing like this: enter image description here

  • You really ought to put some thought and effort into naming your methods and everything else. `type` and `vai` are both terrible names for methods. – jmcilhinney Jul 12 '21 at 08:18
  • 1
    "doesn't really work" isn't an error description. WHAT doesn't work? Did you specify the correct working directory so that it can find the file? Does the file exist? ... – derpirscher Jul 12 '21 at 08:25
  • @derpirscher if I know what the problem is I would'nt ask for help, would I? I can just say that if I start the exe automatically it seems "hang" or half-started, even the default app icon is not showing properly in the windows application bar, instead if I start it manually I can see the default coloured VB form icon. I don't know how can I put this differently, there is no error message and, as said, the code works because do what is supposed to do when started manually – Gabriele Cozzolino Jul 12 '21 at 09:18
  • The fact that you don't know what the problem is does not mean that you can't make an effort to find out or at least make an effort to provide us with as much information as possible. Did you bother to put any logging code in the application to tell you where it gets up to, what course execution takes and what data is in use at the time? Apparently not. Did you bother to tell us exactly how you're achieving the app being run at startup? Apparently not. You're a developer, not a user, so you should be a developer. – jmcilhinney Jul 12 '21 at 09:44
  • Obviously you don't know, what the problem is, and that's ok. But "doesn't work" isn't a valid problem description. Imagine calling your mechanic and just saying "my car isn't working" what would he be able to conclude from that? Could be anything from dead battery, over missing fuel to a serious damage in the engine ... – derpirscher Jul 12 '21 at 09:44
  • @jmcilhinney you're right, what can I do to log this correctly? I think it's something to do with how windows starts the app in a way or another, or some conflict with other auto-starting apps, and not of my code – Gabriele Cozzolino Jul 12 '21 at 10:18
  • I don't intend this to sound disrespectful but it will anyway: you don't know what you're talking about. Based on my over 20 years as a developer and even more as a Windows user, I'm fairly certain the issue is what I described below. Unless you have a better understanding of how the current directory works than seems apparent or what your app is doing than you have described, you're not qualified to say it's not. You should follow the advice I have provided and only look further if that doesn't help. – jmcilhinney Jul 12 '21 at 10:41
  • OK, I read the question again and maybe I have misinterpreted. What is the actual issue? Is it that the form isn't displayed or that the file isn't processed? I was under the impression that it was the latter but the former is a different matter. If it is the former, please provide a full and clear explanation, which includes exactly how you're starting the app. – jmcilhinney Jul 12 '21 at 10:45
  • For the record, you should still do as I suggested in the answer below, i.e. specify the full file path, even if it is working in this case. It's also possible that there's more than one problem and that will fix one of them but there's another issue related to the form not displaying. – jmcilhinney Jul 12 '21 at 11:51
  • @jmcilhinney the code is WORKING, when I start the app manually it does what is should: read a text file and type it simulating keyboard key-press. So the problem IS NOT my code, it's something about how Windows manage processes started at user logon. The app started automatically this way simply works stragely: it appears in the processes list, but the visual part (icon in the app bar and form) looks "incomplete" and in this strange state the app don't do what is supposed to. But I guess it's just because isn't really started properly – Gabriele Cozzolino Jul 12 '21 at 12:51

2 Answers2

0

I don't know this for a fact but I suspect that the issue is the fact that you are not specifying the location of the file. If you provide only the file name then it is assumed to be in the application's current directory. That current directory is often the folder that the EXE is in but it is not always and it can change. DO NOT rely on the current directory being any particular folder. ALWAYS specify the path of a file. If the file is in the program folder then specify that:

Dim filePath = Path.Combine(Application.StartupPath, "trans.txt")

If File.Exists(filePath) Then
    Using reader As New StreamReader(filePath)

EDIT:

If you are running the application at startup by adding a shortcut to the user's Startup folder then, just like any other shortcut, you can set the working directory there. If you haven't set the then the current directory will not be the application folder and thus a file identified only by name will not be assumed to be in that folder.

If you are starting the app that way (which you should have told us in the question) then either set the working directory of the shortcut (which is years-old Windows functionality and nothing to do with VB.NET) or do as I already suggested and specify the full path when referring to the file in code. Better yet, do both. As I already said, DO NOT rely on the current directory being any particular folder, with this being a perfect example of why, but it still doesn't hurt to set the current directory anyway if you have the opportunity.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • I don't think this is the problem, the trans.txt file IS in the exe directory and the code WORKS when I start the exe manually – Gabriele Cozzolino Jul 12 '21 at 09:20
  • @GabrieleCozzolino, so you're complaining in a comment on the question that someone is expecting you to know what the problem is but now we're supposed to care what you think the problem isn't? Maybe take the advice of someone who's been doing this for over 20 years and try my suggestion. – jmcilhinney Jul 12 '21 at 09:40
  • I'm not complaining, check my original question, info you're looking for was there from beginning. I'll try to specify folder – Gabriele Cozzolino Jul 12 '21 at 10:16
0

It was a Windows task scheduler fault, that for some reason didn't executed the exe correctly at logon. I've solved the issue by using Task Till Down and everything works fine now.