Click here to Skip to main content
15,891,409 members
Home / Discussions / Visual Basic
   

Visual Basic

 
SuggestionRe: bouton imprimer DGV complet Pin
Richard Deeming25-Jan-16 5:15
mveRichard Deeming25-Jan-16 5:15 
GeneralRe: bouton imprimer DGV complet Pin
Dorsaf Ouersighni25-Jan-16 20:45
Dorsaf Ouersighni25-Jan-16 20:45 
QuestionXML Datagridview control & saving changes Pin
Dan Chapin25-Jan-16 3:40
Dan Chapin25-Jan-16 3:40 
QuestionSetting Primary Display Pin
Jayme6518-Jan-16 23:33
Jayme6518-Jan-16 23:33 
AnswerRe: Setting Primary Display Pin
Richard Andrew x6419-Jan-16 4:39
professionalRichard Andrew x6419-Jan-16 4:39 
GeneralRe: Setting Primary Display Pin
Jayme6519-Jan-16 6:46
Jayme6519-Jan-16 6:46 
GeneralRe: Setting Primary Display Pin
Richard Andrew x6419-Jan-16 8:05
professionalRichard Andrew x6419-Jan-16 8:05 
QuestionThread vs Task, seeking advice before I write code, design point of view. Pin
jkirkerx14-Jan-16 7:49
professionaljkirkerx14-Jan-16 7:49 
So I'm working on my Windows Service that sends email, in which I can read and write the parameters needed for it to communicate now.

I rewrote the service from scratch, and made 3 thread classes.
So the service runs Thread1 to test for communications and file I/O success.
Next Thread2 runs and check for jobs, if there are no jobs, it just sleeps for 15 minutes and checks again.
Finally, the jobs must be sent by Thread3 or the Await Task.

I've never done this before, this is my first time, and I want to get it right.
I did read an article about race conditions, so I can avoid that, and the principles of Threading
http://visualbasic.about.com/od/usingvbnet/a/threadingintro.htm[^]

Thread1 - Test for Communications, Sort of a systems check, Service Stops on failure.
Thread2 - Check for Jobs, if jobs exist, send email
Thread3 - Send Email
Await Task - Send Email

Now if there are jobs, I load the job and run it.

Question:
Is is stupid to load Thread3 to send the job from Thread2? Another Thread called from a Thread.
I did figure out how to stop the threads on shutdown or power down, and sleep. But I'm not sure what the best design would be.

Or should I just use Thread2 to load an Await Task, and have Thread2 monitor the progress, and wait for the Task to complete. Then Thread2 can load the next job if it exist and repeat the process again.

[Edit]
Now I have another question:
I have this thread base code that seemed pretty practical, used an online example.
I placed my job checking code in RunScheduler, and was going to place code to send the emails in StartWorkerThread. I don't get the RunPayLoad. What is Payload Code?
Public Class serviceThread_Jobs_BaseCode

    Public Shared ServiceThread As Thread
    Public Shared ThreadRunning As Boolean = True
    Public Shared KeepRunning As Boolean = True

    Private Shared PollingInterval As Integer = My.Settings.PollingInterval
    Private Shared ActionInterval As Integer = My.Settings.ActionInterval

    Public Shared Sub Thread_Start()

        ServiceThread = New Thread(AddressOf RunScheduler)
        ServiceThread.Name = "serviceThread_Jobs_BaseCode"
        ServiceThread.Start()

        KeepRunning = True
        custom_eventLog.WriteToEventLog("serviceThread_Jobs_BaseCode - Thread_Start() has been called", 7100, EventLogEntryType.Information)

    End Sub
    Public Shared Sub Thread_Stop()

        KeepRunning = False

        custom_eventLog.WriteToEventLog(
            "serviceThread_Jobs_BaseCode is stopping. " & vbCrLf & _
            "Please note: the worker process will live on for up to " & _
            PollingInterval / 1000 & " seconds before it terminates.",
            7100
        )

    End Sub
    Private Shared Sub RunPayload(ByVal objInput As Object)

        ' Write your service payload code and/or function calls here. For example:
        Try

            custom_eventLog.WriteToEventLog("serviceThread_Jobs_BaseCode - payload execution: " & objInput, 7100)

        Catch e As Exception

            custom_eventLog.WriteToEventLog("serviceThread_Jobs_BaseCode - payload execution error: " & e.Message & vbCrLf, 7200)

        End Try

    End Sub
    Private Shared Sub RunScheduler()

        Dim LastActionTime As Date = DateAdd(DateInterval.Day, -1, Date.Now)
        Do While KeepRunning

            If DateDiff(DateInterval.Second, LastActionTime, Date.Now) >= ActionInterval Then

                LastActionTime = Date.Now

                'check for posted jobs

                StartWorkerThread()

            End If

            Thread.Sleep(PollingInterval)

        Loop

    End Sub
    Private Shared Sub StartWorkerThread()

        'Check for the Thread Stop Command and Stop Running
        If (True = KeepRunning) Then

            Dim WorkerThread As Thread = New Thread(AddressOf RunPayload)

            ' A parameter value can be transferred to the payload function via Start():
            WorkerThread.Start("Hello world " & Date.Now.ToString("yyyy-MM-dd HH:mm:ss"))

        Else

            ThreadRunning = False

        End If

    End Sub

