Jiew Meng

Web Developer, Computer Science Student

Posts tagged "csharp"

MarkdownEdit: Word Processing Powered by Markdown

The Problem

After using Markdown in StackOverflow for sometime, I grown fond of it and wanted to use it for any generic documents, an alternative for Microsoft Word for example. There wasn’t any app I found that uses it. Also Microsoft Word is bloated for my needs, plus, it isn’t good for code, no way I found to have syntax highlighting. So I started developing MarkdownEdit

The Vision

Word processing powered by Markdown

The Result

it can also generate HTML using Templates

Download

This project is release Free & Open Source under the MIT License. Source Code on GitHub

Read More

Posts tagged "csharp"

Posts tagged "csharp"

C#: Using The BackgroundWorker for Time Consuming/Blocking Tasks

Suppose you have some time-consuming “very useful” function like below

for (var i = 1; i <= 10; i++)
{
    Thread.Sleep(TimeSpan.FromMilliseconds(i * 200));
}

If it runs on the UI thread, you will get a “hanged” UI (you can’t click buttons, drag the window etc). To fix that, you can use a BackgroundWorker, its easy. There are 3 main events that you need to handle

  • DoWork - where all your processing code will go
  • ProgressChanged - to do something when progress changes. eg. update progress bar
  • RunWorkerCompleted - triggered when background operation (DoWork) has completed, been canceled or raise an exception

The code below is very simple and self-explanatory, ask me in the comments if you have any questions

public MainWindow()
{
    InitializeComponent();

    #region Worker Stuff
    _worker = new BackgroundWorker();
    _worker.WorkerReportsProgress = true;
    _worker.WorkerSupportsCancellation = true;

    // Where your useful, time-consuming, blocking code will go
    _worker.DoWork += (s, args) =>
    {
        for (var i = 1; i <= 10; i++)
        {
            // handle the cancelation of the task
            if (_worker.CancellationPending) 
            {
                args.Cancel = true;
                return;
            }
            _worker.ReportProgress(i * 10);
            Thread.Sleep(TimeSpan.FromMilliseconds(i * 200));
        }
        args.Result = "Done!";
    };

    // As progress is changed, update the UI
    _worker.ProgressChanged += (s, args) =>
    {
        progBar.Value = args.ProgressPercentage;
        txtLog.Text = args.ProgressPercentage + "%";
    };

    // Handle when the worker is canceled, raises an exception or completes 
    _worker.RunWorkerCompleted += (s, args) =>
    {
        if (args.Cancelled)
            txtLog.Text = "Canceled";
        else if (args.Error != null)
            txtLog.Text = args.Error.Message;
        else
            txtLog.Text = args.Result.ToString();
    };
    #endregion
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    _worker.RunWorkerAsync(); // Starts running code in DoWork()
}

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    _worker.CancelAsync(); // Cancel the operation. BackgroundWorker.CancelationPending will be set to true
}

A video showing the outcome,

Source Code: on MediaFire

Posts tagged "csharp"

Differences between BinaryWriter.Write(string) vs StreamWriter.Write(string)

An important point from the MSDN Documentation

This method first writes the length of the string as a UTF-7 encoded unsigned integer, and then writes that many characters to the stream by using the BinaryWriter instance’s current encoding

So the length of string is prefixed then the text. So to avoid the prefix, just encode your string into bytes

BinaryWriter memBW = new BinaryWriter(memStream, encoder);
memBW.Write(encoder.GetBytes("Hello World"));

Search

Loading

Likes

Following