0

我有以下问题。我完成了我的申请,哪一个做点什么。同时一些代码(绘制图表并将数据保存到日志文件)可以由几个线程使用。我无法同步保存这些线程的数据。文件被其他进程使用有一些例外。在 form1.cs 文件上,我正在启动这个线程,这些线程正在另一个文件(charts.cs)上启动函数。

form1.cs 文件的一部分:

UserControl1 us = ctrl as UserControl1;
us.newThread = new Thread(new ThreadStart(us.wykres.CreateChart));
us.newThread.Start();

图表.cs 文件:

public class Charts
{
    private StreamWriter sw = new StreamWriter("logFile.txt", true);

    static readonly object LogLock = new object();

    private ZedGraphControl zzz;

    public ZedGraphControl ZZZ
    {
        get { return zzz; }
        set { zzz = value; }
    }

    private UserControl1 uc1;

    public UserControl1 Uc1
    {
        get { return uc1; }
        set { uc1 = value; }
    }
    //jakiś kod

    void WriteLog(string wpis, StreamWriter streamW)
    {
        lock (LogLock)
        {
            streamW.WriteLine(wpis);
            streamW.Flush();
        }
    }

    public void CreateChart()
    {
        try
        {
            //tutaj znów jakiś kod
            //poniżej najważniejsza

                while ()
                {

                    if ()
                    {

                        if (go == false)
                        {
                            ZZZ.Invoke(Uc1.warnDelegate, "Osiągnięto strefę bezpiecznych wartości");
                        }

                        wpis = "jakis string";
                        WriteLog(wpis, sw);
                        wpis = null;
                    }
                    if ()
                    {
                        if ()
                        {
                            ZZZ.Invoke(Uc1.warnDelegate, "Osiągnięto strefę 1");
                        }

                        wpis = "jakis string";
                        WriteLog(wpis, sw);
                        wpis = null;
                    }
                    else if ()
                    {
                        if ()
                        {
                            ZZZ.Invoke(Uc1.warnDelegate, "Osiągnięto strefę 2");
                        }

                        wpis = "jakis string";
                        WriteLog(wpis, sw);
                        wpis = null;
                    }

                //jakiś kod odnośnie rysowania wykresow

                ZZZ.Invoke(Uc1.myDelegate);
                Thread.Sleep(odstepCzasu * 1000);
                }
        }
        catch (InvalidOperationException e)
        {
            MessageBox.Show(e.Message);
        }
        catch (ThreadAbortException)
        {

        }
    }         
}

}

userControl1.cs 文件的一部分:

public delegate void RefreshDelegate();
public delegate void ShowWarningDialogDelegate(string aaa, string bbb, string ccc);        
public RefreshDelegate myDelegate;
public ShowWarningDialogDelegate warnDelegate;
public Thread newThread = null;

public Charts wykres = null;

public UserControl1()
{
    InitializeComponent();
    wykres = new Charts();
    wykres.ZZZ = zedGraphControl1;
    wykres.Uc1 = this;
    myDelegate = new RefreshDelegate(wykres.ZZZ.Refresh);
    warnDelegate = new ShowWarningDialogDelegate(minDelegate);
}

private void minDelegate(string strLabel1, string strLabel2)        
{
    WarningForm forma = new WarningForm(strLabel1, strLabel2);
    forma.Show();
}

你能告诉我如何将它同步到几个线程同时访问一个日志文件(当他们想要保存一些东西时)吗?我听说这是典型的生产者-消费者问题,但我不知道如何在我的情况下使用它。对于任何暂停,我都会非常高兴。问候。

4

3 回答 3

1

You can use the lock() function of C# to lock an object which will allow you to only allow one thread at a time inside the lock() function.

1) Create an object to use as a lock in your class.

static readonly object LogLock = new object();

2) Move your logging code into it's own method and use the lock() function to force only one thread at a time to execute the critical area, in this case the StreamWriter stuff.

        void WriteLog(string wpis, StreamWriter sw)
    {
        lock (LogLock)
        {
            sw.WriteLine(wpis);
            sw.Flush();
        }
    }

3) Call your threadsafe logging method concurrently with as many threads as you want.

WriteLog("test log text.", sw);
于 2011-03-15T19:05:02.047 回答
0

您可以创建其他方法来写入日志。然后你可以同步这个方法。

于 2011-03-15T18:16:16.003 回答
0

看看Semaphore课堂。您可以使用它来同步访问文件的线程。简而言之,您希望在任何给定时间点只有一个线程写入文件。

于 2011-03-15T18:16:25.510 回答