【问题标题】:From Service to Form suddenly doesn't work [closed]从服务到形式突然不起作用[关闭]
【发布时间】:2013-10-30 23:52:34
【问题描述】:

我用 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 不起作用(它什么都不做)。 这可能只是一个非常愚蠢的问题,但我很困惑,而且我已经很久没有研究这个问题了。

【问题讨论】:

  • “不起作用”不能很好地描述您的问题。什么不起作用?是否调用了 fileSystemWatcher1_Deleted 方法? “它只是没有做它应该做的事情” => 它应该做什么?
  • @CyrilGandon 对不起,我的意思是它什么都不做。这并不是说我遇到错误或其他事情或做他不应该做的事情,但它什么也没做。
  • 您是否在该段代码中放置了断点?您是否将 pause 变量设置为 true 以便 if 块实际执行?
  • 解释“不起作用”。调试。你的Start_Click 运行完成了吗? fileSystemWatcher1_Deleted 是否被调用? pause 的值是多少?
  • 我以为您说您已经进行了所有必要的更改以使其正常工作?另外,您确定要检查if (pause) 而不是if (!pause)

标签: c# winforms


【解决方案1】:

FileSystemWatcher 的属性SynchronizingObject 设置为您的表单:

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 事件在同一个上调用 创建组件的线程。

旁注:属性SynchronizingObjectISynchronizeInvokeControl 也是ISynchronizeInvoke

【讨论】:

  • 好的,这可能对我有帮助,但仍然没有解决我的主要问题。仍然给你答案,因为我似乎无法在这里得到答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-10
  • 2013-09-29
  • 1970-01-01
  • 2014-11-16
  • 1970-01-01
  • 1970-01-01
  • 2015-02-21
相关资源
最近更新 更多