English 中文(简体)
统一资料存档的最佳途径
原标题:Optimal way to synchronize saving information to file

我的守则如下:

void OnEvent(....){ SaveData();}

———— 数据看起来像:

void SaveData()
{
    new Thread(
        ()=>
        {
            lock(data)
            {
                Serialize(data);
            }
        }).Start();
}

我的问题是,在序列化过程中,该事件可能数次发射,我希望在最初的序列化过程结束之后,才能完成序列化。

这样做的最佳方式是什么?

问题回答

你需要处理的一些问题:

第一,不使用<代码> 阅读类别。 确实如此。 除非你重写平行处理的框架,否则如果你重新使用<代码>Thread/,你几乎肯定会犯错。 采用System.Threading.Tasks.Task或(如果在4.0之前重新采用System.Threading.ThreadPool,以作为同义执行。

Second, you should be aware that sharing instance variables like data between threads is sticky. You re much better off passing the data that you want to save to the async function so that there are no issues of contention or race conditions.

第三,如果是执行命令,你想要做些什么:

<things happen>
OnEvent fires
<things happen>
Serialize starts
OnEventFires
Serialize completes

code>OnEvent fires in the summary of 以来,它是否完全忽视了它,或者它是否计划了另一个序列化? 如果你们在这里回答“b”的话,应该怎么做?

<things happen>
OnEvent fires
<things happen>
Serialize starts
OnEvent fires
<things happen>
OnEvent fires
Serialize completes

Should two calls to Serialize be scheduled, or just one?





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签