none
FORM PLOBLEM RRS feed

  • Question

  • I have created a form that allows a given task to be executed once it is opened,

    but when it is minimized the task is not executed anymore.

    I'm asking for your help .

    I want now that when I minimize my Form the task continues to run.
    here is my code that does not meet my expectations


    PrivateSubForm1_Load(sender AsObject, e As EventArgs)HandlesMyBase.Load
    
        
            Me.Activate()EndSubPrivateSubForm1_Activated(ByVal sender AsObject, _
    ByVal e As System.EventArgs)HandlesMyBase.Activated
    
         'Here is the code that executes the task
    
    EndSub




    Sunday, May 20, 2018 9:34 AM

All replies

  • Hello,

    Please edit your post so that your actual question is outside the code block and secondly, redo the code block as it seems you pasted it in a strange manner.

    Best thought on the issue is the form is deactivated when you minimize the form. Rather than calling Me.Active in form load which is incorrect if the intent is to start some code when the form displays use the Shown event of the form rather than Activate.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, May 20, 2018 9:44 AM
    Moderator
  • I want you to help me keep the form active even if it is minimized
    Sunday, May 20, 2018 10:23 AM
  • Yes the form loads only once (or it should be closed and really be released which sometimes takes some time).

    Use the Activated event. 

    https://msdn.microsoft.com/en-us/library/system.windows.forms.form.activated(v=vs.110).aspx


    Success
    Cor

    Sunday, May 20, 2018 10:24 AM
  • I want you to help me keep the form active even if it is minimized

    A minimized form is never active.  The "active" form is the one currently with focus.

    Perhaps you want a timer or background thread to execute your code.

    You will need to give us some idea of what task you actually want to perform, as this will likely determine the best method of executing it.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Sunday, May 20, 2018 12:37 PM
    Moderator
  • in fact, the FORM acts as a server and automatically sends a message to all client applications. and I want it to continue this task even if it is minimized

    this is a task

     
     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.MaximizeBox = False
            Me.Activate()
            MakeOnTop()
            monserveur = New TcpListener(IPAddress.Parse(TextBox2.Text), Integer.Parse(textport.Text))
            LesClients = New List(Of TcpClient)
            monserveur.Start()
            Dim context As TaskScheduler = TaskScheduler.FromCurrentSynchronizationContext()
            Task.Factory.StartNew(Sub() accepter(context), CancellationToken.None, TaskCreationOptions.LongRunning)



        End Sub

        Private Sub Form1_Activated(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles MyBase.Activated

            Try

                If (RichTextBox1.Text IsNot "") Then
                    Dim LeMessage As String = RichTextBox1.Text
                    Dim buffer() As Byte = Encoding.UTF8.GetBytes(LeMessage)


                    For i As Integer = LesClients.Count - 1 To 0 Step -1
                        LesClients(i).GetStream().Write(buffer, 0, buffer.Length)

                    Next


                End If

            Catch ex As Exception

            End Try
            RichTextBox1.Text = ""
        End Sub



    Sunday, May 20, 2018 3:40 PM
  • Then the task you start needs to be a long running process that does not exit.  It should contain a loop which continues to execute until some other code ends it.

    This sounds like a process for a Service application, rather than a Forms application.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Sunday, May 20, 2018 3:44 PM
    Moderator
  •  yes it is a tcp / ip , socket or service application that already communicates with the client applications

    what do you finally suggest to me?

    Sunday, May 20, 2018 4:13 PM
  • Something along the lines of:

        Private _IsRunning As Boolean = True
        Private Async Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'setup your connection
            Do While _IsRunning = True
                'use async await TCP methods for connections
                'after a client connects, begin client processing task
            Loop
            Me.Close()
        End Sub
    
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            If _IsRunning Then
                _IsRunning = False
                e.Cancel = True
            End If
        End Sub
    You might want to review this code sample for more information (look at Form1 in AsyncTcpSample).  You can use your current communications protocol instead of the one defined in the sample.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Sunday, May 20, 2018 9:14 PM
    Moderator
  •  yes it is a tcp / ip , socket or service application that already communicates with the client applications

    what do you finally suggest to me?

    I don't know how to write a service but that would be what you would probably want to do for running a service. And then you would need some kind of app that could comminicate in some fashion with the service possibly as a UI.

    Also see Introduction to Windows Service Applications and for the UI to communicate with the service Service​Controller Class.

    On the other hand what exactly is the app you created to be used for? A chat application or some type of notification application to other systems?


    La vida loca


    Monday, May 21, 2018 2:36 AM