ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Other Software > Developer's Corner

Forms calls a class, class interacting with a form

<< < (2/2)

f0dder:
CW: you don't get a leak from #1 unless you have a really unusual GUI design :)

Amen to #2, though. MVC 4tw.

CWuestefeld:
CW: you don't get a leak from #1 unless you have a really unusual GUI design :)
-f0dder (November 22, 2008, 12:58 PM)
--- End quote ---

In .Net WinForms (which I assume is the environment, judging by the VB syntax), you will have a leak. This must be the case in any garbage-collected environment. In this example, the TextBox (and hence any handles it's using, too) cannot be collected while there are any live objects referencing them. The reference from MyClass will hold any referenced TextBox in memory until the MyClass object itself is collected.

I meant to offer a more concrete suggestion as a worthwhile and instructive step before jumping into MVC.

Declare an Event in your Class, and let your form listen for that event. This is easy to do: syntactically it's just like when your form waits for a button click or something like that. The advantage is that MyClass doesn't need to have the faintest idea of how the UI works. He just needs to shout out by way of the Event: "Hey! I've got a new value for myname (or whatever)!".

The same caveats about leaks apply, though. When you subscribe to the Event, you're holding a reference to the MyClass instance. That instance can't be garbage collected until the reference is cleared, either explicitly or by the death of the form that's listening to it.

mediaguycouk:
Something that I have learnt, which I havn't followed here, is that I shouldn't ask direct questions. I should say what I'm doing and ask for general advise.

Here is what I'm doing...

I have a front end interface for ffmpeg. Ffmpeg is a command line video encoding. The class in this case is Ffmpeg.cs, which runs all the arguements


--- Code: C# ---using System;using System.Collections.Generic;using System.Diagnostics;using System.IO;using System.Text;using System.Windows.Forms; namespace H264Convert{    class Ffmpeg    {        // Where FFMPEG should be        // This is also in Form1.cs        public string ffmpegLocation = @"C:\program files\ffmpeg\ffmpeg.exe";         // Create a new process        System.Diagnostics.Process proc = new System.Diagnostics.Process();         // Function to actually envoke FFMPEG        public void runFFMPEG(string inputFile, string outputFile, string aspectRatio,            string dimensions, string bitRate, string passes, string videoTypeArg,            string audioFrequency, string audioBitRate, string deinterlace, string otherArgs, string audioCodec)        {            // See if the video to be output already exists            if (File.Exists(outputFile))            {                                // and stop FFMPEG from starting                return;            }            // Create arguements from passed variables            string arguement = " -i \"" + inputFile + "\"" +                " -aspect " + aspectRatio +                " -pass " + passes +                " " + videoTypeArg +                " -b " + bitRate +                " -ar " + audioFrequency +                " -ab " + audioBitRate +                " -acodec " + audioCodec +                 " -s " + dimensions +                " " + deinterlace +                " " + otherArgs +                " " + "-y" + // Overwrite                " \"" + outputFile + "\"";             // Stop proc raising events            proc.EnableRaisingEvents = false;            // Location of file to start (FFMPEG)            proc.StartInfo.FileName = ffmpegLocation;            // Arguements to pass to file            proc.StartInfo.Arguments = arguement;            // Use shell execute            proc.StartInfo.UseShellExecute = false;            // Don't show FFMPEG            proc.StartInfo.CreateNoWindow = false;            // Do not steal information from the FFMPEG window             proc.StartInfo.RedirectStandardOutput = false;            proc.StartInfo.RedirectStandardInput = false;            proc.StartInfo.RedirectStandardError = false;            // Start FFMPEG            proc.Start();            // Change processor priority from 'Normal' to 'Below Normal'            proc.PriorityClass = System.Diagnostics.ProcessPriorityClass.BelowNormal;            // Don't do anything else until it is done            proc.WaitForExit();            // Once FFMPEG is finished it will close. Just be sure that it is            proc.Close();            // Exit out of function            return;        }    }}
What I want is for the program to read the output from Ffmpeg.exe...


--- Code: C# ---StreamReader sr = proc.StandardError;                      while ((s = sr.ReadLine()) != null)            {                t += s;            }             doSomethingWith(t);
... and update a progress bar and text box based on what the streamreader reads.

So I thought I needed to pass the text box, but is there a better way?

Navigation

[0] Message Index

[*] Previous page

Go to full version