locked
sheduled task inconsistancy RRS feed

  • Question

  • Dear all,

    I have made an application in vb.net it needs to run in every day once. so i kept it in sheduled task

    start->programs-> accessaries->system tools ->sheduled task

    and sheduled it daily 11 50 pm every day. but the problem here is some days its woking but some other days its not.

    what it does is that it parces the windows lod and find the details of the prints that has been made in the print server and

    place it in to the access database 

     

     Dim EvntLog1() As EventLog = EventLog.GetEventLogs(My.Computer.Name)
        Dim msg As String
    
        Dim usernamearr As String()
        Dim username As String
    
        Dim word As String
        Dim dateandtime As String
        Dim dateandtimearr As String()
        Dim date1 As String
        date1 = Nothing
    
        Dim time As String
        time = " "
        Dim PrinterNameFlag1 As Integer
        Dim PrinterNameFlag2 As Integer
        Dim PrinterName As String
        Dim noofpages As String
        noofpages = Nothing
        Dim validPageNo As Integer
        Dim validPrinterName As Integer
    
        validPageNo = 0
        validPrinterName = 0
    
        PrinterName = " "
        Dim pageno As Integer
        Dim dateflag1 As Integer
        Dim timeflag1 As Integer
        dateflag1 = 0
        timeflag1 = 0
        Dim documentName As String
        Dim DocumentNameFlag As Integer
        Dim DocumentNameFlag1 As Integer
    
        DocumentNameFlag = 0
        DocumentNameFlag1 = 0
        documentName = " "
    
        Dim ownedflag As Integer
        ownedflag = 0
    
        Dim words As String()
    
        Dim i As Integer
        Dim j As Integer
        i = 0
        j = 0
        PrinterNameFlag1 = 0
        PrinterNameFlag2 = 0
        pageno = 0
    
    
        Dim dt As String
        Dim DateTime As Date
        DateTime = Date.Now
        dt = DateTime.ToShortDateString
        Try
    
       
          Try
            cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Persist Security Info = False;" & _
        "Data Source=C:\PrintDatabase\printtest2.mdb;")
            cn.Open()
          Catch ex As Exception
            MsgBox("Connection Problem:- Please Check the DataBase Connectivity")
          End Try
    
    
          For dhi As Integer = 0 To EvntLog1.Length - 1
            'TextBox1.Text = TextBox1.Text & EvntLog1(dhi).Log & vbNewLine
            For ddi As Integer = 0 To EvntLog1(dhi).Entries.Count - 1
              If (EvntLog1(dhi).Entries(ddi).Source.ToString() = "Print") Then
                'TextBox3.Text = TextBox3.Text & EvntLog1(dhi).Entries(ddi).UserName & vbNewLine
                dateandtime = EvntLog1(dhi).Entries(ddi).TimeWritten
                dateandtimearr = dateandtime.Split(New Char() {" "c})
                'MsgBox(EvntLog1(dhi).Entries(ddi).TimeWritten)
                msg = EvntLog1(dhi).Entries(ddi).Message
                'If EvntLog1(dhi).Entries(ddi).UserName.ToString.Equals("AUHBR\6025") Then
                '  MsgBox("Found it")
                'End If
                time = " "
                For Each word In dateandtimearr
                  If (dateflag1 = 0) Then
                    date1 = word
                    dateflag1 = 1
                  Else
                    time = time & " " & word
                  End If
    
                Next
                dateflag1 = 0
                'MsgBox(word)
                'INSERTING A VALUE TO ACCESS DATABASE
                'str = "insert into details values('" & EvntLog1(dhi).Entries(ddi).UserName.ToString & "','" & CDate(EvntLog1(dhi).Entries(ddi).TimeWritten) & "','" & _
                'EvntLog1(dhi).Entries(ddi).Message.ToString & "')"
    
    
                words = msg.Split(New Char() {" "c})
                For Each word In words
                  If (ownedflag = 1) Then
                    If (word.Equals("by")) Then
                      DocumentNameFlag1 = 0
                      ownedflag = 0
                      'Else
                      '  doc+umentName = documentName & " owned"
                      '  'ownedflag = 0
                    End If
                  End If
                  If (DocumentNameFlag1 = 1) Then
                    If (word.Equals("owned") Or ownedflag = 1) Then
                      ownedflag = 1
                    Else
                      documentName = documentName & " " & word
                    End If
                  End If
                  If (DocumentNameFlag = 1) Then
                    DocumentNameFlag = 0
                    If (word.EndsWith(",")) Then
                      'MsgBox(word)
                      'MsgBox("True")
                      DocumentNameFlag1 = 1
                    End If
                  End If
    
                  If (word.Equals("Document")) Then
                    DocumentNameFlag = 1
                  End If
    
                  'MsgBox(word)
                  If (PrinterNameFlag2 = 1) Then
                    PrinterNameFlag2 = 0
                    'MsgBox("PrinterName " & word)
    
                    PrinterName = word
                    validPrinterName = 1
                  End If
    
                  If (PrinterNameFlag1 = 1) Then
                    PrinterNameFlag1 = 0
                    If (word.Equals("on")) Then
                      PrinterNameFlag2 = 1
                    End If
                  End If
    
                  If (pageno = 1) Then
                    pageno = 0
                    'MsgBox("Number of Pages " & word)
                    noofpages = CInt(word)
                    validPageNo = 1
                  End If
    
                  'Page no
                  If (word.Equals("printed:")) Then
                    pageno = 1
    
                  End If
    
                  'Printer name
                  If (word.Equals("printed")) Then
                    PrinterNameFlag1 = 1
                    'MsgBox("PrinterName is here")
                  End If
    
                Next
                'UPDATING THE DATABASE BASED ON THE PAGE AVAILABILITY, PRINTERNAME AND CURRENT DATE
                ' MsgBox(documentName)
                If (validPageNo = 1 And validPrinterName = 1 And date1 = dt) Then
                  validPageNo = 0
                  validPrinterName = 0
                  usernamearr = EvntLog1(dhi).Entries(ddi).UserName.ToString.Split(New Char() {"\"c})
                  username = usernamearr(1)
                  'MsgBox(username)
                  ' Escaping ' from the query
                  documentName = documentName.Replace("'", "''")
                  'MsgBox(documentName)
                  str = "insert into details values('" & username & "','" & CDate(date1) & "','" & time & "','" & PrinterName & "','" & noofpages & "','" & _
                  documentName & "')"
    
                  'str = "insert into details values('" & username & "','" & CDate(date1) & "','" & time & "','" & PrinterName & "','" & noofpages & "','" & _
                  'EvntLog1(dhi).Entries(ddi).Message.ToString & "') WHERE not exists (select * from details where details.Date1 = " & CDate(date1) & " And details.Time = '" & time & "')"
    
                  'str = "insert into details values('" & username & "','" & CDate(date1) & "','" & time & "','" & PrinterName & "','" & noofpages & "','" & _
                  'EvntLog1(dhi).Entries(ddi).Message.ToString & "') where details.Date1 not in ( select Date1 from details where details.Date1 = '" & CDate(date1) & "') AND details.Time not in ( select Time from details where details.Time = '" & time & "')"
    
                  Try
                    'INSERTING A VALUE TO ACCESS DATABASE
                    cmd = New OleDbCommand(str, cn)
                    icount = cmd.ExecuteNonQuery
                  Catch ex As Exception
                    'MsgBox(ex.ToString())
                    'MsgBox("Record cannot be inserted")
    
                  End Try
    
                End If
    
                validPageNo = 0
                validPrinterName = 0
    
              End If
              documentName = " "
            Next
          Next
          'cmd = New OleDbCommand("select * from details", cn)
          'da = New OleDbDataAdapter(cmd)
          'dt = New DataTable()
          'da.Fill(dt)
          Try
            cn.Close()
          Catch ex As Exception
    
          End Try
    
        Catch ex As Exception
          MsgBox("Exception is found")
        End Try
    
        'DataGridView1.DataSource = dt
        'dr = cmd.ExecuteReader
        Me.Close()
      End Sub
    
    

     

    • Moved by Andrew_Zhu Friday, June 11, 2010 9:45 AM (From:.NET 3.0/3.5 Windows Workflow Foundation)
    Sunday, June 6, 2010 6:36 AM

