0

I would like to ask anyone of you if you have tried this kind of scenario. I developed a windows application in c#. In main Program (Program.cs) I added a thread mutex just to multitask many operations executed under it.

//under Program.cs
public static FormOneInstance frmOneInstance;
    static void Main()
    {
         t = new Thread(new ThreadStart(CreateInputOutput));
         t.Start();
    }
    public static void CreateInputOutput()
    {
                try
                {
                    mutex.WaitOne(); 
                    ExecuteManyOperationsHere(); 
                }
                finally
                {
                    mutex.ReleaseMutex();
                    Thread.CurrentThread.Abort(); //Has ThreadAbortException here
                    t = null;
                }
      }



  public static void ExecuteManyOperationsHere()
    {
      frmOneInstance  =new FormOneInstance (); //this has lot of execution on formLoad that includes ShowSummary()
    }

And on my other Form FormOneInstance class, I used System.Windows.Forms Timer

 private void timer1_Tick(object sender, EventArgs e)
        {

            ShowSummary();
        }

Now, I want to separate the thread in main and UI. Thanks!

Boas Enkler
  • 12,264
  • 16
  • 69
  • 143
Juran
  • 177
  • 1
  • 8
  • 1
    You need message loop for this timer... Note that it is really unclear why you pick that timer for console app... and what code with `mutex.WaitOne` should represent. It may be good idea to read [MCVE] guidance to improve the question. – Alexei Levenkov Apr 04 '16 at 04:04
  • This is not a console app. why message loop is needed? – Juran Apr 04 '16 at 05:12
  • Mutexes are a synchronization mechanism for two or more threads to access a shared resource at the same time. I don't see the mutex being used by more than one thread and I don't see what resource is being synchronized. If timer1_Tick is being used to show progress/results from the other thread then it is not likely to be that easy. Are you familiar with Background Workers? – Sam Hobbs Apr 04 '16 at 06:18
  • Yes, I used that before but my application still hangs up. So, when I tried using mutex, it runs smoothly – Juran Apr 04 '16 at 06:54
  • @user34660 I already updated my post. – Juran Apr 04 '16 at 07:23
  • It is still not clear what your requirements are. – Sam Hobbs Apr 04 '16 at 17:44
  • All i wanted is to separate the thread in main and UI forms – Juran Apr 05 '16 at 08:28

2 Answers2

1

The Windows Forms Timer runs on the ui thread and there is only one see this SO post.

See for example the Threading.Timer on MSDN

You could use a timer which doesn't this stuff. but be sure to invoke back, to the ui thread when the timer code wants to change values that are shown in the ui

myTimer= new Timer( handleTimer, null, timerIntervall, Timeout.Infinite );

Then you can create a method which invokes your CreateInputOutput method on the ui thread in order to show a new ui element

public void handleTimer(object State)
{
// PLace here the blocking code which shoudl not block the ui
this.invoke(...)  // here you can call the method which should be run un the ui thread. Like creating new forms
}

}

for more details see also this post about threading timer

Community
  • 1
  • 1
Boas Enkler
  • 12,264
  • 16
  • 69
  • 143
1

Since it is not clear what your requirements are, I will assume that what you really want to use is a BackgroundWorker. The following also have samples.

BackgroundWorker.ReportProgress Method (Int32, Object) (System.ComponentModel)

Walkthrough: Multithreading with the BackgroundWorker Component (C# and Visual Basic)

How to: Run an Operation in the Background

Sam Hobbs
  • 2,594
  • 3
  • 21
  • 32