Pagina 2 di 2 primaprima 1 2
Visualizzazione dei risultati da 11 a 14 su 14
  1. #11
    Allora. Ricominciamo.
    Come mi hai consigliato, la form ora non ha bisogno di parametri, si modifica con dei metodi pubblici.
    Ora.
    Ricapitoliamo. Non posso usare un timer nella form, perchè non devo effettuare un Refresh, bensì, devo simulare la ricezione della stringa ogni tot secondi.

    Ti pongo una domanda diretta, così forse riesco a risolvere il mio problema.

    Ho questa classe che riceve la stringa, fa i suoi controlli, crea l'interfaccia, la modifica ... ed ora?

    Ho la form modificata, viene modificata alla ricezione di ogni stringa (che riesco a simulare perfettamente).

    Il mio problema è semplice, come faccio nel main ad aggiornare questa form?

    Se la do in pasto alla Applcation.Run, senza thread, l'esecuzione è statica, non posso più modificare nulla.
    Se metto l'application.run in un thread apposito, riesco a modificare l'interfaccia, ma non riesco a visualizzarla con La Show(), ma solo con la ShowDialog(), e anch'essa deve esser necessariamente in un thread, altrimenti non posso più eseguire altre istruzioni!

    Non capisco quando mi dici di mettere tutto nella form. Come faccio a mettere il main al suo interno?
    A cosa ti riferisci con Form secondario??

    Comunque grazie mille per l'aiuto!

    Non ti posto il progetto perchè è abbastanza lungo, non vorrei scocciarti.

  2. #12
    Utente di HTML.it L'avatar di U235
    Registrato dal
    Mar 2006
    Messaggi
    1,536
    ciao, no figurati, non mi scocci se posso cerco sempre di dare il mio aiuto, a volte imparando anche io.

    Alla tua domanda diretta provo a rispondere in maniera diretta :
    agisci sul campo statico della program che contiene il form da modificare.


    forse mi sta sfuggendo qualcosa, ma se ad esempio vuoi ottenere la ricezione della stringa nella Program, dovresti creare un campo statico che contiene il form che lanci, e quando ricevi la stringa agisci sul metodo del form dal campo statico.

    quel che ti consigliavo io era di non utilizzare un campo statico nella program, ma ingloabare tutta la logica dentro il form, ma comunque avrai i tuoi motivi per volerlo li.


    un doppio esempio (sia con ricezione in program , sia eventualmente ricezione su form) :

    NB
    al posto del timer puoi usare tranquillamente un thread separato che simula in modo asoincrono la ricezione della stringa come fai tu nella tua classe.

    program.cs
    codice:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    using System.IO;
    
    namespace test2
    {
        static class Program
        {
            static Form1 form;//dichiaro un campo statico di tipo form1 che conterrà appunto il nostro form.
            static Timer timer;
            static int index;
            static string[] listaWav;
            /// <summary>
            /// Punto di ingresso principale dell'applicazione.
            /// </summary>
            [STAThread]
            static void Main()
            {            
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                form = new Form1();//qui valorizzo il campo statico che conterrà il form
                //commentando da gli spezzoni di codice racchiuso tra gli asterisco  e togliendolo da quello dal Form1 ottieni lo stesso risultato
                //*****************************************
                timer = new Timer();
                timer.Interval = 5000;
                timer.Tick += new EventHandler(timer_Tick);
                listaWav = Directory.GetFiles(Application.StartupPath, "*.wav");
                if (listaWav.Length > 0)
                {
                    form.CambiaMusica(listaWav[index++]);
                    timer.Enabled = true;
                    timer.Start();
                }
                //******************************************
                Application.Run(form);
            }
            //*************************************************
            static void timer_Tick(object sender, EventArgs e)
            {
                if (index < listaWav.Length)
                {
                    //qui chiamo la modifica del form
                    form.CambiaMusica(listaWav[index]);
                }
                else timer.Stop();
                index++;
            }
            //*************************************************
        }
    }

    form1.cs
    codice:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Media;
    using System.IO;
    
    namespace test2
    {
        public partial class Form1 : Form
        {
            SoundPlayer sp;
            Timer timer;
            string[] listaWav;
            int index;
            public Form1()
            {
                InitializeComponent();
                index=0;
                sp = new SoundPlayer();
                this.BackColor = Color.Black;
                //se togli il commento alle righe sotto, non hai bisogno di chiamare la modifica dal main del programma
                //il timer simula solo la chiamata di quando arriva una nuova stringa
                /*timer = new Timer();
                timer.Interval = 5000;
                timer.Tick += new EventHandler(timer_Tick);
                listaWav = Directory.GetFiles(Application.StartupPath,"*.wav");
                if (listaWav.Length > 0)
                {
                    sp.SoundLocation = listaWav[index++];
                    sp.Play();
                    timer.Enabled = true;
                    timer.Start();
                }*/
            }
            /*
            void timer_Tick(object sender, EventArgs e)
            {
                if (index < listaWav.Length)
                {
                    CambiaMusica(listaWav[index]);
                }
                else timer.Stop();
                index++;
            }
            */
    
            //questo simula la modifca chiamata dalla ricezione della stringa
            public void CambiaMusica(string path)
            {
                sp.Stop();
                sp.SoundLocation = path;
                sp.Play();
                if (this.BackColor == Color.Black)
                    this.BackColor = Color.White;
                else
                    this.BackColor = Color.Black;
            }
        }
    }
    nella program, dopo aver avviato il form, simulo con un timer la ricezione della stringa ogni 5 secondi, alla ricezione, quindi, nel metodo chiamato dal timer, chiamo il metodo del form che effettua la modifica, che, in questo caso, si tratta del cambio di musica e colore di sfondo, direttamente dalla program. Invece commentando le parti con asterico nella program, e tolgliendo i commenti nel pezzo dentro la form1, faccio in modo che la program avvii solo il form, e dopo sarà il form a fare tutto da solo (in questo caso simulando da dentro se stesso la logica che riceve la stringa).

    chiaramente, se vuoi puoi postare il tuo codice, o magari mettere un link dove scaricarlo credo sia gradito (perlomeno a me) cosi si capisce meglio perchè agisci in questo modo.

    P.S.
    per provare il codice che ho postato dovresti mettere qualche file .wav nella cartella dell'eseguibile.

  3. #13
    Ok, grazie mille.
    Ho risolto in parte il problema.
    Tuttavia, ce n'è uno che persiste.
    Per la program, l'ho gestita come dici tu, e va alla grande. Il problema è unico, non mi aggiorna la finestra corrente!
    Io credo che il problema sia nella Form, che ora ti posto.

    codice:
    using System;
    
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.IO;
    
    namespace SmsMobilev1
    {
        public partial class Interface : Form
        {
            // Importazione della libreria per l'esecuzione del file audio
            [DllImport("coredll.dll")]
            private static extern int PlaySound(string szSound, IntPtr hModule, int flags);
    
            /// <summary>
            /// Flags per l'esecuzione del suono
            /// </summary>
    
            [Flags]
            private enum PlaySoundFlags
            {
                SndSync = 0x0,
                SndFilename = 0x20000,
            }
    
    
            // Costanti
            public const string splitFault = "/";
            public const int nSensors = 10;
            public const string pathSounds = @"\sounds";
            public const string nameSoundAllarm = @"\Allarm.wav";
    
            public Interface()
            {
                InitializeComponent();
    ******************************************************************************************************************************
    Se quì ci metto una this.Show(), o una this.ShowDialog(), riesco a visualizzare l'interfaccia aggiornata, ma su
    una nuova finestra! Non è ciò che vorrei.
    *****************************************************************************************************************************
            }
    
            private void FrmStartAudio_Load(object sender, EventArgs e)
            {
                // Path del File Audio
                string path;
                path = Path.GetDirectoryName(
                        System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
    
                // Esecuzione del file
                PlaySound(path + pathSounds + nameSoundAllarm,
                    IntPtr.Zero, (int)(PlaySoundFlags.SndFilename)
                                 | (int)(PlaySoundFlags.SndSync));
            }
    
            private void menuItem1_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
    
            private void menuItem2_Click(object sender, EventArgs e)
            {
                new viewHistory().ShowDialog();
            }
    
            public void setValueText(string text) {
                this.ValueValue.Text = text;
    *****************************************************************************************************************************
    Secondo me, per ogni set, ci vorrebbe un istruzione, che però ne conosco, ne ho trovato, per dire di aggiornare
    l'elemento.
    ********************************************************************************************************************************
            }
    
            public void setTimeText(string text)
            {
                this.TimeValue.Text = text;
            }
    
            public void setUnitText(string text)
            {
                this.UnitValue.Text = text;
            }
    
            public void setFaultText(string fault,string nSensors)
            {
                this.FaultValue.Text = fault + "/" + nSensors;
            }
    
            public void setInterpretation(bool interpretation) {
                this.Interpretation = interpretation;
                if (this.Interpretation)
                {
                    // 
                    // FaultValue
                    // 
                    this.FaultValue.Font = new System.Drawing.Font("Courier New", 10F, System.Drawing.FontStyle.Regular);
                    this.FaultValue.ForeColor = System.Drawing.Color.Red;
                    this.FaultValue.Location = new System.Drawing.Point(192, 150);
                    this.FaultValue.Name = "FaultValue";
                    this.FaultValue.Size = new System.Drawing.Size(48, 20);
                    // 
                    // UnitValue
                    // 
                    this.UnitValue.Font = new System.Drawing.Font("Courier New", 10F, System.Drawing.FontStyle.Regular);
                    this.UnitValue.ForeColor = System.Drawing.Color.Red;
                    this.UnitValue.Location = new System.Drawing.Point(150, 150);
                    this.UnitValue.Name = "UnitValue";
                    this.UnitValue.Size = new System.Drawing.Size(43, 20);
                    // 
                    // ValueValue
                    // 
                    this.ValueValue.Font = new System.Drawing.Font("Courier New", 10F, System.Drawing.FontStyle.Regular);
                    this.ValueValue.ForeColor = System.Drawing.Color.Red;
                    this.ValueValue.Location = new System.Drawing.Point(98, 150);
                    this.ValueValue.Name = "ValueValue";
                    this.ValueValue.Size = new System.Drawing.Size(34, 20);
                    // 
                    // TimeValue
                    // 
                    this.TimeValue.Font = new System.Drawing.Font("Courier New", 10F, System.Drawing.FontStyle.Regular);
                    this.TimeValue.ForeColor = System.Drawing.Color.Red;
                    this.TimeValue.Location = new System.Drawing.Point(42, 150);
                    this.TimeValue.Name = "TimeValue";
                    this.TimeValue.Size = new System.Drawing.Size(50, 20);
                    // 
                    // flagAllarm
                    // 
                    this.flagAllarm.Location = new System.Drawing.Point(4, 138);
                    this.flagAllarm.Name = "flagAllarm";
                    this.flagAllarm.Size = new System.Drawing.Size(32, 32);
                    this.flagAllarm.Visible = true;
                    // 
                    // flagNoAllarm
                    //
                    this.flagNoAllarm.Location = new System.Drawing.Point(0, 0);
                    this.flagNoAllarm.Name = "flagNoAllarm";
                    this.flagNoAllarm.Size = new System.Drawing.Size(0, 0);
                    this.flagNoAllarm.Visible = false;
                }
                else {
                    // 
                    // FaultValue
                    // 
                    this.FaultValue.Font = new System.Drawing.Font("Courier New", 10F, System.Drawing.FontStyle.Regular);
                    this.FaultValue.ForeColor = System.Drawing.Color.Lime;
                    this.FaultValue.Location = new System.Drawing.Point(192, 150);
                    this.FaultValue.Name = "FaultValue";
                    this.FaultValue.Size = new System.Drawing.Size(48, 20);
                    // 
                    // UnitValue
                    // 
                    this.UnitValue.Font = new System.Drawing.Font("Courier New", 10F, System.Drawing.FontStyle.Regular);
                    this.UnitValue.ForeColor = System.Drawing.Color.Lime;
                    this.UnitValue.Location = new System.Drawing.Point(150, 150);
                    this.UnitValue.Name = "UnitValue";
                    this.UnitValue.Size = new System.Drawing.Size(43, 20);
                    // 
                    // ValueValue
                    // 
                    this.ValueValue.Font = new System.Drawing.Font("Courier New", 10F, System.Drawing.FontStyle.Regular);
                    this.ValueValue.ForeColor = System.Drawing.Color.Lime;
                    this.ValueValue.Location = new System.Drawing.Point(98, 150);
                    this.ValueValue.Name = "ValueValue";
                    this.ValueValue.Size = new System.Drawing.Size(34, 20);
                    // 
                    // TimeValue
                    // 
                    this.TimeValue.Font = new System.Drawing.Font("Courier New", 10F, System.Drawing.FontStyle.Regular);
                    this.TimeValue.ForeColor = System.Drawing.Color.Lime;
                    this.TimeValue.Location = new System.Drawing.Point(42, 150);
                    this.TimeValue.Name = "TimeValue";
                    this.TimeValue.Size = new System.Drawing.Size(50, 20);
                    // 
                    // flagAllarm
                    // 
                    this.flagAllarm.Location = new System.Drawing.Point(0, 0);
                    this.flagAllarm.Name = "flagAllarm";
                    this.flagAllarm.Size = new System.Drawing.Size(0, 0);
                    this.flagAllarm.Visible = false;
                    // 
                    // flagNoAllarm
                    // 
                    this.flagNoAllarm.Location = new System.Drawing.Point(4, 138);
                    this.flagNoAllarm.Name = "flagNoAllarm";
                    this.flagNoAllarm.Size = new System.Drawing.Size(33, 32);
                    this.flagNoAllarm.Visible = true;
                }
            }
        }
    }
    Ora ti posto la program e ti faccio vedere, come, con il tuo aiuto ho semi-risolto il problema.

    codice:
    using System;
    
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using System.Windows.Forms;
    
    namespace SmsMobilev1
    {
        static class SmsMobileEcecute
        {
            // Timer, per la temporizzazione della ricezione delle stringhe
    
    ***********************************************************************************************************************************
    I membri statici che servono nel mio caso, sono solo questi due. smsMobile, è la classe nella quale è presente
    il metodo per la ricezione delle stringhe. Il perchè te lo spiego tra un pò.
    ************************************************************************************************************************************
    
            static Timer timer;
            static SmsMobile smsMobile;
    
             // Costanti
            public const string subPathSurvey = @"\textFile";
            public const string nameSurveyFile = @"\rilevazioni.txt";
    
            [MTAThread]
            static void Main()
            {
                timer = new Timer();
                timer.Interval = 10000;
                timer.Tick += new EventHandler(timer_Tick);
                smsMobile = new SmsMobile();
                timer.Enabled = true;
                Application.Run(smsMobile.currentInterface);
    *********************************************************************************************************************************
    La form non è un membro statico, perchè è già un membro della classe SmsMobile
    *********************************************************************************************************************************
            }
    
            static void timer_Tick(object sender, EventArgs e)
            {
                string pathSurevy = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
                + subPathSurvey + nameSurveyFile;
                string relativePath = subPathSurvey + nameSurveyFile;
                if (countLines(pathSurevy) > 0)
                {
    
    **************************************************************************************************************************************
    in questo caso, l'evento della scadenza del timer, non fa altro che "ricevere la stringa".
    la ricezione della stringa, nella classe SmsMobile, si occupa di far partire le set per la form, dove la currentInterface
    è un membro della classe stessa.
    ***************************************************************************************************************************************
    
                    string survey = extractLine(relativePath, new object(), new EventArgs());
                    smsMobile.receiveSurvey(survey);
                }
                else timer.Enabled = false;
            }
    
           //Altra roba che non serve!
        }
    }
    Ora secondo me dovremmo arrivare al problema di fondo!

    Edit:

    Mancano:
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(fals e);
    timer.Stop()
    timer.Start()
    perchè non sono supportate dal .net 2.0 Windows Mobile!

  4. #14
    Non sono ancora riuscito a capire come è possibile aggiornare una Form, data in pasto ad una Application.run!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.