End Class


modified 14-Jan-16 14:09pm.

GeneralRe: Thread vs Task, seeking advice before I write code, design point of view. Pin
Sascha Lefèvre14-Jan-16 8:10
professionalSascha Lefèvre14-Jan-16 8:10 
GeneralRe: Thread vs Task, seeking advice before I write code, design point of view. Pin
jkirkerx14-Jan-16 8:33
professionaljkirkerx14-Jan-16 8:33 
AnswerRe: Thread vs Task, seeking advice before I write code, design point of view. Pin
Sascha Lefèvre14-Jan-16 9:40
professionalSascha Lefèvre14-Jan-16 9:40 
GeneralRe: Thread vs Task, seeking advice before I write code, design point of view. Pin
jkirkerx14-Jan-16 11:06
professionaljkirkerx14-Jan-16 11:06 
AnswerRe: Thread vs Task, seeking advice before I write code, design point of view. Pin
Wombaticus15-Jan-16 0:05
Wombaticus15-Jan-16 0:05 
GeneralRe: Thread vs Task, seeking advice before I write code, design point of view. Pin
jkirkerx15-Jan-16 6:06
professionaljkirkerx15-Jan-16 6:06 
GeneralRe: Thread vs Task, seeking advice before I write code, design point of view. Pin
Wombaticus15-Jan-16 6:19
Wombaticus15-Jan-16 6:19 
GeneralRe: Thread vs Task, seeking advice before I write code, design point of view. Pin
jkirkerx15-Jan-16 7:13
professionaljkirkerx15-Jan-16 7:13 
GeneralRe: Thread vs Task, seeking advice before I write code, design point of view. Pin
Wombaticus15-Jan-16 7:38
Wombaticus15-Jan-16 7:38 
GeneralRe: Thread vs Task, seeking advice before I write code, design point of view. Pin
jkirkerx15-Jan-16 9:00
professionaljkirkerx15-Jan-16 9:00 
GeneralRe: Thread vs Task, seeking advice before I write code, design point of view. Pin
Wombaticus16-Jan-16 7:22
Wombaticus16-Jan-16 7:22 
GeneralRe: Thread vs Task, seeking advice before I write code, design point of view. Pin
jkirkerx17-Jan-16 6:59
professionaljkirkerx17-Jan-16 6:59 
QuestionHow to alert checked items from checkedlistbox Pin
Daniel Elmnas11-Jan-16 2:46
Daniel Elmnas11-Jan-16 2:46 
AnswerRe: How to alert checked items from checkedlistbox Pin
Richard Deeming11-Jan-16 3:57
mveRichard Deeming11-Jan-16 3:57 
QuestionWindows service and a Form program, serializing data stored in a file, shared between the 2 programs Pin
jkirkerx9-Jan-16 13:29
professionaljkirkerx9-Jan-16 13:29 
AnswerRe: Windows service and a Form program, serializing data stored in a file, shared between the 2 programs Pin
Dave Kreskowiak9-Jan-16 18:17
mveDave Kreskowiak9-Jan-16 18:17 
GeneralRe: Windows service and a Form program, serializing data stored in a file, shared between the 2 programs Pin
jkirkerx10-Jan-16 7:33
professionaljkirkerx10-Jan-16 7:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.