Author Topic: Await Task Asyn calling from button form not worked  (Read 526 times)

0 Members and 1 Guest are viewing this topic.

daboho

  • Newt
  • Posts: 40
Await Task Asyn calling from button form not worked
« on: October 05, 2022, 09:49:23 AM »
this code if using commandmethod is run as well but if
code run from button not work el Invalid input what is wrong
Code: [Select]
[CommandMethod("Cetak")]
        public async void cetakKePloter()
        {
            var doc = acApp.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            var layCols = new List<Layout>();
            using (var tr = db.TransactionManager.StartTransaction())
            {
                DialogResult dresult = MessageBox.Show("Yakin Mau Plot Semua Layout Ke Printer:", "Plotting..", MessageBoxButtons.YesNo);
                if (dresult == DialogResult.No) return;
                var lays = tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;
                foreach (var item in lays)
                {
                    var key = item.Key;
                    var value = item.Value;
                    ed.WriteMessage($"\nkey is {key} and item is {value}");
                    if (key.ToUpper() == "MODEL") continue;
                    var lay = tr.GetObject(value, OpenMode.ForWrite) as Layout;
                    string name = lay.LayoutName;
                    layCols.Add(lay);

                }
                layCols.Sort((p1, p2) => p2.TabOrder.CompareTo(p1.TabOrder));

                short bgPlot = (short)acApp.GetSystemVariable("BACKGROUNDPLOT");
                acApp.SetSystemVariable("BACKGROUNDPLOT", 0);

                foreach (var l in layCols)
                {

                    LayoutManager.Current.CurrentLayout = l.LayoutName;
                    ed.WriteMessage($"\nlayout name is {l.LayoutName}\n");
                    await ed.CommandAsync("-plot", "No");
                    await ed.CommandAsync(l.LayoutName);
                    await ed.CommandAsync("");
                    await ed.CommandAsync("", "N", "N", "Y");
                    Thread.Sleep(1000);
                }

                acApp.SetSystemVariable("BACKGROUNDPLOT", bgPlot);

                tr.Commit();
            }

        }
if code run using form button1 not working el invalid input what is wrong
Code: [Select]
  private async void button5_Click(object sender, EventArgs e)

n.yuan

  • Bull Frog
  • Posts: 333
Re: Await Task Asyn calling from button form not worked
« Reply #1 on: October 06, 2022, 09:35:04 AM »
You need to indicate how the code is called in the Button_Click event handler. That is, do you have all the lines of code copied from the CommandMethod to the button_click handler (thus, the code includes at "await...", so the lines after the "await..." are actually waiting for their turns); or you simply call the CommandMethod directly"

private async void Button_Click(....)
{
   cetakKePloter();
}

if so, the button_Click will not wait for the call to be completed, because the method cetakKePloter() is not awaitable (not returning a Task).

If you want the whole thing the the commandmethod can be shareable between the CommandMethod and the button_click. you need to wrap up the code as a awaitable async method (i.e. return a Task):

private async Task DoPlotting()
{
   ...
   await ....
   ...
}

Then the CommandMethod:
[CommandMethod(...)]
public async void cetakKePloter()
{
   await DoPlotting();
}

The button_click:
private async void Button_Click(...)
{
  await DoPlotting();
}

daboho

  • Newt
  • Posts: 40
Re: Await Task Asyn calling from button form not worked
« Reply #2 on: October 06, 2022, 12:14:48 PM »
I Has try before all
called from method but still el_invalidinput,can you try this ?
Code: [Select]
private asyn void button_click(...)
{
  //call method
}
private asyn void method1()
{
await....
}
but still el invalid input

n.yuan

  • Bull Frog
  • Posts: 333
Re: Await Task Asyn calling from button form not worked
« Reply #3 on: October 06, 2022, 11:22:00 PM »
Ah, it is the Editor.Command()/CommandAsync() issue: it can only be used in DocumentContext. Are you using mofeless form? If yes, then you cannot run the code directly. You need to use SendStringToExecute() to call the Command "CetAk".

daboho

  • Newt
  • Posts: 40
Re: Await Task Asyn calling from button form not worked
« Reply #4 on: October 07, 2022, 09:51:20 AM »
if using  SendStringToExecute()
while my code is get variable from control in button like from combobox etc that is problem again
because SendStringToExecute()  only run in last of command
can you try this sir because my plooter must using await if not using await my ploter every even page is blank page,can you help me ?