none
Unable to run Service thread for long time RRS feed

  • Question

  • Hi All,


    I am currently working on file watcher system in windows service. When ever user uploads any type of media file my service will convert the uploaded media to ".SWF" format. To convert this format i am using FFMPEG Codec.

    The Problem is when ever i upload media of size less than 900kb it is working fine. The service converting it to swf format. However when i upload file greater than this like 2mb , then my service is not working here(I mean file creating with 0kb size).

    can u ppl help me how to run the service with the maximum size of fiules.

    I have written thread also , but still the same prob.

    Here is the code.

    private void fwService_Created(object sender, System.IO.FileSystemEventArgs e)

    {

    FilePath = e.FullPath;

    if (FilePath.Contains(".wmv") || FilePath.Contains(".avi"))

    {

    ThreadStart job = new ThreadStart(RunJob);

    Thread thread = new Thread(job);

    thread.Start();

    }

    }



    public void RunJob()

    {

    string FullPath = FilePath;

    if (FullPath.Contains(".wmv") || FullPath.Contains(".avi"))

    {

    string inputfile, outputfile, filargs;

    string withoutext;

    if (FullPath.Contains(".wmv"))

    {

    withoutext = FullPath.Replace(".wmv", "");

    }

    else

    {

    withoutext = FullPath.Replace(".avi", "");

    }

    inputfile = FullPath;

    outputfile = withoutext + ".swf";

    filargs = "-i " + inputfile + " -ar 22050 " + outputfile;

    Process proc;

    proc = new Process();

    proc.StartInfo.FileName = "D:\\ffmpeg\\ffmpeg.exe";

    proc.StartInfo.Arguments = filargs;

    proc.StartInfo.UseShellExecute = false;

    proc.StartInfo.CreateNoWindow = false;

    proc.StartInfo.RedirectStandardOutput = false;

    try

    {

    proc.Start();

    proc.WaitForExit();

    proc.Close();

    proc.Dispose();

    }

    catch (Exception ex)

    {

    //Response.Write(ex.Message);

    }

    // File.Delete(FilePath);

    GC.Collect();

    }

    else

    {

    }

    }

    }

    any help how to work when file size is more?


    Regards
    Sith


    sithender
    Sunday, March 15, 2009 2:40 PM

All replies

  • The FileSystemWatcher events will run while the file is being uploaded, not after it is done uploading.  Your conversion program will try to open the file before the upload is completed.  Or when it is still locked.  Note how that issue is related to file size, a larger file will take longer to upload.

    You'll have to delay running the utility until the upload is completed.  One possible way is to try to open the file.  If the software used to upload the file puts a lock on the file, it should, your attempt to open it will fail.  Retry periodically.  If you don't get a lock exception, you can't do much but arbitrarily delay.  Reset the timer when you see the file size change or you get additional FSW notifications.

    Hans Passant.
    Sunday, March 15, 2009 3:44 PM
    Moderator
  • Hello,


    Thanks for your reply. based on your reply i have changed my code littile bit . 

     

    private void WaitToLoad(string fileName, int milliseconds)

    {

    DateTime dtStart = DateTime.Now;

    TimeSpan ts = new TimeSpan(0);

    while (true)

    {

    try

     

    {

    traceMyProgram("trying to open the file created");

    ts = DateTime.Now.Subtract(dtStart);

    FileStream fs = new FileStream

     

    (fileName, FileMode.Open, FileAccess.Read, FileShare.None);

    fs.Close();

    traceMyProgram("opened the file created");

    return;

    }

    catch (FileNotFoundException)

    {

    traceMyProgram("file not found");

    throw;

    }

    catch (IOException exc)

    {

    traceMyProgram("IO Exception" + ts.Milliseconds.ToString() + " - " + milliseconds.ToString());

    if (ts.Milliseconds > milliseconds)

    {

    traceMyProgram("file not loaded");

    throw new Exception(exc.Message);

    //throw new FileNotLoaded(fileName, milliseconds, exc);

     

    }

    Thread.Sleep(1000);

    continue;

    }

    catch (Exception exc)

    {

    traceMyProgram(exc.Message);

    Thread.Sleep(1000);

    continue;

    }}}

    To this method i am passing the newly created filename and time to wait. once this method successfully opens file then only i am running the new process. even this also not working for me.
    Do you have any idea or links , which can help me to run a service which converts media files to swf format. I need the code to be run using when ever a new filr is created in my c:\upload folder.                                                                                                                               Thanks & Regards                                                                                                               Sith


    sithender
    Monday, March 16, 2009 11:29 AM