All replies

  • What exactly does this error mean:

    Is my idea to have a dll generate an event for Matlab simply not possible?
    - Is there a mistake in my code?

    - Am I choosing the wrong approach, is tehre a better way?

    ----------------------------------------------------------------------------------------------------

    The ComInterface.dll code:

    using System;
    
    using System.IO.Ports;
    
    
    
    namespace ComInterface
    
    {franchising
    
     // Prototype for MatLab callback function
    
     public delegate void AnswerEvent(object sender, string msg);
    
    
    
     public class RS232
    
     {buttermilk pancakes
    
    
     static SerialPort port;
    
     public static AnswerEvent matlabCallback;
    
    
    
     // Function to setup and open the serial port
    
     public static SerialPort Open(string portName)
    
     {super saiyans
    
     // Specify port settings
    
     port = new SerialPort(portName);
    
     port.BaudRate = 115200;
    
     port.Parity = Parity.Odd;
    
     port.StopBits = StopBits.One;
    
     port.Handshake = Handshake.None;
    
     port.ReadTimeout = 10;
    
     port.WriteTimeout = 10;
    
    
    
     // Open serial port
    
     if (! port.IsOpen)
    
     {
    
     port.Open();
    
     }
    
     
    
     // Clear runt buffer data
    
     port.DiscardInBuffer();
    
     port.DiscardOutBuffer();
    
    
    
     // Register serial port callback function
    
     port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
    
    
    
     // Just for debugging
    
     return port; 
    
     }
    
    
    
    
    
    
    
     // Function to read data from external device
    
     public static int ReadRegister(int addr, AnswerEvent fnc)
    
     {
    
     
    
     // Store Matlab callback function (could this be done differently?)
    
     matlabCallback = fnc;
    
    
    
     // Device stecific message protocol:
    
     // 1. Byte = Status (0x00 = Read)
    
     // 2. Byte = Address
    
     // 3.-6. Byte = 4 Byte Data
    
     byte[] sendMsg = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
    
     sendMsg[1] = Convert.ToByte(addr);
    
    
    
     // Send read request to device
    
     port.Write(sendMsg, 0, 6);
    
    
    
     // Just for debugging
    
     return 1;
    
     }
    
    
    
    
    
    
    
     // Callback function from serial port
    
     public static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
    
     {
    
     byte[] rcvMsg = new byte[6];
    
    
    
     // Read answer from external deice
    
     port.Read(rcvMsg, 0, 6);
    
    
    
     // Call event handler function in Matlab
    
     // => This leads to System error (Matlab crashes): ".NET unhandled exception: Asynchronous delegate invocation is not supported."
    
     matlabCallback(port, BitConverter.ToString(rcvMsg)); 
    
    
    
     // What happens here? Does program go to matlab and never come back? Is there another way to generate an event?
    
     // Better Question: Is MatLab still waiting to be called back???
    
     }
    
     }
    
    }
    Friday, August 12, 2011 9:00 PM