-2

C# で Windows サービスを作成し、それを Windows フォームに変換しました。(動作させるために必要な変更を行ったので心配しないでください)

そのため、突然一部の機能が機能しなくなりました。フォームでエラーが発生することはありませんが、実行すべきことを実行していません。関連するコードは次のとおりです。

    private void Start_Click(object sender, EventArgs e)
    {
        this.fileSystemWatcher1 = new System.IO.FileSystemWatcher();
        ((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).BeginInit();
        fileSystemWatcher1.Path = source;
        fileSystemWatcher1.Created += new FileSystemEventHandler(fileSystemWatcher1_Created);
        fileSystemWatcher1.Changed += new FileSystemEventHandler(fileSystemWatcher1_Changed);
        fileSystemWatcher1.Deleted += new FileSystemEventHandler(fileSystemWatcher1_Deleted);
        fileSystemWatcher1.Renamed += new RenamedEventHandler(fileSystemWatcher1_Renamed);

        this.fileSystemWatcher1.EnableRaisingEvents = true;
        this.fileSystemWatcher1.IncludeSubdirectories = true;
        ((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).EndInit();
        logger("Service started " + DateTime.Now);
    }

    public static void logger(String entry)
    {
        String logfile = ConfigurationManager.AppSettings[@"log"];
        if (File.Exists(@logfile))
        {
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(@logfile, true))
            {
                file.WriteLine(entry);
            }
        }
        else
        {
            string[] lines = { entry };
            System.IO.File.WriteAllLines(@logfile, lines);
        }
    }

//some more functions as the logger.


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

            cut_copy = ConfigurationManager.AppSettings[@"cutter"];
            logger("File created> " + e.FullPath + " -Date:" + DateTime.Now);
            filepath = Path.Combine(source, e.Name);
            name = Path.GetFileNameWithoutExtension(filepath);
            extension = Path.GetExtension(e.FullPath);
            size = e.Name.Length;

            strSelectCmd = "INSERT INTO" + tablepostgresql + " (" + column1 + "," + column2 + "," + column3 + "," + column4 + ") VALUES('" + name + "','" + size + "',now(),'" + extension + "')";
            readquery = "select * from " + tablemysql + " where name='" + name + "'";
            Mysql();
            postgresql();
            Record();

            if (string.IsNullOrEmpty(filename) == false)
            {
                if (Directory.Exists(e.FullPath))
                {
                    copyfolder();
                    Directory.CreateDirectory(target);
                }
                else
                {
                    if (WaitForFileAvailable(e.FullPath, TimeSpan.FromSeconds(30)))
                    {
                        var file = Path.Combine(source, e.Name);
                        var copy_file = Path.Combine(target, e.Name);
                        var destination = Path.Combine(target, Path.ChangeExtension(source, Path.GetExtension(source)));


                            if (File.Exists(file))// Check to see if the file exists. 
                            {                     //If it does delete the file in the target and copy the one from the source to the target.
                                File.Delete(copy_file);

                            }
                            File.Copy(e.FullPath, copy_file);

                    }
                    else // The file failed to become available within 10 seconds.
                    {
                        logger("Copy has failed reason: File is being used by another program");
                    }
                }
            }
            else
            {
                query = "INSERT INTO " + tablemysql + " (name,size,last_edit,extension) VALUES('" + name + "','" + size + "',now(),'" + extension + "')";
                Mysql();
            }
            variable_reset();

    }

私の問題は、ボタンのすべての機能が のように機能しているが、機能logger("Service started " + DateTime.Now);fileSystemWatcher1_Createdない (何もしない) ことです。これは非常にばかげた質問かもしれませんが、私は非常に混乱しており、これに取り組んでからかなり長い時間が経ちました。

4

1 に答える 1

1

のプロパティSynchronizingObjectFileSystemWatcherフォームに設定します。

this.fileSystemWatcher1 = new System.IO.FileSystemWatcher();
this.fileSystemWatcher1.SynchronizingObject = this;

FileSystemWatcher.SynchronizingObject Property@MSDN

Changed、Created、Deleted、および Renamed イベントがボタンなどの視覚的な Windows フォーム コンポーネントによって処理される場合、システム スレッド プールを介したコンポーネントへのアクセスが機能しないか、例外が発生する可能性があります。これを回避するには、SynchronizingObject を Windows フォーム コンポーネントに設定します。これにより、Changed、Created、Deleted、および Renamed イベントを処理するメソッドが、コンポーネントが作成された同じスレッドで呼び出されます。

サイドノート:プロパティSynchronizingObjectISynchronizeInvoke、そしてあまりにもですControlISynchronizeInvoke

于 2013-10-30T15:43:31.783 に答える